import logging

from handlers.detectors import detect_is_admin
from keyboards.default.start import start_admin
from keyboards.inline.admin.success_payment import withdraw_money_balance
from loader import dp, bot, db
from data.config import ADMINS
from keyboards.default.back import back

from states.balance import Balance

from aiogram import types
from aiogram.dispatcher import FSMContext

@dp.callback_query_handler(text="withdraw_money", state=Balance.menu)
async def withdraaw_money_from_balance(call: types.CallbackQuery, state: FSMContext):
    user_id = call.from_user.id

    select_user = await db.select_user_data(user_id)
    balance = select_user[0][1]

    if balance >= 10000:
        text = "Pul yechib olish uchun karta raqami kiriting...\n\n" \
               "Mavjud to'lov turlari\n\n▪Humo"

        await call.message.delete()
        await call.message.answer(text=text, reply_markup=back)

        await Balance.withdraw.set()

    else:
        text = "⚠ Pul chiqarish uchun hisobingizda kamida 10.000 so'm bo'lishi shart!"
        await call.answer(text, show_alert=True)

@dp.message_handler(state=Balance.withdraw, content_types=types.ContentType.TEXT)
async def identify_card(message: types.Message, state: FSMContext):
    msg = message.text

    await message.answer(
        text="Yaxshi, endi nech pul chiqarib olmoqchi ekanligingizni yozing\n\nMasalan: 10000",
        reply_markup=back
    )

    await state.update_data(
        {'card_number': msg}
    )

    await Balance.money.set()

@dp.message_handler(state=Balance.money, content_types=types.ContentType.TEXT)
async def identify_how_much_money(message: types.Message, state: FSMContext):
    data = await state.get_data()
    card_number = data.get('card_number')

    user_id = message.from_user.id
    full_name = message.from_user.full_name
    user_mention = message.from_user.get_mention(name=full_name)

    msg = message.text

    select_user = await db.select_user_data(user_id)
    balance = select_user[0][2]

    try:
        summa = int(msg)
        if summa >= 10000:
            if balance >= summa:
                await bot.send_message(
                    chat_id=message.chat.id,
                    text="✅ Pul yechish uchun so'rovingiz qabul qilindi, admin tez orada to'lovni amalga oshiradi",
                    reply_markup=await detect_is_admin(user_id)
                )

                admin_text = f"👤 {user_mention}\n💳 {card_number}\n💸 {summa} so'm\n💰 {balance}"

                await bot.send_message(
                    chat_id=ADMINS[0],
                    text=admin_text,
                    reply_markup=withdraw_money_balance(user_id, summa)
                )

                await state.reset_state(with_data=False)

            elif balance < summa:
                await bot.send_message(
                    chat_id=message.chat.id,
                    text=f"Hisobingizda {balance} so'm pul va siz {summa} so'm "
                         f"chiqarishga harakat qilyapsiz, iltimos boshqattan urinib ko'ring\n\n"
                         f"Masalan: 10000",
                    reply_markup=back
                )

        else:
            text = "⚠ Eng kamida 10000 so'm chiqarib olish mumkin, summa kiriting\n\nMasalan: 10000"
            await message.answer(text, reply_markup=back)

    except ValueError as VE:
        logging.info(VE)
        await message.answer(
            text='Iltimos, faqat raqamlardan foydalaning\n\nMasalan: 10000',
            reply_markup=back
        )

@dp.callback_query_handler(text_contains="tolandi_", state='*')
async def final_withdraw(call: types.CallbackQuery, state: FSMContext):
    data = call.data
    splited = data.split('_')

    user_id = splited[1]
    summa = splited[2]

    select_user_data = await db.select_user_data(int(user_id))
    balance = select_user_data[0][2]

    get_user = await bot.get_chat(user_id)
    full_name = get_user.full_name
    user_mention = call.from_user.get_mention(full_name)

    end_balance = balance - int(summa)
    await db.update_user_balancee(end_balance, int(user_id))

    await call.message.delete()

    await bot.send_message(
        chat_id=call.message.chat.id,
        text="Foydalanuvchi puli to'lab berildi",
        reply_markup=start_admin
    )

    text = f"✅ Pul toʻlandi: {user_mention}\n" \
           f"👤 Foydalanuvchi ID: {user_id}\n" \
           f"💸 Miqdor: {summa} soʻm"

    await bot.send_message(
        chat_id=-1001943689507,
        text=text
    )

    await bot.send_message(
        chat_id=user_id,
        text=f"Sizning pul yechish so'rovingiz qabul qilindi va {summa} so'm to'lab berildi",
        reply_markup=await detect_is_admin(user_id)
    )

    await state.finish()

@dp.callback_query_handler(text_contains="bekor_qilish_", state='*')
async def final_withdraw(call: types.CallbackQuery, state: FSMContext):
    data = call.data
    splited = data.split('_')

    user_id = splited[2]
    summa = splited[3]

    await call.message.delete()

    await state.update_data(
        {'final_id': user_id, 'final_summa': summa}
    )

    await bot.send_message(
        chat_id=call.message.chat.id,
        text="To'lov bekor bo'lishining sababini kiriting",
        reply_markup=back
    )

    await Balance.cancel_payment.set()

@dp.message_handler(state=Balance.cancel_payment, content_types=types.ContentType.TEXT)
async def cancel_payment_user(message: types.Message, state: FSMContext):
    data = await state.get_data()
    user_id = data.get('final_id')
    final_summa = data.get('final_summa')

    msg = message.text

    await bot.send_message(
        chat_id=user_id,
        text=f"Sizning {final_summa} so'm chiqarishdagi harakatingiz bekor qilindi\n\nSabab: {msg}"
    )

    await bot.send_message(
        chat_id=message.chat.id,
        text="Xabar foydalanuvchiga yuborildi va to'lov harakati bekor qilindi",
        reply_markup=start_admin
    )

    await state.finish()


def contact_name(name, family_name, symbol):

    result = name + symbol + family_name

    print(result)

name = input()
last_name = input()
bond = input()

contact_name(name, last_name, bond)

# Second version

fist_name = input()
last_name = input()
delimiter = input()

print(f'{fist_name}{delimiter}{last_name}')

Data Types and Variables - Lab/01. Concat Names.py


def smallestRangeI(nums: list[int], k: int) -> int:
    max_nums = max(nums) - k
    min_nums = min(nums) + k

    result = max_nums - min_nums

    return result if result >= 0 else 0

nums = [1, 3, 6]
# nums = [10, 0]
# nums = [1]
k = 3
# k = 2
# k = 0

print(smallestRangeI(nums, k))


# File: CheckTags.py

"""
This program checks that tags are properly matched in an HTML file.
This version of the program runs in Python; the checktags version runs
directly from the command line.
"""

import html.parser
import urllib.request
import urllib.error

def CheckTags():
    """Reads a URL from the user and then checks it for tag matching."""
    url = input("URL: ")
    checkURL(url)

def checkURL(url):
    """Checks whether the tags are balanced in the specified URL."""
    try:
        response = urllib.request.urlopen(url)
        print(type(response))
        parser = HTMLTagParser()
        parser.checkTags(response.read().decode("UTF-8"))
    except urllib.error.URLError:
        print("Something went wrong")

class HTMLTagParser(html.parser.HTMLParser):

    """
    This class extends the standard HTML parser and overrides the
    callback methods used for start and end tags.
    """

    def __init__(self):
        """Creates a new HTMLTagParser object."""
        html.parser.HTMLParser.__init__(self)
        self._link_stack=[]
    def brokenLinks(self):
        for url in self._link_stack:
            try:
                response=urllib.request.urlopen(url)
                self.checkTags(response.read().decode("UTF-8"))
            except urllib.error.URLError:
                print("Link doesn't work")
    def checkTags(self, text):
        """Checks that the tags are balanced in the supplied text."""
        try:
            self._stack = [ ]
            self.feed(text)
            while len(self._stack) > 0:
                startTag,startLine = self._stack.pop()
                print("Missing </" + startTag + "> for <" + startTag +
                    "> at line " + str(startLine))
        except urllib.error.URLError:
            print("something didn't work")

    def handle_starttag(self, startTag, attributes):
        """Overrides the callback function for start tags."""
        try:
            startLine,_ = self.getpos()
            self._stack.append((startTag, startLine))
            print(attributes)
            self._link_stack = [x[1] for x in attributes if 'href' in attributes or 'src' in attributes]
        except urllib.error.URLError:
            print("nope")

    def handle_endtag(self, endTag):
        """Overrides the callback function for end tags."""
        try:
            endLine,_ = self.getpos()
            if len(self._stack) == 0:
                print("No <" + endTag + "> for </" + endTag +
                    "> at line " + str(endLine))
            else:
                while len(self._stack) > 0:
                    startTag,startLine = self._stack.pop()
                    if startTag == endTag:
                        break;
                    print("Missing </" + startTag + "> for <" + startTag +
                        "> at line " + str(startLine))
        except urllib.error.URLError:
            print("bad")
'''
I need to make this code be able to handle broken links in the html page

'''
# Startup code

if __name__ == "__main__":
    CheckTags()


from datetime import datetime
import backtrader as bt
import tushare as ts
import pandas as pd

class MyStrategy1(bt.Strategy):
    params = (('maperiod', 20),
              ('printlog', False),)

    def __init__(self):
        # 指定价格序列
        self.dataclose = self.datas[0].close
        # 初始化交易指令、买卖价格和手续费
        self.order = None
        self.buyprice = None
        self.buycomm = None
        self.process = []
        # 添加移动均线指标
        self.sma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.maperiod)

    # 策略核心，根据条件执行买卖交易指令（必选）
    def next(self):
        # 记录收盘价
        # self.log(f'收盘价, {dataclose[0]}')
        if self.order:  # 检查是否有指令等待执行,
            return
        # 检查是否持仓
        if not self.position:  # 没有持仓
            # 执行买入条件判断：收盘价格上涨突破15日均线
            if self.dataclose[0] > self.sma[0]:
                self.log('BUY CREATE, %.2f' % self.dataclose[0])
                # 执行买入
                self.order = self.buy()
        else:
            # 执行卖出条件判断：收盘价格跌破15日均线
            if self.dataclose[0] < self.sma[0]:
                self.log('SELL CREATE, %.2f' % self.dataclose[0])
                # 执行卖出
                self.order = self.sell()

    # 交易记录日志（可省略，默认不输出结果）
    def log(self, txt, dt=None, doprint=False):
        if self.params.printlog or doprint:
            dt = dt or self.datas[0].datetime.date(0)
            print(f'{dt.isoformat()},{txt}')
            self.process.append(dt.isoformat())
            self.process.append(txt)
            self.process.append('\n')

    # 记录交易执行情况（可省略，默认不输出结果）
    def notify_order(self, order):
        # 如果order为submitted/accepted,返回空
        if order.status in [order.Submitted, order.Accepted]:
            return
        # 如果order为buy/sell executed,报告价格结果
        if order.status in [order.Completed]:
            if order.isbuy():
                self.log(f'买入:\n价格:{order.executed.price},成本:{order.executed.value},手续费:{order.executed.comm}')
                self.buyprice = order.executed.price
                self.buycomm = order.executed.comm
            else:
                self.log(f'卖出:\n价格：{order.executed.price},\
                成本: {order.executed.value},\
                手续费{order.executed.comm}')
            self.bar_executed = len(self)
            # 如果指令取消/交易失败, 报告结果
        elif order.status in [order.Canceled, order.Margin, order.Rejected]:
            self.log('交易失败')
        self.order = None

    # 记录交易收益情况（可省略，默认不输出结果）
    def notify_trade(self, trade):
        if not trade.isclosed:
            return
        self.log(f'策略收益：\n毛收益:{trade.pnl:.2f}, 净收益:{trade.pnlcomm:.2f}')

    # 回测结束后输出结果（可省略，默认输出结果）
    def stop(self):
        self.log('(MA均线: %2d日) 期末总资金 %.2f' % (self.params.maperiod, self.broker.getvalue()), doprint=True)
        f = open("./static/logs.log", "w")
        f.writelines(self.process)
        f.close()

def back(stock_code, startcash):
    # 初始化cerebro回测系统设置
    cerebro = bt.Cerebro()
    # 获取数据
    # start = input('输入回测起始时间(xx-xx-xx)：')
    start = '2018-01-01'
    # end = input('输入回测结束时间(xx-xx-xx)：')
    end = '2021-06-01'

    start1 = datetime.strptime(start, "%Y-%m-%d")
    end1 = datetime.strptime(end, "%Y-%m-%d")

    df = ts.get_k_data(stock_code, autype='qfq', start=start, end=end)
    # df=ts.get_k_data('600000',autype='qfq',start='2018-01-01',end='2021-03-30')
    df.index = pd.to_datetime(df.date)
    df = df[['open', 'high', 'low', 'close', 'volume']]
    data = bt.feeds.PandasData(dataname=df, fromdate=start1, todate=end1)
    # data = bt.feeds.PandasData(dataname=df,fromdate=datetime(2018, 1, 1),todate=datetime(2021, 3, 30))
    # 加载数据
    cerebro.adddata(data)
    # 将交易策略加载到回测系统中
    # 设置printlog=True，表示打印交易日志log
    cerebro.addstrategy(MyStrategy1, maperiod=20, printlog=True)
    # 设置初始资本
    cerebro.broker.setcash(startcash)
    # 设置交易手续费为 0.1%
    cerebro.broker.setcommission(commission=0.001)
    # 设置买入设置、策略、数量
    cerebro.addsizer(bt.sizers.FixedSize, stake=1000)

    # 回测结果
    cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio')
    cerebro.addanalyzer(bt.analyzers.AnnualReturn, _name='_AnnualReturn')
    cerebro.addanalyzer(bt.analyzers.Calmar, _name='_Calmar')
    cerebro.addanalyzer(bt.analyzers.DrawDown, _name='_DrawDown')
    # cerebro.addanalyzer(bt.analyzers.TimeDrawDown, _name='_TimeDrawDown')
    cerebro.addanalyzer(bt.analyzers.GrossLeverage, _name='_GrossLeverage')
    cerebro.addanalyzer(bt.analyzers.PositionsValue, _name='_PositionsValue')
    cerebro.addanalyzer(bt.analyzers.LogReturnsRolling, _name='_LogReturnsRolling')
    cerebro.addanalyzer(bt.analyzers.PeriodStats, _name='_PeriodStats')
    cerebro.addanalyzer(bt.analyzers.Returns, _name='_Returns')
    cerebro.addanalyzer(bt.analyzers.SharpeRatio, _name='_SharpeRatio')
    # cerebro.addanalyzer(bt.analyzers.SharpeRatio_A, _name='_SharpeRatio_A')
    cerebro.addanalyzer(bt.analyzers.SQN, _name='_SQN')
    cerebro.addanalyzer(bt.analyzers.TimeReturn, _name='_TimeReturn')
    cerebro.addanalyzer(bt.analyzers.TradeAnalyzer, _name='_TradeAnalyzer')
    cerebro.addanalyzer(bt.analyzers.Transactions, _name='_Transactions')
    cerebro.addanalyzer(bt.analyzers.VWR, _name='_VWR')

    results = cerebro.run()
    # 获取最后总资金
    print(results)
    print('eeeeeeeeeeeee')
    portvalue = cerebro.broker.getvalue()
    fpnl = portvalue - startcash

    performance_dict = {}
    calmar_ratio = list(results[0].analyzers._Calmar.get_analysis().values())[-1]
    drawdown_info = results[0].analyzers._DrawDown.get_analysis()
    average_drawdown_len = drawdown_info['len']
    average_drawdown_rate = drawdown_info['drawdown']
    average_drawdown_money = drawdown_info['moneydown']
    max_drawdown_len = drawdown_info['max']['len']
    max_drawdown_rate = drawdown_info['max']['drawdown']
    max_drawdown_money = drawdown_info['max']['moneydown']
    PeriodStats_info = results[0].analyzers._PeriodStats.get_analysis()
    average_rate = PeriodStats_info['average']
    stddev_rate = PeriodStats_info['stddev']
    positive_year = PeriodStats_info['positive']
    negative_year = PeriodStats_info['negative']
    nochange_year = PeriodStats_info['nochange']
    best_year = PeriodStats_info['best']
    worst_year = PeriodStats_info['worst']
    SQN_info = results[0].analyzers._SQN.get_analysis()
    sqn_ratio = SQN_info['sqn']
    VWR_info = results[0].analyzers._VWR.get_analysis()
    vwr_ratio = VWR_info['vwr']
    sharpe_info = results[0].analyzers._SharpeRatio.get_analysis()
    sharpe_ratio = sharpe_info['sharperatio']

    performance_dict['calmar_ratio'] = calmar_ratio
    performance_dict['average_drawdown_len'] = average_drawdown_len
    performance_dict['average_drawdown_rate'] = average_drawdown_rate
    performance_dict['average_drawdown_money'] = average_drawdown_money
    performance_dict['max_drawdown_len'] = max_drawdown_len
    performance_dict['max_drawdown_rate'] = max_drawdown_rate
    performance_dict['max_drawdown_money'] = max_drawdown_money
    performance_dict['average_rate'] = average_rate
    performance_dict['stddev_rate'] = stddev_rate
    performance_dict['positive_year'] = positive_year
    performance_dict['negative_year'] = negative_year
    performance_dict['nochange_year'] = nochange_year
    performance_dict['best_year'] = best_year
    performance_dict['worst_year'] = worst_year
    performance_dict['sqn_ratio'] = sqn_ratio
    performance_dict['vwr_ratio'] = vwr_ratio
    performance_dict['sharpe_info'] = sharpe_ratio

    performance = pd.DataFrame(performance_dict, index=[0]).T
    print(performance)

    # Print out the final result
    print(f'\n总资金: {portvalue:.2f}')
    print(f'净收益: {round(fpnl, 2)}')
    ans = {}
    ans['all'] = int(portvalue)
    ans['jing'] = int(fpnl)
    # cerebro.plot(style='candlestick')
    return ans

if __name__ == '__main__':
    stock_code = input('输入回测股票代码：')
    startcash = float(input('输入回测初始资本：'))
    back(stock_code, startcash)


# https://medium.com/analytics-vidhya/computer-vision-and-deep-learning-part-2-586b6a0d3220  --- main
# https://github.com/Esri/raster-deep-learning/blob/master/docs/writing_deep_learning_python_raster_functions.md
import cv2
import numpy as np

from matplotlib import pyplot as plt
cv_image= cv2.imread("/home/jameshung/Pictures/forest01.jpg",0)
one =cv2.adaptiveThreshold(cv_image,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
ret2, two = cv2.threshold(cv_image,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
blur = cv2.GaussianBlur(cv_image,(5,5),0)
ret3,three = cv2.threshold(blur, 0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
images = [cv_image, 0, one,
          cv_image, 0, two,
          blur, 0, three]
titles = ['Original Image','Histogram','Adaptive Mean Thresholding',
          'Original Image','Histogram',"Otsu's Thresholding",
          'Gaussian filtered Image','Histogram',"Otsu's Thresholding of Gaussian Blur Image"]
for i in range(3):
    plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
    plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
    plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
    plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
    plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
    plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
plt.show()
cv2.waitKey(1000)


def getClickData(clickData):
    point = CSafePoint(clickData)
    pointData = point.getPoint()
    log.print(pointData.get('lat'))
    log.print(pointData.get('lon'))
    log.print(pointData.get('customdata'))
    log.print(pointData.get('pointIndex'))
    log.print(pointData.get('pointNumber'))
    log.print(pointData.get('curveNumber'))
    return str(pointData.getDict())

def clickState(n_clicks, clickData):
    click = screenVariables.get('myTopologyMap').getValue()
    return str(click) + ' ' + str(clickData)

def getZoom(n_intervals, figure):
    figure = CSafeFigure(figure=figure)
    return figure.getZoom()

def getClickedInfo(clickData):
    point = CSafePoint(clickData)
    pointData = point.getPoint()
    return 'Lat: ' + str(pointData.get('lat')) + ', ' + 'Lon: ' + str(pointData.get('lon')) + ', ' + 'Data: ' + pointData.get('customdata')

clicks = 0
def setPointCoordinates(n_clicks, clickData, id, lat, lon):
    myTopologyMap = screenVariables.get(getNameFromId(id))
    global clicks
    if n_clicks is None:
        n_clicks = 0
    log.print("HEEEEEEEEEEEEEEERE")
    log.print(clicks)
    log.print(n_clicks)
    if clickData is None:
        clicks = n_clicks
        return myTopologyMap.getFigure()
    point = CSafePoint(clickData)
    pointData = point.getPoint()
    setIndex = pointData.get('curveNumber')
    pointIndex = pointData.get('pointIndex')
    if n_clicks != clicks: #no click on figure, click on button
        clicks = n_clicks
        if lat is None or lon is None:
            return myTopologyMap.getFigure()
        return myTopologyMap.setNewCoordinatesForPoint(setIndex, pointIndex, lat, lon)
    else: #click on figure
        return myTopologyMap.highlight(setIndex, pointIndex)


import pygame, humans, sys, menu
from random import randint

scan_delay = 300
coll_delay = 300
move_delay = 300

menu.valmynd()
skra = open("settings/exit.txt", "r")
for line in skra:
    if line == "True":
        sys.exit()
skra.close()
settings = (open("settings/settings.txt", "r").read()).split()

fps = int(settings[2])

pygame.init()
student_list = []
for stud_num in range(200):
    student_list.append(humans.student(randint(0,800), randint(0,800), randint(1,10), stud_num))

school = pygame.display.set_mode((800, 800))
school.fill((255,255,255))
while 1:#gameplay loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()
    target =randint(0,len(student_list) - 1)
    school.fill((255, 255, 255))
    for student in student_list:
        school.blit(student.image,student.rect)
        if scan_delay >= 10:
            student.scan_surroundings(student_list)
        if move_delay >= 10:
            student.move(student.moveto(student.find_friend()))
        else:
            student.move(student.move_angle)
        student.check_collide(student_list)
    if scan_delay >= 10:
        scan_delay = 0
    else:
        scan_delay += 1
    if move_delay >= 10:
        move_delay = 0
    else:
        move_delay += 1
    if coll_delay >= 1:
        coll_delay = 0
    else:
        coll_delay += 1
    pygame.display.flip()
    pygame.time.Clock().tick(fps)


#animation ICMECAT rose scatter plot with HI CME circles and in situ data

from scipy import stats
import scipy.io
from matplotlib import cm
import sys
import os
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import sunpy.time
import time
import pickle
import seaborn as sns
import math

#for reading catalogues
def getcat(filename):
  print('reading CAT '+filename)
  cat=scipy.io.readsav(filename, verbose='true')
  print('done reading CAT')
  return cat

def getpositions(filename):
  print( 'reading positions in '+filename)
  pos=scipy.io.readsav(filename, verbose='true')
  print( 'done reading positions')
  return pos

def time_to_num_cat(time_in):

  #for time conversion from catalogue .sav to numerical time
  #this for 1-minute data or lower time resolution

  #for all catalogues
  #time_in is the time in format: 2007-11-17T07:20:00 or 2007-11-17T07:20Z
  #for times help see:
  #http://docs.sunpy.org/en/latest/guide/time.html
  #http://matplotlib.org/examples/pylab_examples/date_demo2.html

  j=0
  #time_str=np.empty(np.size(time_in),dtype='S19')
  time_str= ['' for x in range(len(time_in))]
  #=np.chararray(np.size(time_in),itemsize=19)
  time_num=np.zeros(np.size(time_in))

  for i in time_in:

   #convert from bytes (output of scipy.readsav) to string
   time_str[j]=time_in[j][0:16].decode()+':00'
   year=int(time_str[j][0:4])
   time_str[j]
   #convert time to sunpy friendly time and to matplotlibdatetime
   #only for valid times so 9999 in year is not converted
   #pdb.set_trace()
   if year < 2100:
    	  time_num[j]=mdates.date2num(sunpy.time.parse_time(time_str[j]))
   j=j+1
   #the date format in matplotlib is e.g. 735202.67569444
   #this is time in days since 0001-01-01 UTC, plus 1.

   #return time_num which is already an array and convert the list of strings to an array
  return time_num, np.array(time_str)

def decode_array(bytearrin):
 #for decoding the strings from the IDL .sav file to a list of python strings, not bytes
 #make list of python lists with arbitrary length
 bytearrout= ['' for x in range(len(bytearrin))]
 for i in range(0,len(bytearrin)-1):
  bytearrout[i]=bytearrin[i].decode()
 #has to be np array so to be used with numpy "where"
 bytearrout=np.array(bytearrout)
 return bytearrout

def IDL_time_to_num(time_in):
 #convert IDL time to matplotlib datetime
 time_num=np.zeros(np.size(time_in))
 for ii in np.arange(0,np.size(time_in)):
   time_num[ii]=mdates.date2num(sunpy.time.parse_time(time_in[ii]))

 return time_num

######################################################
#main program

plt.close('all')

sns.set_context("talk")
sns.set_style("darkgrid")
################## CONTROLS

#how much time is between frames
dayjump=0.25

#either keep or fade detections
fade=1
keep=0

#if keep is selected, the alpha for plotting each dot
keepalpha=0.7

#how long an ARRIVAL stays visible in fade mode
fadedays=30

#how big the circles are on the plot
bscale=4

#half width of the circles
lamda=30

################################

print( 'start icmecat animation program.')

#get ICMECAT
filename_icmecat='ALLCATS/HELCATS_ICMECAT_v10_SCEQ.sav'
i=getcat(filename_icmecat)

#get parameters
bmean=i.icmecat['MO_BMEAN']*bscale #bscale makes circles larger in movie
long=i.icmecat['SC_LONG_HEEQ']*np.pi/180 #heeq longitude converted to radians
rdist=i.icmecat['sc_heliodistance'] #AU
sc=i.icmecat['sc_insitu'] #string
sc=decode_array(sc)

#get indices of events in different spacecraft
vexind=np.where(sc == 'VEX')
staind=np.where(sc == 'STEREO-A')
stbind=np.where(sc == 'STEREO-B')
winind=np.where(sc == 'Wind')
mesind=np.where(sc == 'MESSENGER')
ulyind=np.where(sc == 'ULYSSES')

##################################### read in situ

print( 'read MESSENGER')
#get insitu data
mes= pickle.load( open( "DATACAT/MES_2007to2015_SCEQ_removed.p", "rb" ) )
#time conversion
#mes_time=IDL_time_to_num(mes.time)
print( 'read MESSENGER done.')

print ('read VEX')
#get insitu data
vex= pickle.load( open( "DATACAT/VEX_2007to2014_SCEQ_removed.p", "rb" ) )
#time conversion
#vex_time=IDL_time_to_num(vex.time)
print( 'read VEX done.')

print( 'read Wind')
#get insitu data
wind= pickle.load( open( "DATACAT/WIND_2007to2016_HEEQ.p", "rb" ) )
#time conversion
#wind_time=IDL_time_to_num(wind.time)
print( 'read Wind done.')

print( 'read STEREO-A')
#get insitu data
sta= pickle.load( open( "DATACAT/STA_2007to2015_SCEQ.p", "rb" ) )
#time conversion
#sta_time=IDL_time_to_num(sta.time)
print( 'read STA done.')

print( 'read STEREO-B')
#get insitu data
stb= pickle.load( open( "DATACAT/STB_2007to2014_SCEQ.p", "rb" ) )

#time conversion
#stb_time=IDL_time_to_num(stb.time)
print( 'read STB done.')

#save times
#pickle.dump([vex_time,wind_time,sta_time,stb_time,mes_time], open( "DATACAT/Insitu_times_mdates_2.p", "wb" ) )

#quicker when just reloading times
[vex_time,wind_time,sta_time,stb_time,mes_time]=pickle.load( open( "DATACAT/Insitu_times_mdates_2.p", "rb" ) )
#print 'loaded in situ times'
######################################

#get positions
pos=getpositions('DATACAT/positions_2007_2018_HEEQ_6hours.sav')
[pos_time_num,pos_time_str]=time_to_num_cat(pos.time)
#available as pos.mercury etc.

#get cme apex positions
h=getcat('ALLCATS/hicat_v3_cat_behind_visual.sav')
[h_time_num,h_time_str]=time_to_num_cat(h.all_apex_t_str)

all_apex_s=decode_array(h.all_apex_s)

#make time conversion for all icme_start_time variables
#save it as string
icme_start_time_str=i.icmecat['icme_start_time']
#save it as matplotlib date number
[icme_start_time_num,icme_start_time_str]=time_to_num_cat(icme_start_time_str)

#for each spacecraft, make a zeros array
active_icme_vex=np.zeros(np.size(icme_start_time_num))
active_icme_stb=np.zeros(np.size(icme_start_time_num))
active_icme_sta=np.zeros(np.size(icme_start_time_num))
active_icme_win=np.zeros(np.size(icme_start_time_num))
active_icme_mes=np.zeros(np.size(icme_start_time_num))
active_icme_uly=np.zeros(np.size(icme_start_time_num))

#initiate plot
plt.figure(1, figsize=(12, 6), dpi=100, facecolor='w', edgecolor='w')

#full  movie April 2014 Jan 1 until end of November 2014
frame_time_num=mdates.date2num(sunpy.time.parse_time('2007-Apr-1'))

################################### plot over all frames
for k in np.arange(12680/4,(12680+120)/4,dayjump):
#3169 is time in days

 start=time.time()
 #to current frame time, the days need to be added, so +k is done
 #save frame time as string to write on plot
 frame_time_str=str(mdates.num2date(frame_time_num+k))
 print( 'current frame_time_num+k', frame_time_str)

 #for each frame time, check active ICMEs by looking into the full catalogue:

 for m in range(0,len(icme_start_time_num)):

  #calculate difference in icme_start_time to current frame
  icme_diff_to_frame=(frame_time_num+k)-icme_start_time_num[m]

  #for all icme_start_times that are later than the current frame,
  #make them active for 30 days (fading) or infinite (keeping).

  #**********************for fading
  if fade > 0:
   if  icme_diff_to_frame > 0 and icme_diff_to_frame < fadedays:
     #check if this active icme belongs to a spacecraft
     #in1d compares to arrays; true or 1 if m is contained in vexind
      if np.in1d(m,vexind) == 1:
         active_icme_vex[m]=icme_diff_to_frame
     #same for the other spacecraft
      if np.in1d(m,stbind) == 1:
         active_icme_stb[m]=icme_diff_to_frame
      if np.in1d(m,staind) == 1:
         active_icme_sta[m]=icme_diff_to_frame
      if np.in1d(m,winind) == 1:
         active_icme_win[m]=icme_diff_to_frame
      if np.in1d(m,mesind) == 1:
         active_icme_mes[m]=icme_diff_to_frame
      if np.in1d(m,ulyind) == 1:
         active_icme_uly[m]=icme_diff_to_frame

   else:
     #if no detection, set the index to 0
     active_icme_vex[m]=0
     active_icme_stb[m]=0
     active_icme_sta[m]=0
     active_icme_win[m]=0
     active_icme_mes[m]=0
     active_icme_uly[m]=0

#************************** for keeping
  if keep > 0:
   if  icme_diff_to_frame > 0:
          #check if this active icme belongs to a spacecraft
       #in1d compares to arrays; true or 1 if m is contained in vexind
       if np.in1d(m,vexind) == 1:
           active_icme_vex[m]=icme_diff_to_frame
       #same for the other spacecraft
       if np.in1d(m,stbind) == 1:
           active_icme_stb[m]=icme_diff_to_frame
       if np.in1d(m,staind) == 1:
           active_icme_sta[m]=icme_diff_to_frame
       if np.in1d(m,winind) == 1:
           active_icme_win[m]=icme_diff_to_frame
       if np.in1d(m,mesind) == 1:
           active_icme_mes[m]=icme_diff_to_frame
   else:
      #if no detection, set the index to 0
      active_icme_vex[m]=0
      active_icme_stb[m]=0
      active_icme_sta[m]=0
      active_icme_win[m]=0
      active_icme_mes[m]=0

 #look which ICMEs are active
 active_index_vex=np.where(active_icme_vex > 0)
 active_index_stb=np.where(active_icme_stb > 0)
 active_index_sta=np.where(active_icme_sta > 0)
 active_index_win=np.where(active_icme_win > 0)
 active_index_mes=np.where(active_icme_mes > 0)
 active_index_uly=np.where(active_icme_uly > 0)

 #print 'active icme indices are:', active_index_vex

 print (' ')

 #check for active CME indices from HICAT (with the lists produced in IDL for the apex positions)
 #check where time is identical to frame time
 cmeind=np.where(h_time_num == frame_time_num+k)

 ############make plot

 #                       rows - columns, starts with 0
 ax = plt.subplot2grid((5,2), (0, 0), rowspan=5, projection='polar')
 #ax = plt.subplot(121,projection='polar')

 ######################## 1 plot all active CME circles
 #ax.scatter(h.all_apex_long[cmeind]*np.pi/180,h.all_apex_r[cmeind], s=10, c='black', alpha=1, marker='s')

 #plot all active CME circles
 #if np.size(cmeind) >0:
 for p in range(0,np.size(cmeind)):
   #print p, h.all_apex_long[cmeind[0][p]], h.all_apex_r[cmeind[0][p]]
   #central d
   dir=np.array([np.cos(h.all_apex_long[cmeind[0][p]]*np.pi/180),np.sin(h.all_apex_long[cmeind[0][p]]*np.pi/180)])*h.all_apex_r[cmeind[0][p]]

   #points on circle, correct for longitude
   circ_ang = ((np.arange(111)*2-20)*np.pi/180)-(h.all_apex_long[cmeind[0][p]]*np.pi/180)

   #these equations are from moestl and davies 2013
   xc = 0+dir[0]/(1+np.sin(lamda*np.pi/180)) + (h.all_apex_r[cmeind[0][p]]*np.sin(lamda*np.pi/180)/(1+np.sin(lamda*np.pi/180)))*np.sin(circ_ang)
   yc = 0+dir[1]/(1+np.sin(lamda*np.pi/180)) + (h.all_apex_r[cmeind[0][p]]*np.sin(lamda*np.pi/180)/(1+np.sin(lamda*np.pi/180)))*np.cos(circ_ang)
   #now convert to polar coordinates
   rcirc=np.sqrt(xc**2+yc**2)
   longcirc=np.arctan2(yc,xc)
   #plot in correct color
   if all_apex_s[cmeind[0][p]] == 'A':
    #make alpha dependent on distance to solar equatorial plane - maximum latitude is -40/+40 -
    #so to make also the -/+40 latitude CME visible, divide by 50 so alpha > 0 for these events
    ax.plot(longcirc,rcirc, c='red', alpha=1-abs(h.all_apex_lat[cmeind[0][p]]/50), lw=1.5)
   if all_apex_s[cmeind[0][p]] == 'B':
    ax.plot(longcirc,rcirc, c='royalblue', alpha=1-abs(h.all_apex_lat[cmeind[0][p]]/50), lw=1.5)

 ####################### 3 plot ICME detections
 #fader style plot alpha dependent on time difference - for this loop over each element:
 if fade >0:

  for y in range(0,np.size(active_index_vex)):
   z=active_index_vex[0][y] #access elements in tuple that is produced by where
   fadealpha=1-active_icme_vex[z]/(fadedays)  #fadedays is maximum difference in time, and alpha from 0 to 1
   ax.scatter(long[z], rdist[z], s=bmean[z], c='orange', alpha=fadealpha)

  for y in range(0,np.size(active_index_sta)):
   z=active_index_sta[0][y]
   fadealpha=1-active_icme_sta[z]/(fadedays)  #30 days is maximum difference in time, and alpha from 0 to 1
   ax.scatter(long[z], rdist[z], s=bmean[z], c='red', alpha=fadealpha)

  for y in range(0,np.size(active_index_stb)):
   z=active_index_stb[0][y]
   fadealpha=1-active_icme_stb[z]/(fadedays)  #30 days is maximum difference in time, and alpha from 0 to 1
   ax.scatter(long[z], rdist[z], s=bmean[z], c='royalblue', alpha=fadealpha)

  for y in range(0,np.size(active_index_win)):
   z=active_index_win[0][y]
   fadealpha=1-active_icme_win[z]/(fadedays)  #30 days is maximum difference in time, and alpha from 0 to 1
   ax.scatter(long[z], rdist[z], s=bmean[z], c='mediumseagreen', alpha=fadealpha)

  for y in range(0,np.size(active_index_mes)):
   z=active_index_mes[0][y]
   fadealpha=1-active_icme_mes[z]/(fadedays)  #30 days is maximum difference in time, and alpha from 0 to 1
   ax.scatter(long[z], rdist[z], s=bmean[z], c='dimgrey', alpha=fadealpha)

  for y in range(0,np.size(active_index_uly)):
   z=active_index_uly[0][y]
   fadealpha=1-active_icme_uly[z]/(fadedays)  #30 days is maximum difference in time, and alpha from 0 to 1
   ax.scatter(long[z], rdist[z], s=bmean[z], c='darkolivegreen', alpha=fadealpha)

 if keep >0:
  ax.scatter(long[active_index_vex], rdist[active_index_vex], s=bmean[active_index_vex], c='orange', alpha=keepalpha)
  ax.scatter(long[active_index_sta], rdist[active_index_sta], s=bmean[active_index_sta], c='red', alpha=keepalpha)
  ax.scatter(long[active_index_stb], rdist[active_index_stb], s=bmean[active_index_stb], c='royalblue', alpha=keepalpha)
  ax.scatter(long[active_index_win], rdist[active_index_win], s=bmean[active_index_win], c='mediumseagreen', alpha=keepalpha)
  ax.scatter(long[active_index_mes], rdist[active_index_mes], s=bmean[active_index_mes], c='dimgrey', alpha=keepalpha)

 plt.suptitle('STEREO/HI modeled CMEs (SSEF30) + in situ ICME detections and data    HELCATS - HIGEOCAT ICMECAT DATACAT', fontsize=12)

 #Sun
 ax.scatter(0,0,s=100,c='yellow',alpha=0.8, edgecolors='yellow')
 plt.figtext(0.30,0.5,'Sun', fontsize=10, ha='center')

 #Earth
 plt.figtext(0.30,0.25,'Earth', fontsize=10, ha='center')

 #units
 #plt.figtext(0.525,0.0735,'HEEQ longitude', fontsize=10, ha='left')
 #plt.figtext(0.655,0.164,'AU', fontsize=10, ha='center')

 #----------------- legend
 plt.figtext(0.05,0.02,'Mercury', color='dimgrey', ha='center', fontsize=12)
 plt.figtext(0.15,0.02,'MESSENGER', color='dimgrey', ha='center', fontsize=10)
 plt.figtext(0.25	,0.02,'Venus', color='orange', ha='center',fontsize=12)
 plt.figtext(0.35,0.02,'STEREO-A', color='red', ha='center',fontsize=12)
 plt.figtext(0.48,0.02,'STEREO-B', color='royalblue', ha='center',fontsize=12)
 plt.figtext(0.58,0.02,'Earth', color='mediumseagreen', ha='center',fontsize=12)
 plt.figtext(0.65,0.02,'Mars', color='orangered', ha='center',fontsize=10)
 plt.figtext(0.71,0.02,'MSL', color='magenta', ha='center', fontsize=10)
 plt.figtext(0.76,0.02,'Maven', color='steelblue', ha='center', fontsize=10)
 plt.figtext(0.83,0.02,'Ulysses', color='darkolivegreen', ha='center', fontsize=10)
 plt.figtext(0.90,0.02,'Rosetta', color='black', ha='center', fontsize=10)

 #add legend for bmean
 bleg=np.array([10,50,100])*bscale
 blegstr=['10 nT','50','100']

 blegr=np.zeros(len(bleg))+1.6
 blegt=np.radians(range(170,195,10))
 ax.scatter(blegt, blegr,s=bleg,c='violet', edgecolor='violet')

 for p in range(0,len(bleg)):
   ax.annotate(blegstr[p],xy=(blegt[p],blegr[p]-0.2), ha='center', va='center', fontsize=8)

 ############################## plot positions
 #check which index is closest in positions to current time
 #frame_time_num+k vs. pos_time_num

 timeind=np.where(frame_time_num+k-pos_time_num == min(abs((frame_time_num+k)-pos_time_num)))

 #index 1 is longitude, 0 is rdist
 ax.scatter(pos.venus[1,timeind], pos.venus[0,timeind], s=50, c='orange', alpha=1, lw=0)
 ax.scatter(pos.mercury[1,timeind], pos.mercury[0,timeind], s=50, c='dimgrey', alpha=1,lw=0)
 ax.scatter(pos.messenger[1,timeind], pos.messenger[0,timeind], s=25, c='dimgrey', alpha=1,lw=0,marker='s')
 ax.scatter(pos.sta[1,timeind], pos.sta[0,timeind], s=25, c='red', alpha=1,lw=0, marker='s')
 ax.scatter(pos.stb[1,timeind], pos.stb[0,timeind], s=25, c='royalblue', alpha=1,lw=0, marker='s')
 ax.scatter(pos.earth[1,timeind], pos.earth[0,timeind], s=50, c='mediumseagreen', alpha=1,lw=0)
 ax.scatter(pos.mars[1,timeind], pos.mars[0,timeind], s=50, c='orangered', alpha=1,lw=0)
 ax.scatter(pos.ulysses[1,timeind], pos.ulysses[0,timeind], s=25, c='darkolivegreen', alpha=1,lw=0,marker='s')
 ax.scatter(pos.msl[1,timeind], pos.msl[0,timeind], s=25, c='magenta', alpha=1,lw=0,marker='s')
 ax.scatter(pos.maven[1,timeind], pos.maven[0,timeind], s=25, c='steelblue', alpha=1,lw=0, marker='s')
 ax.scatter(pos.rosetta[1,timeind], pos.rosetta[0,timeind], s=25, c='black', alpha=1,lw=0, marker='s')

 #set axes
 plt.thetagrids(range(0,360,45),(u'0\u00b0 HEEQ longitude',u'45\u00b0',u'90\u00b0',u'135\u00b0',u'+/- 180\u00b0',u'-135\u00b0',u'-90\u00b0',u'-45\u00b0'), fmt='%d', frac = 1.05,fontsize=10)
 ax.set_theta_zero_location('S')
 ax.set_ylim(0, 1.8)
 plt.rgrids((0.4,0.7,1.0,1.3,1.6),('0.4','0.7','1.0','1.3','1.6 AU'),fontsize=10)

 #plot text for date extra so it does not move
 #year
 plt.figtext(0.47-0.22,0.9,frame_time_str[0:4], fontsize=13, ha='center')
 #month
 plt.figtext(0.51-0.22,0.9,frame_time_str[5:7], fontsize=13, ha='center')
 #day
 plt.figtext(0.54-0.22,0.9,frame_time_str[8:10], fontsize=13, ha='center')
 #hours
 plt.figtext(0.57-0.22,0.9,frame_time_str[11:13], fontsize=13, ha='center')

 #mysignature
 plt.figtext(0.96,0.01,r'$C. M\ddot{o}stl$', fontsize=7, ha='center')

 ############# 5 in situ data plots

 plotstartdate=mdates.num2date(frame_time_num+k-3)
 plotenddate=mdates.num2date(frame_time_num+k+3)

 #slicing

 #take only those indices where the difference to frame_time_num+k is less than 3
 mes_ind_plot=np.where(abs(mes_time-(frame_time_num+k)) < 3)
 vex_ind_plot=np.where(abs(vex_time-(frame_time_num+k)) < 3)
 stb_ind_plot=np.where(abs(stb_time-(frame_time_num+k)) < 3)
 sta_ind_plot=np.where(abs(sta_time-(frame_time_num+k)) < 3)
 wind_ind_plot=np.where(abs(wind_time-(frame_time_num+k)) < 3)

 #rows - columns
 #MESSENGER
 ax2 = plt.subplot2grid((5,2), (0, 1))
 ax2.plot_date(mes_time[mes_ind_plot],mes.btot[mes_ind_plot],'-k', lw=0.3)
 ax2.plot_date(mes_time[mes_ind_plot],mes.bx[mes_ind_plot], '-r',lw=0.3)
 ax2.plot_date(mes_time[mes_ind_plot],mes.by[mes_ind_plot],'-g',lw=0.3)
 ax2.plot_date(mes_time[mes_ind_plot],mes.bz[mes_ind_plot],'-b',lw=0.3)
 plt.tick_params( axis='x', labelbottom='off')
 #current time
 plt.yticks(fontsize=9)
 plt.ylabel('B SCEQ [nT]', fontsize=9)
 ax2.plot_date([mdates.num2date(frame_time_num+k),mdates.num2date(frame_time_num+k)], [-120,120],'-k', lw=0.5, alpha=0.8)
 plt.xlim((plotstartdate, plotenddate))
 plt.ylim((-120, 120))

 #VEX
 ax3 = plt.subplot2grid((5,2), (1, 1))
 ax3.plot_date(vex_time[vex_ind_plot],vex.btot[vex_ind_plot],'-k', lw=0.3)
 ax3.plot_date(vex_time[vex_ind_plot],vex.bx[vex_ind_plot], '-r',lw=0.3)
 ax3.plot_date(vex_time[vex_ind_plot],vex.by[vex_ind_plot],'-g',lw=0.3)
 ax3.plot_date(vex_time[vex_ind_plot],vex.bz[vex_ind_plot],'-b',lw=0.3)
 plt.tick_params( axis='x', labelbottom='off')
 plt.yticks(fontsize=9)
 plt.ylabel('B SCEQ [nT]', fontsize=9)
 ax3.plot_date([mdates.num2date(frame_time_num+k),mdates.num2date(frame_time_num+k)], [-50,50],'-k', lw=0.5, alpha=0.8)
 plt.xlim((plotstartdate, plotenddate))
 plt.ylim((-50, 50))

 #Earth
 ax4 = plt.subplot2grid((5,2), (2, 1))
 ax4.plot_date(wind_time[wind_ind_plot],wind.btot[wind_ind_plot],'-k', lw=0.3)
 ax4.plot_date(wind_time[wind_ind_plot],wind.bx[wind_ind_plot], '-r',lw=0.3)
 ax4.plot_date(wind_time[wind_ind_plot],wind.by[wind_ind_plot],'-g',lw=0.3)
 ax4.plot_date(wind_time[wind_ind_plot],wind.bz[wind_ind_plot],'-b',lw=0.3)
 plt.tick_params( axis='x', labelbottom='off')
 plt.yticks(fontsize=9)
 plt.ylabel('B SCEQ [nT]', fontsize=9)
 ax4.plot_date([mdates.num2date(frame_time_num+k),mdates.num2date(frame_time_num+k)], [-50,50],'-k', lw=0.5, alpha=0.8)
 plt.xlim((plotstartdate, plotenddate))
 plt.ylim((-35, 35))

 #STA
 ax5 = plt.subplot2grid((5,2), (3, 1))
 ax5.plot_date(sta_time[sta_ind_plot],sta.btot[sta_ind_plot],'-k', lw=0.3)
 ax5.plot_date(sta_time[sta_ind_plot],sta.bx[sta_ind_plot], '-r',lw=0.3)
 ax5.plot_date(sta_time[sta_ind_plot],sta.by[sta_ind_plot],'-g',lw=0.3)
 ax5.plot_date(sta_time[sta_ind_plot],sta.bz[sta_ind_plot],'-b',lw=0.3)
 plt.tick_params( axis='x', labelbottom='off')
 plt.yticks(fontsize=9)
 plt.ylabel('B SCEQ [nT]', fontsize=9)
 ax5.plot_date([mdates.num2date(frame_time_num+k),mdates.num2date(frame_time_num+k)], [-50,50],'-k', lw=0.5, alpha=0.8)
 plt.xlim((plotstartdate, plotenddate))
 plt.ylim((-35, 35))

 #STB
 ax6 = plt.subplot2grid((5,2), (4, 1))
 ax6.plot_date(stb_time[stb_ind_plot],stb.btot[stb_ind_plot],'-k', lw=0.3)
 ax6.plot_date(stb_time[stb_ind_plot],stb.bx[stb_ind_plot], '-r',lw=0.3)
 ax6.plot_date(stb_time[stb_ind_plot],stb.by[stb_ind_plot],'-g',lw=0.3)
 ax6.plot_date(stb_time[stb_ind_plot],stb.bz[stb_ind_plot],'-b',lw=0.3)
 plt.xlim((plotstartdate, plotenddate))

 myformat = mdates.DateFormatter('%m-%d')
 ax6.xaxis.set_major_formatter(myformat)
 plt.yticks(fontsize=9)
 plt.ylabel('B SCEQ [nT]', fontsize=9)
 ax6.plot_date([mdates.num2date(frame_time_num+k),mdates.num2date(frame_time_num+k)], [-50,50],'-k', lw=0.5, alpha=0.8)
 plt.ylim((-35, 35))
 plt.xticks(fontsize=10)

 #labeling of spacecraft and longitude in HEEQ
 plt.figtext(0.92,0.82,'MESSENGER',color='dimgrey', fontsize=10, ha='left')
 plt.figtext(0.94,0.77,"%d" % (pos.messenger[1,timeind]*180/np.pi),color='black', fontsize=10, ha='right')

 plt.figtext(0.92,0.82-0.165,'VEX',color='orange', fontsize=10, ha='left')
 plt.figtext(0.94,0.77-0.165,"%d" % (pos.venus[1,timeind]*180/np.pi),color='black', fontsize=10, ha='right')

 plt.figtext(0.92,0.82-0.165*2,'Wind',color='mediumseagreen', fontsize=10, ha='left')
 plt.figtext(0.94,0.77-0.165*2,"%d" % (pos.earth[1,timeind]*180/np.pi),color='black', fontsize=10, ha='right')

 plt.figtext(0.92,0.82-0.165*3,'STEREO-A',color='red', fontsize=10, ha='left')
 plt.figtext(0.94,0.77-0.165*3,"%d" % (pos.sta[1,timeind]*180/np.pi),color='black', fontsize=10, ha='right')

 plt.figtext(0.92,0.82-0.165*4,'STEREO-B',color='royalblue', fontsize=10, ha='left')
 plt.figtext(0.94,0.77-0.165*4,"%d" % (pos.stb[1,timeind]*180/np.pi),color='black', fontsize=10, ha='right')

 #labeling in situ components
 plt.figtext(0.75,0.92,'Bx',color='red', fontsize=10, ha='left')
 plt.figtext(0.8,0.92,'By',color='green', fontsize=10, ha='left')
 plt.figtext(0.85,0.92,'Bz',color='blue', fontsize=10, ha='left')

 #save figure for frame - this starts with zero at the start time
 framestr = '%04i' % (k*4)
 #framenr=framenr+1
 print( 'frame nr.', framestr)
 #plt.show()
 if fade >0:
  plt.savefig('animations/animation_icmecat_6hour_fade_circ_insitu_final_full/icmecat_'+framestr+'.png',  dpi=300)
  #plt.savefig('animations/animation_icmecat_6hour_fade_circ_insitu_final_full/icmecat_'+framestr+'.jpg',  dpi=300)

# if keep >0:
#  plt.savefig('animations/animation_icmecat_6hour_keep_circ_insitu_final_full/icmecat_'+framestr+'.jpg', format='jpg', dpi=300)

 end=time.time()
 print( 'took time in seconds:', (end-start) ,'for this frame')

 #clears plot window
 plt.clf()

############end of cycle

#make animation convert with automator into jpg before

#os.system('/Users/chris/movie/ffmpeg -r 15 -i /Users/chris/python/catpy/animations/animation_icmecat_6hour_fade_circ_insitu_final_full_jpg/icmecat_%04d.jpg -b 5000k -r 15 animations/icmecat_anim_6hour_fade_circ_insitu_final_full.mp4 -y')

print( 'made movie')

print( 'end icmecat animation program.')

#/Users/chris/movie/ffmpeg -r 15 -i /Users/chris/python/catpy/animations/animation_icmecat_6hour_fade_circ_insitu_all/icmecat_%04d.jpg -b 5000k -r 15 animations/icmecat_anim_6hour_fade_circ_insitu_all.mp4 -y


# /usr/bin/python3.6
# -*- coding:utf-8 -*-

class Solution(object):
    def alphabetBoardPath(self, target):
        """
        :type target: str
        :rtype: str
        """
        board = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]
        m = {}
        for i, row in enumerate(board):
            for j, c in enumerate(row):
                m[c] = (i, j)

        x, y = 0, 0
        ret = ""
        for c in target:
            xx, yy = m[c]
            if (x, y) == (xx, yy):
                ret += "!"
            else:
                dx = xx - x
                dy = yy - y

                abs_dx = abs(dx)
                abs_dy = abs(dy)
                if dx >= 0 and dy >= 0:
                    ret += "R"*abs_dy+"D"*abs_dx
                elif dx >= 0 and dy <= 0:
                    ret += "L"*abs_dy+"D"*abs_dx
                elif dx <= 0 and dy <= 0:
                    ret += "L"*abs_dy+"U"*abs_dx
                else:
                    ret += "U"*abs_dx+"R"*abs_dy
                ret += "!"
            x, y = xx, yy
        return ret

def main():
    s = Solution()
    print(s.alphabetBoardPath("leet"))
    print(s.alphabetBoardPath("code"))

if __name__ == "__main__":
    main()


import requests
from bs4 import BeautifulSoup
import wikipedia

class unotes:
    def __init__(self,*args):
        self.data_list = [a.replace(' ','_')for a in args]
        self.content = {}
        self.links = {}

    def __str__(self):
        return f"unotes for {self.data_list}"

    def search(self):
        contents = []
        wiki_content = []
        sub_results = []
        data = self.data_list
        for i in range(len(data)):
            searches = wikipedia.search(data[i])
            # print(searches)
            try:
                main_result = wikipedia.summary(searches[0],auto_suggest=False)
            except:
                main_result = f"Some errors. Please consider changing the parameters for {data[i]}. :) "
            self.content[f'{data[i]}'] = main_result
            req = requests.get(f"https://www.google.com/search?q={str(data[i].replace('_','+'))}")
            content = BeautifulSoup(req.text,features='html.parser')
            x = content.find_all('a')
            links = []
            for j in x:
                link = j.attrs['href']
                if 'url' in link.split("?")[0]:
                    url_ = link.split("?")[1].split("&")[0].split("=")[1]
                    if "%" not in url_:
                        links.append(url_)
            self.links[data[i]] = links
            temp_ = {'name':f'{data[i]}','content':str(main_result),'links':links}
            contents.append(temp_)
        return contents

    def save(self,file_name,data_obj):
        html_upper = """
<!DOCTYPE html>

    <title>Unotes</title>

U-notes

        """
        html_lower = f""
        file =  open(f'{str(file_name)}','w+',encoding='utf-8')
        file.write(str(html_upper))
        for i in data_obj:
            name = i['name']
            content = i['content']
            links = i['links']
            show_function_ = f""
            content_ = f"
{name}

{content}

<button type='button' class='show-text' onclick='show_{name}();'>See links ^_^</button>
"
            file.write(str(show_function_))
            file.write(str(content_))
            for link in links:
                link_ = f"
{link}
"
                file.write(str(link_))
            content_end = "
"
            file.write(str(content_end))
        file.write(str(html_lower))
        file.close()

def main():
    note = unotes('param1','param2')
    chapters = note.search()
    note.save('file_name.html',chapters)

if __name__=='__main__':
    main()


import numpy as np
import cv2
from imgutil import read_img
from scipy.optimize import minimize
from mathutil import Rx, Ry, Rz

in_size = (1080//2, 1920//2)
fov_factor = 1
marks = [
    (1, 2, [((925, 1080//2 - 338), (1131 - 1920//2, 1080//2 - 383)),
            ((946, 1080//2 - 321), (1156 - 1920//2, 1080//2 - 375)),
            ((834, 1080//2 - 390), (1036 - 1920//2, 1080//2 - 398)),
            ((952, 1080//2 - 372), (1154 - 1920//2, 1080//2 - 428)),
            ((808, 1080//2 - 367), (1014 - 1920//2, 1080//2 - 364))]),
    (5, 1, [((891, 1080//2 - 450), (1007 - 1920//2, 1080//2 - 407)),
            ((950, 1080//2 - 331), (1075 - 1920//2, 1080//2 - 313)),
            ((842, 1080//2 - 380), (970 - 1920//2, 1080//2 - 316))])
]
camid_to_param_offset = {2: 0, 5: 3}

def to_vec(px, py):
    fov = fov_factor*np.array([1, in_size[0]/in_size[1]])
    x = fov[0]*(2*px/in_size[1] - 1)
    y = fov[1]*(2*py/in_size[0] - 1)
    vec = np.array([x, y, 1])
    return vec/np.linalg.norm(vec)

def gen_matrix(camid, params):
    if camid == 1:
        return Rx(np.pi/2)
    else:
        off = camid_to_param_offset[camid]
        return np.matmul(Rx(params[off + 2]), np.matmul(Ry(params[off + 1]), Rz(params[off])))

def calc_chi2(params):
    chi2 = 0
    for a, b, marker in marks:
        Ma = gen_matrix(a, params)
        Mb = gen_matrix(b, params)
        for m1, m2 in marker:
            diff = np.matmul(Ma, to_vec(*m1)) - np.matmul(Mb, to_vec(*m2))
            chi2 += np.sum(diff**2)
    return chi2

def show_markers():
    for a, b, marker in marks:
        a = read_img(a, in_size)
        b = read_img(b, in_size)
        for m1, m2 in marker:
            cv2.circle(a, m1, 5, (1, 0, 0))
            cv2.circle(b, m2, 5, (1, 0, 0))
        cv2.imshow("f", np.flip(np.hstack([a, b]), axis=0))
        while cv2.waitKey(0) != ord("q"):
            pass
        cv2.destroyAllWindows()

show_markers()
res = minimize(calc_chi2, x0=[0]*3*len(marks))
print(repr(gen_matrix(2, res.x)))
print(repr(gen_matrix(5, res.x)))


#! python3
# sendDuesReminders.py - sends emails based on payment status in spreadsheet

import smtplib
import openpyxl

wb = openpyxl.load_workbook('C:\\Users\\daize\\Desktop\\pythontest\\Automate\\duesRecords.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')

lastCol = sheet.max_column
latestMonth = sheet.cell(row=1, column=lastCol).value

unpaidMembers = {}
for r in range(2, sheet.max_row+1):
    payment = sheet.cell(row=r, column=lastCol).value
    if payment != 'paid':
        name = sheet.cell(row=r, column=1).value
        email = sheet.cell(row=r, column=2).value
        unpaidMembers[name] = email

smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()      #TLS加密的连接需要此代码进行加密，SSL不需要
smtpObj.login('account', 'passwd')

for name, email in unpaidMembers:
    body = "Suject: %s dues unpaid.\nDear %s...not paid dues for %s....Thanks" % (latestMonth, name, latestMonth)
    print('Sending email to %s' % email)
    sendmailStatus = smtpObj.sendmail('my account', email, body)        #所有收件人都发送成功，会返回空

    if sendmailStatus != {}:
        print('problem send email to %s:%s' % (email, sendmailStatus))
smtpObj.quit()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Created on Fri Mar 10 12:54:45 2023
"""
import numpy as np
from pyGITR.math_helper import *
from typing import Callable
import matplotlib.pyplot as plt
import pydoc
import netCDF4
import os

def Gaussian(x: np.ndarray = np.linspace(-15000, 15000, 100000), sigma: float = 5.0, mu: float = 120.0, beta: float = 1.0, Normalized=True):
    f = (1/(sigma*np.sqrt(2*np.pi)))*np.exp(-((x-mu)/sigma)**2)

    f[np.argwhere(x<0)] = 0

    if Normalized:
        f = f/Integrale(f, x, Array=False)
    return f

def Gaussian_test(x: np.ndarray = np.linspace(-15000, 15000, 100000), sigma: float = 20.0, mu: float = 130.0, beta: float = 1.0, Normalized=True):
    f = (1/(sigma*np.sqrt(2*np.pi)))*np.exp(-((x-mu)/sigma)**2)

    f[np.argwhere(x<0)] = 0

    # if Normalized:
    #     f = f/Integrale(f, x, Array=False)
    return f

def Gaussian_Jerome(x: np.ndarray = np.linspace(-15000, 15000, 100000), sigma: float = 5.0, mu: float = 120, beta: float = 0.0, Normalized=True):
    f = np.abs(x)**beta*np.exp(-1.0/2.0*((x-mu)/sigma)**2)
    if beta > 0:
        f[np.argwhere(x<0)] = 0
    if Normalized:
        f = f/Integrale(f, x, Array=False)
    return f

def Thomson(x: np.ndarray = np.linspace(0, 300, 10000), xb: float = 8.64, xc: float = 100, Normalized=True):
    assert not (xc <= xb), "xc cannot be <= xb"
    f = x/(x + xb) ** 3*(1.0-np.sqrt((x+xb)/(xc+xb)))
    f[np.argwhere(x > xc)] = 0.0
    # if Normalized:
    #     f = f/Integrale(f, x, Array=False)
    return f

def Levy(x=np.linspace(0.1,10,10000), c=1, mu=0):
    return np.sqrt(c/2/np.pi)*np.exp(-c/(x-mu))/((x-mu)**1.5)

x = np.linspace(1, 200, 5005)

pdf_p = Gaussian_Jerome(x)
pdf_q = Gaussian_test(x)

pdf_p_times_q = np.multiply(pdf_p,pdf_q)

#Normalization = 1.0
Normalization = Integrale(pdf_p_times_q, x, Array=False)
#Normalization = 1.0

pdf_p_times_q = np.divide(pdf_p_times_q,Normalization)

pdf_p_times_q_divide_q = np.divide(pdf_p_times_q,pdf_q)

#Normalization = Integrale(pdf_p_times_q_divide_q, x, Array=False)
#Normalization = 1.0
#print(Normalization)

pdf_p_times_q_divide_q = np.divide(pdf_p_times_q_divide_q,Normalization)

# #print(pdf_q)
plt.figure()
plt.plot(x,pdf_p,label="p distribution")
plt.plot(x,pdf_q,label="q distribution")
#plt.plot(x,pdf_p_times_q,label="multiplied distribution")
#plt.plot(x,Integrale(np.multiply(pdf_p,1.0), x, Array=True),label="cumulative of p times q")
#plt.plot(x,pdf_p_times_q_divide_q,label="multiplied distribution")
plt.xlim(100,150)
plt.legend()
plt.show()

# Gaussian(np.array([x]))
# Gaussian_Jerome(np.array([x]))


#
class rest_get_action_queue(rest_get_table_handler):
    def __init__(self):
        desc = [
          "List service and node actions posted in the action_queue.",
        ]
        examples = [
          "# curl -u %(email)s -o- https://%(collector)s/init/rest/api/actions?query=status=R",
        ]

        q = q_filter(node_field=db.action_queue.node_id)

        rest_get_table_handler.__init__(
          self,
          path="/actions",
          tables=["action_queue"],
          q=q,
          desc=desc,
          examples=examples,
        )

#
class rest_post_action_queue(rest_post_handler):
    def __init__(self):
        desc = [
          "Modify action queue entries",
        ]
        examples = [
          """# curl -u %(email)s -X POST --header 'Content-Type: application/json' -d @/tmp/list.json -o- https://%(collector)s/init/rest/api/actions"""
        ]
        rest_post_handler.__init__(
          self,
          path="/actions",
          tables=["action_queue"],
          desc=desc,
          examples=examples
        )

    def handler(self, **vars):
        if 'id' not in vars:
           raise HTTP(400, "The 'id' key must be specified")
        id = vars["id"]
        del(vars["id"])
        return rest_post_action_queue_one().handler(id, **vars)

#
class rest_put_action_queue(rest_put_handler):
    def __init__(self):
        desc = [
          "Enqueue an action that will be executed by opensvc agents.",
          "The user must be responsible for the target node or service.",
          "The action is logged in the collector's log.",
        ]
        data = """
- <property>=<value> pairs.
- **node_id**
. The node targeted by the action. If svc_id is not specified, the
  action is run using the nodemgr opensvc agent command
- **svc_id**
. The service targeted by the action. The action is run using the
  svcmgr opensvc agent command on the node specified by **node_id**.
- **action**
. The opensvc agent action to execute.
- **module**
. The compliance module to run **action** on.
- **moduleset**
. The compliance moduleset to run **action** on.
- **rid**
. The service resource id to limit **action** to.

Each action has specific property requirements:

- ``giveback``:green requires **svc_id**
- ``start``:green requires **svc_id**, optionally **node_id**, **rid**
- ``stop``:green requires **svc_id**, optionally **node_id**, **rid**
- ``freeze``:green requires **svc_id**, optionally **node_id**, **rid**
- ``thaw``:green requires **svc_id**, optionally **node_id**, **rid**
- ``compliance_check``:green requires **node_id**, **module** or **moduleset**, optionally
  **svc_id**
- ``compliance_fix``:green requires **node_id**, **module** or **moduleset**, optionally
  **svc_id**
- ``restart``:green requires **node_id**, **svc_id**, optionally **rid**
- ``syncall``:green requires **node_id**, **svc_id**, optionally **rid**
- ``syncnodes``:green requires **node_id**, **svc_id**, optionally **rid**
- ``syncdrp``:green requires **node_id**, **svc_id**, optionally **rid**
- ``enable``:green requires **node_id**, **svc_id**, optionally **rid**
- ``disable``:green requires **node_id**, **svc_id**, optionally **rid**
- ``pushasset``:green requires **node_id**
- ``pushdisks``:green requires **node_id**
- ``pull``:green requires **node_id**
- ``push``:green requires **node_id**
- ``pushpkg``:green requires **node_id**
- ``pushpatch``:green requires **node_id**
- ``pushstats``:green requires **node_id**
- ``checks``:green requires **node_id**
- ``sysreport``:green requires **node_id**
- ``updatecomp``:green requires **node_id**
- ``updatepkg``:green requires **node_id**
- ``rotate_root_pw``:green requires **node_id**
- ``scanscsi``:green requires **node_id**
- ``reboot``:green requires **node_id**
- ``schedule_reboot``:green requires **node_id**
- ``unschedule_reboot``:green requires **node_id**
- ``shutdown``:green requires **node_id**
- ``wol``:green requires **node_id**
"""
        examples = [
          "# curl -u %(email)s -o- -X PUT -d node_id=5c977731-0562-11e6-8c70-7e9e6cf13c8a -d action=pushasset https://%(collector)s/init/rest/api/actions",
        ]

        rest_put_handler.__init__(
          self,
          path="/actions",
          desc=desc,
          data=data,
          examples=examples
        )

    def handler(self, **vars):
        action_id = json_action_one(vars)
        if action_id > 0:
            action_q_event()
        else:
            raise HTTP(500, "Failed to enqueue action")
        return rest_get_action_queue_one().handler(action_id)

#
class rest_get_action_queue_stats(rest_get_handler):
    def __init__(self):
        desc = [
          "Display action queue statistics",
        ]
        examples = [
          "# curl -u %(email)s -o- https://%(collector)s/init/rest/api/actions/stats",
        ]

        rest_get_handler.__init__(
          self,
          path="/actions/stats",
          desc=desc,
          examples=examples,
        )

    def handler(self, **vars):
        return dict(data=action_queue_ws_data())

#
class rest_get_action_queue_one(rest_get_line_handler):
    def __init__(self):
        desc = [
          "Display properties of a specific action posted in the action queue.",
        ]
        examples = [
          "# curl -u %(email)s -o- https://%(collector)s/init/rest/api/actions/10",
        ]

        rest_get_line_handler.__init__(
          self,
          path="/actions/<id>",
          tables=["action_queue"],
          desc=desc,
          examples=examples,
        )

    def handler(self, id, **vars):
        q = db.action_queue.id == int(id)
        q = q_filter(q, node_field=db.action_queue.node_id)
        self.set_q(q)
        return self.prepare_data(**vars)

#
class rest_delete_action_queue_one(rest_delete_handler):
    def __init__(self):
        desc = [
          "Delete an action posted in the action queue.",
        ]
        examples = [
          "# curl -u %(email)s -o- -X DELETE https://%(collector)s/init/rest/api/actions/10",
        ]

        rest_delete_handler.__init__(
          self,
          path="/actions/<id>",
          tables=["action_queue"],
          desc=desc,
          examples=examples,
        )

    def handler(self, id, **vars):
        check_privilege("NodeManager")
        q = db.action_queue.id == int(id)
        q = q_filter(q, node_field=db.action_queue.node_id)
        row = db(q).select().first()
        if row is None:
            return dict(info="Action %s does not exist in action queue" % id)
        try:
            node_responsible(node_id=row.node_id)
        except HTTP as exc:
            # accept deleting orphaned action
            if exc.status != 404:
                raise
        db(q).delete()
        _log('action_queue.delete',
             'deleted actions %(u)s',
             dict(u=row.command),
             node_id=row.node_id)
        ws_send('action_queue')
        action_q_event()
        return dict(info="Action %s deleted" % id)

#
class rest_post_action_queue_one(rest_post_handler):
    def __init__(self):
        desc = [
          "Modify properties of an action posted in the action queue.",
          "The user must be responsible for the node.",
          "The user must be in the NodeExec or CompExec privilege group.",
          "The modification is logged in the collector's log.",
          "A websocket event is sent to announce the change in the table.",
        ]
        data = """
- <property>=<value> pairs.
- Available properties are: ``%(props)s``:green.
""" % dict(props="status")
        examples = [
          '# curl -u %(email)s -o- -X POST -d status="C" https://%(collector)s/init/rest/api/actions/10',
        ]

        rest_post_handler.__init__(
          self,
          path="/actions/<id>",
          desc=desc,
          data=data,
          tables=["action_queue"],
          examples=examples,
        )

    def handler(self, _id, **vars):
        check_privilege(["NodeExec", "CompExec"])
        q = db.action_queue.id == int(_id)
        q = q_filter(q, node_field=db.action_queue.node_id)
        row = db(q).select().first()
        if row is None:
            return dict(error="Action %s does not exist in action queue" % _id)
        node_responsible(node_id=row.node_id)
        if vars.keys() != ["status"]:
            invalid = ', '.join(sorted(set(vars.keys())-set(["status"])))
            return dict(error="Permission denied: properties not updateable: %(props)s" % dict(props=invalid))
        if row.status == 'T' and vars.get("status") == "C":
            return dict(error="Can not cancel action %d in %s state" % (row.id, row.status))
        if row.status in ('R', 'W') and vars.get("status") == "W":
            return dict(error="Can not redo action %d in %s state" % (row.id, row.status))
        db(q).update(**vars)
        _log('action_queue.update',
             'update properties %(data)s',
             dict(data=beautify_change(row, vars)),
             svc_id=row.svc_id,
             node_id=row.node_id)
        ws_send('action_queue')
        action_q_event()
        return rest_get_action_queue_one().handler(_id)


import os
import sys
from pathlib import Path
from typing import Optional
import tempfile
import time
import queue
import subprocess
import threading

class IPythonInterpreter:
    _END_MESSAGE = "__ INTERPRETER END OF EXECUTION __"
    _INTERPRETER_PROMPT = ">>> "
    _LAST_VAR = "_INTERPRETER_last_val"

    def __init__(
        self,
        *,
        working_dir: Path = None,
        ipython_path: Path = None,
        timeout: int = None,
        deactivate_venv: bool = False,
    ):
        self._working_dir = working_dir
        if ipython_path is None:
            self._ipython_path = Path(sys.executable).parent / "ipython.exe"
        else:
            self._ipython_path = ipython_path
        self._timeout = timeout
        self._deactivate_venv = deactivate_venv
        self._running = False
        self._start()

    def __del__(self):
        self.stop()

    def _get_env(self):
        env = os.environ.copy()
        if self._deactivate_venv:
            if "VIRTUAL_ENV" in env:
                del env["VIRTUAL_ENV"]
            if "_OLD_VIRTUAL_PROMPT" in env:
                env["PROMPT"] = "_OLD_VIRTUAL_PROMPT"
                del env["_OLD_VIRTUAL_PROMPT"]
            if "_OLD_VIRTUAL_PYTHONHOME" in env:
                env["PYTHONHOME"] = "_OLD_VIRTUAL_PYTHONHOME"
                del env["_OLD_VIRTUAL_PYTHONHOME"]
            if "_OLD_VIRTUAL_PATH" in env:
                env["PATH"] = "_OLD_VIRTUAL_PATH"
                del env["_OLD_VIRTUAL_PATH"]
        return env

    def _start(self):
        env = self._get_env()
        self._process = subprocess.Popen(
            [str(self._ipython_path), "--classic"],
            text=True,
            cwd=self._working_dir,
            env=env,
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )
        self._p_stdin = self._process.stdin

        self._stop_threads = False

        self._q_stdout = queue.Queue()
        self._t_stdout = threading.Thread(
            target=self._reader_thread,
            args=(self._process.stdout, self._q_stdout),
            daemon=True,
        )
        self._t_stdout.start()

        self._q_stderr = queue.Queue()
        self._t_stderr = threading.Thread(
            target=self._reader_thread,
            args=(self._process.stderr, self._q_stderr),
            daemon=True,
        )
        self._t_stderr.start()

        self._wait_till_started()
        self._running = True

    def stop(self):
        if self._running:
            self._process.kill()
            self._stop_threads = True
            self._t_stdout.join()
            self._t_stderr.join()
            self._running = False

    def _reader_thread(self, pipe, q):
        while not self._stop_threads:
            q.put(pipe.readline())

    def _read_stdout(self, timeout: Optional[int]) -> Optional[str]:
        start = time.time()
        stdout = ""
        while True:
            try:
                line = self._q_stdout.get(timeout=timeout)
            except queue.Empty:
                line = None
            if timeout is not None and time.time() - start > timeout:
                line = None

            if line is None:
                return None

            if self._END_MESSAGE in line:
                break
            stdout += line

        return stdout[len(self._INTERPRETER_PROMPT) :]

    def _read_stderr(self) -> str:
        stderr = ""
        while not self._q_stderr.empty():
            stderr += self._q_stderr.get()
        return stderr

    def _write_stdin(self, text: str):
        self._p_stdin.write(text)
        self._p_stdin.flush()

    def _wait_till_started(self):
        self._write_stdin(f"'{self._END_MESSAGE}'\n")
        self._read_stdout(timeout=10)

    def _create_script(self, script: str) -> Path:
        lines = script.splitlines()
        if len(lines) > 0:
            is_eval = True
            try:
                compile(lines[-1], "<stdin>", "eval")
            except SyntaxError:
                is_eval = False
            if is_eval:
                lines[-1] = f"{self._LAST_VAR} = ({lines[-1]})"
                lines.append(f"if {self._LAST_VAR} is not None:")
                lines.append(f"    print({self._LAST_VAR})")

        script = "\n".join(lines) + "\n"
        with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as f:
            f.write(script)

        return Path(f.name)

    def _run_script(self, script_path: Path):
        self._write_stdin(f"%run -i {script_path}\n'{self._END_MESSAGE}'\n")

    def _fetch_result(self) -> Optional[str]:
        stdout = self._read_stdout(timeout=self._timeout)
        if stdout is None:
            self.stop()
            self._start()
            return None

        stderr = self._read_stderr()
        return stdout + stderr

    def run_cell(self, script: str) -> Optional[str]:
        """Run the whole cell and return the last result.
        Returns None if the interpreter timed out."""
        script_path = self._create_script(script)
        self._run_script(script_path)
        result = self._fetch_result()
        script_path.unlink()
        return result


from pyspark import SparkConf, SparkContext
import random
import numpy as np
import time

def mapper1(line):
    matrix_name, row, col, num = line.split(",")
    row, col, num = int(row), int(col), int(num)
    mapList = []
    for idx in range(MATRIX_SIZE):
        if matrix_name == 'M':
            key = (row, idx, col)
            value = num
        else:
            key = (idx, col, row)
            value = num
        mapList.append([key, value])
    return mapList

def reducer1(x, y):
    return x * y

def mapper2(x):  # x => ((0, 0, 0), 10)
    row, col, idx = x[0]
    key = (row, col)
    value = x[1]
    return [(key, value)]

def reducer2(x, y):
    return x + y

def map_and_reduce(file_path):
    conf = SparkConf().setMaster("local").setAppName("matrix_multiplication")
    sc = SparkContext(conf=conf)
    lines = sc.textFile(file_path).flatMap(mapper1)
    lines = lines.reduceByKey(reducer1)
    lines = lines.flatMap(mapper2)
    lines = lines.reduceByKey(reducer2)
    return lines.collect()

def gen_test_case(matrix_size):
    M = []
    N = []
    with open("{}input.txt".format(matrix_size), 'w') as f:
        for i in range(matrix_size):
            M_row = []
            for j in range(matrix_size):
                item = random.randint(1, 10)
                M_row.append(item)
                write_str = '{},{},{},{}\n'.format('M', i, j, item)
                f.write(write_str)
            M.append(M_row)

        for i in range(matrix_size):
            N_row = []
            for j in range(matrix_size):
                item = random.randint(1, 10)
                N_row.append(item)
                write_str = '{},{},{},{}\n'.format('N', i, j, item)
                f.write(write_str)
            N.append(N_row)

    answer = np.matmul(np.array(M), np.array(N))
    with open("{}input_answer.txt".format(matrix_size), 'w') as f:
        for i in range(answer.shape[0]):
            for j in range(answer.shape[1]):
                item = answer[i, j]
                write_str = '{},{},{}\n'.format(i, j, item)
                f.write(write_str)

def write_results_to_file(results, matrix_size):
    sort_key = lambda x: (x[0], x[1])
    results.sort(key=sort_key)
    with open("{}output.txt".format(matrix_size), 'w') as f:
        for result in results:
            row, col = result[0]
            value = result[1]
            write_str = '{},{},{}\n'.format(row, col, value)
            f.write(write_str)

if __name__ == '__main__':
    MATRIX_SIZE = 500
    # gen_test_case(MATRIX_SIZE)
    FILE_PATH = '{}input.txt'.format(MATRIX_SIZE)
    start_time = time.time()
    results = map_and_reduce(file_path=FILE_PATH)
    end_time = time.time()
    print('running time: {:.2f} min'.format((end_time - start_time) / 60))
    write_results_to_file(results, MATRIX_SIZE)


import json #to impost post.json
from blog.models import Post

# instance of opening and loading json data
with open('post.json') as f:
    posts_json = json.load(f)

# Loop through JSON data
for post in posts_json:
    """
    input:
        title: the title of the json element
        content: the cotent of the json element

        author_id: the user number of the json element, which is used as the
        ForeignKey to connect the blog site to the User database.
            Still trying to verify, but SQL convention is that the blog primary
            key would author and the foreign key should be author_id.

    output:
        After interation, it will post the JSON elements as new posts in blog
    """
    post = Post(title=post['title'],
    content=post['content'],
    author_id = post['user_id'])

    post.save()


import os
import cv2
import dlib
import numpy as np
from eye import Eye
from calibration import Calibration

class EyeTracking(object):
    """
    This class tracks the user's gaze.
    It provides useful information like the position of the eyes
    and pupils and allows to know if the eyes are open or closed
    """

    def __init__(self):
        self.frame = None
        self.calibration = Calibration()

        self.eye_left = None
        self.eye_right = None
        self.ex_eye_left = None
        self.ex_eye_right = None

        self.is_attention = 100
        self.method = ""

        # _face_detector is used to detect faces
        self._face_detector = dlib.get_frontal_face_detector()

        # _predictor is used to get facial landmarks of a given face
        cwd = os.path.abspath(os.path.dirname(__file__))
        model_path = os.path.abspath(os.path.join(cwd, "./models/shape_predictor_68_face_landmarks.dat"))
        self._predictor = dlib.shape_predictor(model_path)

        # dnn based landmark
        self.net = cv2.dnn.readNetFromCaffe(
            './models/deploy.prototxt.txt',
            './models/res10_300x300_ssd_iter_140000.caffemodel'
        )
        self.landmark_predictor = dlib.shape_predictor('./models/shape_predictor_68_face_landmarks.dat')

    @property
    def pupils_located(self):
        """Check that the pupils have been located"""
        try:
            int(self.eye_left.pupil.x)
            int(self.eye_left.pupil.y)
            int(self.eye_right.pupil.x)
            int(self.eye_right.pupil.y)
            return True
        except Exception:
            return False

    def adjust_gamma(self, image, gamma):
        invGamma = 1.0 / gamma
        table = np.array([((i / 255.0) ** invGamma) * 255
            for i in np.arange(0, 256)]).astype("uint8")

        return cv2.LUT(image, table)

    def preprocess(self, img):
        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        if gray_img.mean() < 130:
                img = self.adjust_gamma(img, 1.0)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

        return img

    def _analyze(self):
        """Detects the face and initialize Eye objects"""
        # 필터링의 중요성 preprocess를 하고 안 하고에 따라 결과가 달라진다.
        # dnn에 감마 값을 잘 조절하면 좋지 않을까.
        frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
        # frame = self.preprocess(self.frame)
        faces = self._face_detector(frame)

        try:
            if faces:
                self.method = "hog"

                landmark = np.empty([68, 2], dtype = int)
                landmarks = self._predictor(frame, faces[0])

                for i in range(68):
                    landmark[i][0] = landmarks.part(i).x
                    landmark[i][1] = landmarks.part(i).y
                # print(landmark)

                self.eye_left = Eye(frame, landmark, 0, self.calibration)
                self.eye_right = Eye(frame, landmark, 1, self.calibration)

            else:
                self.method = "dnn"

                (h, w) = self.frame.shape[:2]

                blob = cv2.dnn.blobFromImage(
                    cv2.resize(self.frame, (300, 300)),
                        1.0,(300, 300), (104.0, 177.0, 123.0
                    )
                )
                self.net.setInput(blob)
                detections = self.net.forward()

                ## bounding box
                list_bboxes = []
                list_confidence = []
                list_dlib_rect = []

                for i in range(0, detections.shape[2]):
                    confidence = detections[0, 0, i, 2]

                    if confidence < 0.6:
                        continue

                    box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                    (l, t, r, b) = box.astype('int')

                    original_vertical_length = b - t
                    t = int(t + original_vertical_length * 0.15)
                    b = int(b - (original_vertical_length) * 0.05)

                    margin = ((b-t) - (r-l))//2
                    l = l - margin if (b-t-r+l)%2 == 0 else l - margin - 1
                    r = r + margin
                    list_bboxes.append([l, t, r, b])
                    list_confidence.append(confidence)
                    rect_bb = dlib.rectangle(left=l, top=t, right=r, bottom=b)
                    list_dlib_rect.append(rect_bb)

                # landmark
                list_landmarks = []

                for rect in list_dlib_rect:
                    points = self.landmark_predictor(self.frame, rect)
                    list_points = list(map(lambda p: (p.x, p.y), points.parts()))
                    list_landmarks.append(list_points)
                    # print(list_landmarks)

                    list_landmarks = list_landmarks[0]

                    self.eye_left = Eye(self.frame, list_landmarks, 0, self.calibration)
                    self.eye_right = Eye(self.frame, list_landmarks, 1, self.calibration)

        except IndexError:
            self.eye_left = None
            self.eye_right = None

    def refresh(self, frame):
        """Refreshes the frame and analyzes it.

        Arguments:
            frame (numpy.ndarray): The frame to analyze
        """
        self.frame = frame
        self._analyze()

    def pupil_left_coords(self):
        """Returns the coordinates of the left pupil"""
        if self.pupils_located:
            x = self.eye_left.origin[0] + self.eye_left.pupil.x
            y = self.eye_left.origin[1] + self.eye_left.pupil.y
            return (x, y)

    def pupil_right_coords(self):
        """Returns the coordinates of the right pupil"""
        if self.pupils_located:
            x = self.eye_right.origin[0] + self.eye_right.pupil.x
            y = self.eye_right.origin[1] + self.eye_right.pupil.y
            return (x, y)

    def get_attention(self):
        return self.is_attention

    def get_method(self):
        return self.method

    def horizontal_ratio(self):
        """Returns a number between 0.0 and 1.0 that indicates the
        horizontal direction of the gaze. The extreme right is 0.0,
        the center is 0.5 and the extreme left is 1.0
        """
        if self.pupils_located:
            pupil_left = self.eye_left.pupil.x / (self.eye_left.center[0] * 2 - 10)
            pupil_right = self.eye_right.pupil.x / (self.eye_right.center[0] * 2 - 10)
            return (pupil_left + pupil_right) / 2

    def vertical_ratio(self):
        """Returns a number between 0.0 and 1.0 that indicates the
        vertical direction of the gaze. The extreme top is 0.0,
        the center is 0.5 and the extreme bottom is 1.0
        """
        if self.pupils_located:
            pupil_left = self.eye_left.pupil.y / (self.eye_left.center[1] * 2 - 10)
            pupil_right = self.eye_right.pupil.y / (self.eye_right.center[1] * 2 - 10)
            return (pupil_left + pupil_right) / 2

    def is_right(self):
        """Returns true if the user is looking to the right"""
        if self.pupils_located:
            return self.horizontal_ratio() <= 0.35

    def is_left(self):
        """Returns true if the user is looking to the left"""
        if self.pupils_located:
            return self.horizontal_ratio() >= 0.65

    def is_center(self):
        """Returns true if the user is looking to the center"""
        if self.pupils_located:
            return self.is_right() is not True and self.is_left() is not True

    def is_blinking(self):
        """Returns true if the user closes his eyes"""
        if self.pupils_located:
            blinking_ratio = (self.eye_left.blinking + self.eye_right.blinking) / 2
            return blinking_ratio > 3.8

    def is_focus(self):
        if self.ex_eye_left is None:
            self.eye_position_update()

            return 0

        if self.pupils_located:
            focus = (
                ((self.eye_left.pupil.x + self.eye_right.pupil.x) / 2) -
                ((self.ex_eye_left.pupil.x + self.ex_eye_right.pupil.x) / 2)
            )

            self.eye_position_update()

            if abs(focus) < 5:
                return 1
            else:
                return 0

    def eye_position_update(self):
        self.ex_eye_left = self.eye_left
        self.ex_eye_right = self.eye_right

    def annotated_frame(self):
        """Returns the main frame with pupils highlighted"""
        frame = self.frame.copy()

        if self.pupils_located:
            color = (0, 255, 0)
            x_left, y_left = self.pupil_left_coords()
            x_right, y_right = self.pupil_right_coords()

            cv2.line(frame, (x_left - 5, y_left), (x_left + 5, y_left), color)
            cv2.line(frame, (x_left, y_left - 5), (x_left, y_left + 5), color)
            cv2.line(frame, (x_right - 5, y_right), (x_right + 5, y_right), color)
            cv2.line(frame, (x_right, y_right - 5), (x_right, y_right + 5), color)

        return frame


# Lint as: python3
"""CoQA: A Conversational Question Answering Challenge"""
# partially taken from https://github.com/NTU-SQUAD/transformers-coqa/blob/2dfd58b70956e935e370989fa421f34bb83bff08/data/processors/coqa.py

from __future__ import absolute_import, division, print_function

import json
import logging
import os
import re
import string
from collections import Counter

import spacy
import datasets

MAX_Q_LEN = 100  # Max length of question
YOUR_LOCAL_DOWNLOAD = "../data"

_CITATION = """\
    @article{reddy-etal-2019-coqa,
        title = "{C}o{QA}: A Conversational Question Answering Challenge",
        author = "Reddy, Siva  and
        Chen, Danqi  and
        Manning, Christopher D.",
        journal = "Transactions of the Association for Computational Linguistics",
        volume = "7",
        month = mar,
        year = "2019",
        url = "https://www.aclweb.org/anthology/Q19-1016",
        doi = "10.1162/tacl_a_00266",
        pages = "249--266",
        }
"""

_DESCRIPTION = """\
    CoQA is a large-scale dataset for building Conversational Question Answering systems. \
    The goal of the CoQA challenge is to measure the ability of machines to understand a text passage\
    and answer a series of interconnected questions that appear in a conversation. \
    CoQA is pronounced as coca.
"""

_HOMEPAGE = "https://stanfordnlp.github.io/coqa/"

_URLs = "https://nlp.stanford.edu/data/coqa/coqa-train-v1.0.json, https://nlp.stanford.edu/data/coqa/coqa-dev-v1.0.json"

def _improve_answer_span(doc_tokens, input_start, input_end, tokenizer, orig_answer_text):
    """Returns tokenized answer spans that better match the annotated answer."""
    tok_answer_text = " ".join(tokenizer.tokenize(orig_answer_text))

    for new_start in range(input_start, input_end + 1):
        for new_end in range(input_end, new_start - 1, -1):
            text_span = " ".join(doc_tokens[new_start: (new_end + 1)])
            if text_span == tok_answer_text:
                return (new_start, new_end)

    return (input_start, input_end)

class Coqa(datasets.GeneratorBasedBuilder):

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="coqa_rc",
            version=VERSION,
            description="Load CoQA dataset for machine reading comprehension tasks",
        )
    ]

    DEFAULT_CONFIG_NAME = "coqa_rc"

    def _info(self):
        if self.config.name == "coqa_rc":
            features = datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "title": datasets.Value("string"),
                    "context": datasets.Value("string"),
                    "question": datasets.Value("string"),
                    "answers": datasets.features.Sequence(
                        {
                            "text": datasets.Value("string"),
                            "answer_start": datasets.Value("int32"),
                            # "answer_end": datasets.Value("int32"),
                        }
                    ),
                    "domain": datasets.Value("string"),  # is "source" in CoQA (e.g., wikipedia)
                }
            )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            supervised_keys=None,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):

        my_urls = _URLs

        # data_dir = dl_manager.download_and_extract(my_urls)
        data_dir = YOUR_LOCAL_DOWNLOAD # point to local dir to avoid downloading the dataset again
        if self.config.name == "coqa_rc":
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    gen_kwargs={
                        "filepath": os.path.join(
                            data_dir, "coqa/coqa-dev-v1.0.json"
                        ),
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "filepath": os.path.join(
                            data_dir, "coqa/coqa-train-v1.0.json"
                        ),
                    },
                ),
            ]

    def _str(self, s):
        """ Convert PTB tokens to normal tokens """
        if (s.lower() == '-lrb-'):
            s = '('
        elif (s.lower() == '-rrb-'):
            s = ')'
        elif (s.lower() == '-lsb-'):
            s = '['
        elif (s.lower() == '-rsb-'):
            s = ']'
        elif (s.lower() == '-lcb-'):
            s = '{'
        elif (s.lower() == '-rcb-'):
            s = '}'
        return s

    def space_extend(self, matchobj):
        return ' ' + matchobj.group(0) + ' '

    def is_whitespace(self, c):
        if c == " " or c == "\t" or c == "\r" or c == "\n" or ord(c) == 0x202F:
            return True
        return False

    def pre_proc(self, text):
        text = re.sub(u'-|\u2010|\u2011|\u2012|\u2013|\u2014|\u2015|%|\[|\]|:|\(|\)|/|\t', self.space_extend, text)
        text = text.strip(' \n')
        text = re.sub('\s+', ' ', text)
        return text

    def process(self, parsed_text):
        output = {'word': [], 'offsets': [], 'sentences': []}

        for token in parsed_text:
            output['word'].append(self._str(token.text))
            output['offsets'].append((token.idx, token.idx + len(token.text)))

        word_idx = 0
        for sent in parsed_text.sents:
            output['sentences'].append((word_idx, word_idx + len(sent)))
            word_idx += len(sent)

        assert word_idx == len(output['word'])
        return output

    def normalize_answer(self, s):
        """Lower text and remove punctuation, storys and extra whitespace."""

        def remove_articles(text):
            regex = re.compile(r'\b(a|an|the)\b', re.UNICODE)
            return re.sub(regex, ' ', text)

        def white_space_fix(text):
            return ' '.join(text.split())

        def remove_punc(text):
            exclude = set(string.punctuation)
            return ''.join(ch for ch in text if ch not in exclude)

        def lower(text):
            return text.lower()

        return white_space_fix(remove_articles(remove_punc(lower(s))))

    def get_raw_context_offsets(self, words, raw_text):
        raw_context_offsets = []
        p = 0
        for token in words:
            while p < len(raw_text) and re.match('\s', raw_text[p]):
                p += 1
            if raw_text[p:p + len(token)] != token:
                print('something is wrong! token', token, 'raw_text:',
                      raw_text)

            raw_context_offsets.append((p, p + len(token)))
            p += len(token)

        return raw_context_offsets

    def find_span(self, offsets, start, end):
        start_index = -1
        end_index = -1
        for i, offset in enumerate(offsets):
            if (start_index < 0) or (start >= offset[0]):
                start_index = i
            if (end_index < 0) and (end <= offset[1]):
                end_index = i
        return (start_index, end_index)

    def find_span_with_gt(self, context, offsets, ground_truth):
        best_f1 = 0.0
        best_span = (len(offsets) - 1, len(offsets) - 1)
        gt = self.normalize_answer(self.pre_proc(ground_truth)).split()

        ls = [
            i for i in range(len(offsets))
            if context[offsets[i][0]:offsets[i][1]].lower() in gt
        ]

        for i in range(len(ls)):
            for j in range(i, len(ls)):
                pred = self.normalize_answer(
                    self.pre_proc(
                        context[offsets[ls[i]][0]:offsets[ls[j]][1]])).split()
                common = Counter(pred) & Counter(gt)
                num_same = sum(common.values())
                if num_same > 0:
                    precision = 1.0 * num_same / len(pred)
                    recall = 1.0 * num_same / len(gt)
                    f1 = (2 * precision * recall) / (precision + recall)
                    if f1 > best_f1:
                        best_f1 = f1
                        best_span = (ls[i], ls[j])
        return best_span

    def _generate_examples(self, filepath):
        """This function returns the examples in the raw (text) form."""
        with open(filepath, encoding="utf-8") as f:
            data = json.load(f)["data"]

        for row in data:
            story = row["story"]
            domain = row["source"]
            title = row["filename"]
            all_prev_utterances = []
            for i, question in enumerate(row['questions']):
                id_ = str(row['id']) + '_' + str(question['turn_id'])
                all_prev_utterances.append(question['input_text'])
                answers =  [{
                    "text": row["answers"][i]["span_text"],
                    "answer_start": row["answers"][i]["span_start"]
                }]

                question_str = " ".join(
                                    list(reversed(all_prev_utterances))
                                ).strip()
                question_str = " ".join(question_str.split()[:MAX_Q_LEN])

                # append the original answer into the utterance list
                orig_answer_text = row["answers"][i]["input_text"]
                all_prev_utterances.append(orig_answer_text)

                qa = {
                        "id": id_,
                        "domain": domain,
                        "title": title,
                        "context": story,
                        "question": question_str,
                        "answers": answers,
                }
                yield id_, qa


#!/usr/bin/env python
#coding=utf-8

import json
from lib.sqs import zhihufav_sqs
from lib.tasks import add_note

def get_sqs_queue():
    sqs_info = zhihufav_sqs.get_messages(10)
    for sqs in sqs_info:
        sqs_body = sqs.get_body()
        receipt_handle = sqs.receipt_handle
        sqs_json = json.loads(sqs_body)
        api_url = sqs_json.get('api_url')
        parent_note = sqs_json.get('parent_note')
        add_note.delay(api_url, parent_note, receipt_handle)

if __name__=="__main__":
    for i in range(5):
        get_sqs_queue()


import json
import logging
import sys
from z3 import z3

from teether.constraints import check_model_and_resolve
from teether.evm.exceptions import IntractablePath
from teether.evm.state import LazySubstituteState, SymRead
from teether.project import Project
from teether.util.z3_extra_util import concrete

def set_balance(addr, bal, i_r):
    calldata = z3.Array('CALLDATA_%d' % i_r.xid, z3.BitVecSort(256), z3.BitVecSort(8))
    new_calldata = z3.Store(z3.Store(z3.Store(z3.Store(calldata, 0, 0x70), 1, 0xa0), 2, 0x82), 3, 0x31)
    for num, byte in enumerate(addr.to_bytes(32, 'big'), 4):
        new_calldata = z3.Store(new_calldata, num, byte)
    subst = [(calldata, new_calldata)]
    state = LazySubstituteState(i_r.state, subst)
    constraints = [z3.substitute(c, subst) for c in i_r.constraints]
    sha_constraints = {sha: z3.simplify(z3.substitute(sha_value, subst)) if not isinstance(sha_value, SymRead)
    else sha_value for sha, sha_value in i_r.sha_constraints.items()}
    mstart, msz = state.stack[-1], state.stack[-2]
    mm = i_r.state.memory.read(mstart, msz)
    if not isinstance(mm, SymRead):
        if all(concrete(m) for m in mm):
            return None
        mm = z3.simplify(z3.Concat([m if not concrete(m) else z3.BitVecVal(m, 8) for m in mm]))
    extra_constraints = [mm == bal]
    try:
        model = check_model_and_resolve(constraints + extra_constraints, sha_constraints)
        sloads = []
        storage = None
        for v in model:
            if v.name().startswith("SLOAD"):
                sloads.append(model.eval(model[v]).as_long())
            if v.name().startswith("STORAGE"):
                storage = z3.simplify(model[v])
        return {sl: model.eval(storage[sl]).as_long() for sl in sloads}
    except IntractablePath:
        return None

def main(code_path, output_file, target_addrs, target_bals):
    if code_path.endswith('.json'):
        with open(code_path, 'rb') as f:
            jd = json.load(f)
        p = Project.from_json(jd)
    else:
        with open(code_path) as infile:
            inbuffer = infile.read().rstrip()
    code = bytes.fromhex(inbuffer)
    p = Project(code)
    with open('%s.project.json' % code_path, 'w') as f:
        json.dump(p.to_json(), f)

    target_addrs = [int(addr, 16) for addr in target_addrs]
    target_bals = [int(bal) for bal in target_bals]
    storage_result = dict()

    addr, bal = target_addrs[0], target_bals[0]
    return_ins = p.cfg.filter_ins('RETURN')
    gen_constraints = p.get_constraints(return_ins)
    results = []
    for _, _, i_r in gen_constraints:
        stor = set_balance(addr, bal, i_r)
        if stor:
            storage_result.update(stor)
            results = [i_r] + results
            break
        results.append(i_r)
    else:
        logging.warning(f"Could not set balance of {hex(addr)} to {bal}")
    for addr,bal in zip(target_addrs[1:], target_bals[1:]):
        for i_r in results:
            stor = set_balance(addr, bal, i_r)
            if stor:
                storage_result.update(stor)
                break
        else:
            for _, _, i_r in gen_constraints:
                stor = set_balance(addr, bal, i_r)
                if stor:
                    storage_result.update(stor)
                    results = [i_r] + results
                    break
                results.append(i_r)
            else:
                logging.warning(f"Could not set balance of {hex(addr)} to {bal}")

    with open(output_file, 'w') as f:
        json.dump({"0x{0:0{1}X}".format(k, 64): "0x{0:0{1}x}".format(v, 64) for k, v in storage_result.items()}, f)

if __name__ == '__main__':

    if len(sys.argv) < 5 or len(sys.argv) % 2 != 1:
        print('Usage: %s <code> <output file> <target-address> <target-balance> [<target-address> <target-balance>] ...'
              % sys.argv[0], file=sys.stderr)
        exit(-1)

    code = sys.argv[1]
    output_file = sys.argv[2]
    target_addresses = sys.argv[3::2]
    target_balances = sys.argv[4::2]

    main(code, output_file, target_addresses, target_balances)


"""6. Write a program that takes a string as input and returns the string with all vowels removed."""
import tests
import time

vowels = ['a', 'e', 'o', 'u', 'y', 'i', 'A', 'E', 'O', 'U', 'Y', 'I']
my_str = tests.cases[0]
#my_str = input('Enter any string: ')
my_str = my_str.strip()

def branch(number_of_repeats=10000):
    def time_of_function(function):
        def wrapped(*args):
            start_time = time.time()
            for i in range(number_of_repeats):
                res = function(*args)
            end_time = time.time()
            dt = (round(end_time - start_time, 3))
            return dt
        return wrapped
    return time_of_function

def try1(s):
    return s.replace('a', '').replace('o', '').replace('e', '').replace('i', '').replace('u', '').replace('y', '').replace('A', '').replace('O', '').replace('E', '').replace('I', '').replace('U', '').replace('Y', '')

def try2(s):
    ans = []
    for l in s:
        if l in vowels:
            continue
        ans.append(l)
    return ''.join(ans)

def try3(s):
    for vowel in vowels:
        s = ''.join(s.split(vowel))
    return s

def try4(s):
    return ''.join([l for l in s if l not in vowels])

print('\nHello, this program will delete all vowels from Walt Whitman poem "O Captain! My Captain!"\n\n\n')
print(my_str)
print('\n\n\nBut you can choose you own text. Type "u" to eneter user\'s text!')
enter = input('Or anything else to continue with default text: ')
if enter.lower() in ('u', 'г'):
    my_str = input('Fine! Enter your text: ')
ans = try1(my_str)
print(ans)
print('\n\n\nDo you want to know which functions works faster? I think you do!')
print('try1 will delete all vowels 10000 times for: ')
print(branch()(try1)(my_str))
print('try2 will delete all vowels 10000 times for: ')
print(branch()(try2)(my_str))
print('try3 will delete all vowels 10000 times for: ')
print(branch()(try3)(my_str))
print('try4 will delete all vowels 10000 times for: ')
print(branch()(try4)(my_str))

# my_str = input("Enter any string")


'''input
4
dwight jello 51430
creed beans 263
stanley pretzels 45121
pam brushtool 941
'''
n = int(input())

gifts = {}

while n > 0:
	s = input().split()
	k = ' '.join(s[:-1]).strip()
	v = int(s[-1])
	gifts[k] = v
	n-=1

gifts = {k: v for k, v in sorted(gifts.items(), key=lambda item: item[1])}

for k, v in gifts.items():
	print(f"{k} {v}")