#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 12 15:36:15 2018

@author: chrelli
added unix time stamps to the first camera!

Ways to slim down the data:
    no unix time stamps?
    no color frame showing? - yes, helps a lot!
    no png compression? Totally fine at 30 fps!

Majow to do list:
    - use arduino to synchronize? Yes, could send out synchronization time code to another unit: Problem: doesn't account for delay of arriving frames

    - use depth roi to slim down writing footprint

    - use LED roi to get blinking time stamps

    -

## with connected device cam
from pyrealsense import offline
offline.save_depth_intrinsics(dev)
"""

#%% Import the nescessary stuff
# basic OS stuff
import time, os, sys, shutil
import json
# for math and plotting
import pandas as pd
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt

# small utilities
import csv
from colour import Color
from itertools import compress # for list selection with logical
from tqdm import tqdm

# for image manipulation
import cv2

# for recording and connecting to the intel realsense librar
#import pyrealsense as pyrs
# add the realsense library
sys.path.append(r'/usr/local/lib')
# and load it!
import pyrealsense2 as rs

#import multiprocessing
from multiprocessing import Process

# import handy Functions
from utils.common_utils import *
from utils.recording_utils import *

#%% Parse some inputs

import argparse

parser = argparse.ArgumentParser(description='Records cad and d images with no roi cut to disk. Also records timestamps and led traces using the auto LED mask. Currently, with no ROI, the program maxes out disk write speed around 45 fps.',formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument('--ncams', type=int, default = 4 , choices=[1,2,3,4],
                    help='number of cameras to stream')

parser.add_argument('--fps',type=int, default = 30 , choices=[30,60],
                    help='select fps to stream')

#parser.add_argument("--singlecore", help="disables mult.proc. for debugging on macbook, overrides ncams to 1",
#                    action="store_true")

parser.add_argument("--plots", help="shows the live video while recording",
                    action="store_true")

args = parser.parse_args()

#%% Constants
# frame_width,frame_height  = 848,480
frame_width,frame_height  = 640,480

fps_choice = args.fps
# number of padding digits for the frame numbers
n_padding_digits = 8

print('# cameras: '+str(args.ncams))
print('Frame size is '+str(frame_width)+'x'+str(frame_height)+' pixels.')
print('Grabbing frames at '+str(fps_choice)+' fps')

# get the current timestring
timestr = time.strftime("%Y%m%d-%H%M%S")

# reset the folder
#data_folder = '/media/chrelli/Data0'
#top_folder = data_folder + '/calibration_' + timestr
#reset_folder_if_present(top_folder)
#
#top_folder_0 = top_folder
#top_folder_1 = top_folder

# reset the folders
top_folder_0 = '/media/chrelli/Data0' + '/calibration_' + timestr
top_folder_1 = '/media/chrelli/Data1' + '/calibration_' + timestr

reset_folder_if_present(top_folder_0)
reset_folder_if_present(top_folder_1)

# also make the numpy folders
npy_folder_0 = top_folder_0+'/npy_raw'
npy_folder_1 = top_folder_1+'/npy_raw'

reset_folder_if_present(npy_folder_0)
reset_folder_if_present(npy_folder_1)

#%% 8 bit color setup
fps_color = (Color('White').rgb)
ts_color = (Color('Peru').rgb)

# convert to 8 bit color
fps_color=tuple(255*x for x in fps_color)
ts_color=tuple(255*x for x in ts_color)

#%% Block for running
# open the pyrealsense server
#serv = pyrs.Service()

# set the start time for the unix time stamp
start_time = time.time()

# open up a realsense context and get a list of the devices!
ctx = rs.context()

devices = [ctx.devices[i] for i in range(args.ncams)]
# sort the devices by their serial numbers
serials = [devices[i].get_info(rs.camera_info.serial_number) for i in range(args.ncams)]
devices = [x for _,x in sorted(zip(serials,devices))]

def sub_function_trick(which_device,top_folder):
    show_frames = args.plots

    ####################
    #
    # DEVICE SETUP BLOCK
    #
    #####################

    # get the serial of that device
    device =  devices[which_device]
    device_serial = device.get_info(rs.camera_info.serial_number)

    #set the preset
    advnc_mode = rs.rs400_advanced_mode(device)
    print("Advanced mode is", "enabled" if advnc_mode.is_enabled() else "disabled")
    # run like
    # advnc_mode.load_json(json_string)

    # load the preset here!
    preset_folder = '/home/chrelli/git/3d_sandbox/mycetrack0p8/presets/'
    if device_serial[:3] == '740':
        preset_name = 'master60pp'
    else:
        preset_name = 'slave60pp'

    jsonFile = preset_folder+preset_name+'.json'
    jsonObj = json.load(open(jsonFile))
    json_string = str(jsonObj).replace("'", '\"')
    print("Configuration " + jsonFile + " loaded");
    time.sleep(1.)
    advnc_mode.load_json(json_string)
    print("Configuration " + jsonFile + " applied!");

    if device_serial[:3] == '740':
        # master
        targetSyncMode = 1
    else:
        # slave
        targetSyncMode = 2
    device.first_depth_sensor().set_option(rs.option.inter_cam_sync_mode, targetSyncMode)

    # first, open up a  config
    config = rs.config()

    # then open a pipeline
    pipeline = rs.pipeline()

    # enable the selected device and streams # RGB SPACE HERE
    config.enable_device(device_serial);
    config.enable_stream(rs.stream.depth, frame_width,frame_height, rs.format.z16, fps_choice)
#        config.enable_stream(rs.stream.color, frame_width,frame_height, rs.format.rgb8, fps_choice)

    config.enable_stream(rs.stream.color, frame_width,frame_height, rs.format.rgb8, fps_choice)
    config.enable_stream(rs.stream.infrared,1, frame_width,frame_height, rs.format.y8, fps_choice)

    print("PING after enabling the sync mode is {}".format(device.first_depth_sensor().get_option(rs.option.inter_cam_sync_mode)))

    # Start streaming, call the stream 'cfg' for some reason, as pr example
    cfg = pipeline.start(config)

    # create an align object
    # alternative is to align to color, faster but less precise: align_to = rs.stream.color
    align_to = rs.stream.depth
    align = rs.align(align_to)

    print('dev '+str(which_device)+' serial is ' + device_serial)
    # Use the first three digits of the serial as a string to tag the device:
    device_tag = device_serial[0:3]

    if show_frames:
        # open a window for cv2
        window_title = "dev"+str(which_device)+"(#" + device_tag + ")"
        cv2.namedWindow(window_title+'cad')

        # block for setting up a low-level fps estimation,
        cnt = 0 # a counter
        last = time.time() # start_time
        fps = 0 # initial fps value

    # save the camera intrinsics
    # Getting the depth sensor's depth scale (see rs-align example for explanation)
    depth_sensor = cfg.get_device().first_depth_sensor()
    depth_scale = depth_sensor.get_depth_scale()
    print ("Depth Scale is: " , depth_scale)

    # this is how to get the intrinsics
    profile = cfg.get_stream(rs.stream.depth) # Fetch stream profile for depth stream
    intr = profile.as_video_stream_profile().get_intrinsics() # Downcast to video_stream_profile

    #% now make file and save time stamps and depth scaling and intrinsics etc
    # use the old naming convention

    parameternames = np.array(['cam_params.fx',
                       'cam_params.fy',
                       'cam_params.ppx',
                       'cam_params.ppy',
                       'd_scale',
                       'fps_choice',
                       'frame_width',
                       'frame_height'])
    parameters = np.array([intr.fx,
                       intr.fy,
                       intr.ppx,
                       intr.ppy,
                       depth_scale,
                       fps_choice,
                       intr.width,
                       intr.height])

    # open a file for writint the parameters
    with open(top_folder+'/parameters_'+str(which_device)+'.csv','w') as intrfile:
        writer = csv.writer(intrfile, delimiter=',')
        writer.writerow(parameternames)
        writer.writerow(parameters)

    # load the automatic led mask from the constants folder!
    led_mask,led_logic,led_centroid = load_auto_roi(which_device)

    # open a file for time stamps
    tsfile = open(top_folder+'/timestamps_'+str(which_device)+'.csv','w')

#    ## HF try to open an HF file
#    import h5py
#    #TODO input from somewhere
#    hf = h5py.File(top_folder+'/dev'+str(which_device)+'_d_'+'.h5', 'w')
#    # also open one for the cad
#    hf_cad = h5py.File(top_folder+'/dev'+str(which_device)+'_cad_'+'.h5', 'w')

    # NPY ADDITION
    npy_folder = top_folder+'/npy_raw'

    # open a file for led stamps
#        ledsfile = open(top_folder+'/ledstamps_'+str(which_device)+'.csv','w')

    print('starting to stream from device '+str(which_device)+'!')
    # wait for a bit for the cam to warm up
    # and loop over 30 frames
    warmup_time = 2 # seconds
    warmup = 0
    while warmup < fps_choice*warmup_time:
        frames = pipeline.wait_for_frames()
        warmup += 1
    print('device '+str(which_device)+' is warmed up!')

    # START A CLOCK FOR THE FRAMES!
    FRAME_CLOCK = 0
    try:
        while True:

            if show_frames:
                # for counting frame rate
                cnt += 1
                if (cnt % 10) == 0:
                    now = time.time() # after 10 frames
                    dt = now - last # how long did it take?
                    fps = 10/dt # calculate frame rate
                    last = now # assign a new value to the 'last time'

            #################################
            #
            # R E A D   B L O C K
            #
            #################################

            # Wait for a coherent pair of frames: depth and color
            frames = pipeline.wait_for_frames()

            # get the frame numbers and time stamps
#            ts = round(frames.get,2)
            ts = frames.get_timestamp()
            fn = frames.get_frame_number()

            # get the unix time stamp
            ts_unix = time.time()-start_time

            # run the alignment process
            aligned_frames = align.process(frames)
            depth_frame = aligned_frames.get_depth_frame() # aligned_depth_frame is a 640x480 depth image
            cad_frame = aligned_frames.get_color_frame()
            # also get one for the LED
            # depth_frame = frames.get_depth_frame()
            # color_frame = frames.get_color_frame()
            infrared_frame = frames.get_infrared_frame()

            # Convert images to numpy arrays
            depth = np.asanyarray(depth_frame.get_data())
            cad = np.asanyarray(cad_frame.get_data())
            c = np.asanyarray(infrared_frame.get_data())

            # get the LED value, round it a bit, could be profiled
            led_stamp = c[led_centroid[1],led_centroid[0]]

            # this is the writing block for the csv file, frame number and time stamp!
#            tsfile.write(str(FRAME_CLOCK)+','+str(fn)+','+str(ts)+','+str(ts_unix)+','+str(single_pixel_RGB2GRAY(led_stamp))+'\n')
            tsfile.write(str(FRAME_CLOCK)+','+str(fn)+','+str(ts)+','+str(ts_unix)+','+str(led_stamp)+'\n')

            # this is the writing block for the csv file, frame number and time stamp!
            #TODO put led with the others in same file?
#            ledsfile.write(str(single_pixel_RGB2GRAY(led_stamp))+'\n')

            # write the depth frames to tiff (replace: send to queue)
#            cv2.imwrite(top_folder+'/dev'+str(which_device)+'_d_'+str(FRAME_CLOCK).rjust(n_padding_digits,'0')+'.png', depth)
#            cv2.imwrite(top_folder+'/dev'+str(which_device)+'_cad_'+str(FRAME_CLOCK).rjust(n_padding_digits,'0')+'.png', cad)

#            hf.create_dataset(str(FRAME_CLOCK), data=depth)
#            hf_cad.create_dataset(str(FRAME_CLOCK), data=cad)

            np.save(npy_folder+'/dev'+str(which_device)+'_d_'+str(FRAME_CLOCK).rjust(n_padding_digits,'0')+'.npy',depth, allow_pickle = False)
            np.save(npy_folder+'/dev'+str(which_device)+'_cad_'+str(FRAME_CLOCK).rjust(n_padding_digits,'0')+'.npy',cad, allow_pickle = False)

            # UPDATE CLOCK
            FRAME_CLOCK += 1

            #
            if show_frames:
                # add text and show the CAD frames
                cv2.putText(cad, window_title+', fps: '+str(fps)[:4], (0, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, fps_color)
                cv2.putText(cad, str(round(ts)), (0, frame_height-20), cv2.FONT_HERSHEY_SIMPLEX, 1, ts_color)
                cv2.imshow(window_title+'cad', cad)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                # looks for a small q to nbe pressed
                # close the time stamp file
                tsfile.close

                # close the hf file
#                hf.close()
#                hf_cad.close()
#                ledsfile.close

                # stop the device
                pipeline.stop()
                print('pipeline from device '+str(which_device)+' is now closed!')
                break

    finally:
        tsfile.close

        # close the hf file
#        hf.close()
#        hf_cad.close()

        # stop the device
        pipeline.stop()
        print('pipeline from device '+str(which_device)+' is now closed!')

#%% define helping funtions for the multiprocessing
# these functions have to not be iterable.

def read_device_0():
    print('starting camera 1!')
    which_device = 0
    top_folder = top_folder_0
    sub_function_trick(which_device,top_folder)

def read_device_1():
    print('starting camera 2!')
    which_device = 1
    top_folder = top_folder_0
    sub_function_trick(which_device,top_folder)

def read_device_2():
    print('starting camera 3!')
    which_device = 2
    top_folder = top_folder_1
    sub_function_trick(which_device,top_folder)

def read_device_3():
    print('starting camera 4!')
    which_device = 3
    top_folder = top_folder_1
    sub_function_trick(which_device,top_folder)

#%% run the processes on independent cores
from multiprocessing import Process
if __name__ == '__main__':
    if args.ncams == 4:
        print('starting 4 cams, with multiprocessing!')
        # start 4 worker processes
        Process(target=read_device_0).start()
        time.sleep(3.)
        Process(target=read_device_1).start()
        Process(target=read_device_2).start()
        Process(target=read_device_3).start()
        Process(target=blink_using_firmata_random).start()

    elif args.ncams == 3:
        print('starting 3 cams, with multiprocessing!')
        Process(target=read_device_0).start()
        Process(target=read_device_1).start()
        Process(target=read_device_2).start()
        Process(target=blink_using_firmata).start()

    elif args.ncams == 2:
        print('starting 2 cams, with multiprocessing!')
        Process(target=read_device_0).start()
        Process(target=read_device_1).start()
        Process(target=blink_using_firmata).start()

    elif args.ncams == 1:
        print('starting 1 cam, with multiprocessing!')
        Process(target=read_device_0).start()
        Process(target=blink_using_firmata).start()


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)


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()


#!/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 io
import os
import random

from PIL import Image
import imageio
import requests
import seventv

def get_response(message: str):
    p_message = message.lower()

    if p_message[:3] == ("add"):
        url = p_message.split(" ")[1]
        return addGif(url)

    if p_message == "help":
        return helpText()

    return 'I didn\'t understand what you wrote. Try typing "help".'

def helpText():
    return "`?add <7tv url> to add a 7tv emoji to your server`"

def addGif(url):
    webpUrl = seventv.get_webp_url(url)
    try:
        webp_data = requests.get(webpUrl).content
    except requests.exceptions.RequestException:
        return "Invalid URL"

    # Extract the name from the url and replace .webp extension with .gif
    gif_name = seventv.get_emote_name(url)

    # Open webp image with PIL
    image = Image.open(io.BytesIO(webp_data))

    # If image is an animated webp, PIL will open it as a sequence of frames
    frames = []
    try:
        while True:
            frames.append(image.copy())
            image.seek(len(frames))  # Skip to next frame
    except EOFError:
        pass  # We have read all the frames from the image now

    # Create a byte buffer and save the gif data into it
    gif_data_buffer = io.BytesIO()

    # Set the duration of each frame based on the original image's frame rate
    duration_per_frame = image.info.get(
        "duration", 100
    )  # Default to 100ms if no duration is set

    imageio.mimwrite(
        gif_data_buffer, frames, "GIF", duration=duration_per_frame, loop=0
    )

    # Get the gif data as bytes
    gif_data = gif_data_buffer.getvalue()

    return gif_data, gif_name


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 http

import requests
import tenacity
from bs4 import BeautifulSoup
from tenacity import retry_if_exception_type, stop_after_attempt

from .exceptions import BadRequest, NetworkError, NotClientError, NotFound, ServerError
from .utils import cache

@tenacity.retry(
    reraise=True,
    retry=retry_if_exception_type(NotClientError),
    stop=stop_after_attempt(5),
)
@cache(ttl=1)
def get_weather_for_city(city: str) -> str:
    """
    Returns temperature of the city in celsius,
    as the source 'https://world-weather.ru' was used
    """

    url = f'https://world-weather.ru/pogoda/russia/{city}'
    page_text = None

    try:
        response = requests.get(url)
        response.raise_for_status()

        page_text = response.text
    except requests.exceptions.HTTPError as err:
        err_status = err.response.status_code

        if err_status == http.HTTPStatus.BAD_REQUEST:
            raise BadRequest() from None

        if err_status == http.HTTPStatus.NOT_FOUND:
            raise NotFound() from None

        if err_status == http.HTTPStatus.INTERNAL_SERVER_ERROR:
            raise ServerError from None

    except requests.exceptions.ConnectionError:
        raise NetworkError from None

    soup = BeautifulSoup(page_text, 'html.parser')

    return soup.find('div', id='weather-now-number').text


#!/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]))


from django.conf.urls import patterns, url, include

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns(
    'sokoban.views',
    url(r'^$', 'index', name='index'),
    url(r'^dashboard/$', 'dashboard', name='dashboard'),
    url(r'^home/$', 'home', name='home'),
    url(r'^403/$', 'alert_login_required', name='login_required'),
    url(r'^(?P<owner>.+)/projects/', 'list_projects', name='project_list'),
    url(r'^project/(?P<name>.*)/(?P<action>.*)/$', 'rest_project',
        name='project_rest'),
    url(r'^project/(?P<name>.*)/$', 'rest_project', name='project_rest'),
    url(r'^middleware/$', 'installed_middle_ware', name="middle_ware_list"),
    url(r'^log/(?P<project_name>.+)/$', 'get_log', name="get_log"),
    url(r'^logs/$', 'get_log', name="get_logs", kwargs={'project_name': None}),
    url(r'^admin/', include(admin.site.urls)),
    url('^accounts/', include('account.urls')),
    url('^scheduler/', include('scheduler.urls')),
)


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)))


import pandas as pd
import glob
from openpyxl import load_workbook
import os

class DataFile:

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

    @staticmethod
    def read_csv_file(file_path):
        """
        reads a *.csv file and returns a pandas DataFrame of the file

        :param file_path: path to a *.csv file
        :return: a pandas DataFrame from the chosen file
        """
        with open(file_path, encoding='UTF-16LE') as file:
            return pd.read_csv(file, sep='\t')

    def combine_to_excel(self, input_directory: str, output_file: str) -> None:
        """
        combines data from csv into one .xlsx file
        :param input_directory: name of directory that contains *.csv files
        :param output_file: name of the new file with combined data
        :return: saves a new *.xlsx file for future manipulations
        """
        parsed = [self.read_csv_file(file_path=path) for path in glob.glob(f'{input_directory}/*.csv')]
        merged = pd.concat(parsed)
        merged.to_excel(output_file, index=False)

    @staticmethod
    def load_invoices(file_path):
        """
        Loads first column in first sheet into a list.
        """
        invoice_file = load_workbook(file_path)
        invoice_sheet = invoice_file.active
        return [
            str(row[0]) for row in
            invoice_sheet.iter_rows(min_row=2, min_col=1, max_col=1, values_only=True)
        ]

    @staticmethod
    def remove_row(file_path):
        """
        Checks if first row contains unnecessary data (not headings)
        and removes it
        :param file_path: path to GFIS excel file

        """
        gfis_file = load_workbook(file_path)
        gfis_sheet = gfis_file.active
        for cell in gfis_sheet.iter_rows(max_row=1, values_only=True):
            if None in cell:
                gfis_sheet.delete_rows(1)
                gfis_file.save(file_path)
                print(f'row has been removed in {file_path}')
            else:
                print(f'no rows to remove in {file_path}. GFIS data spreadsheet is OK.')

    @staticmethod
    def remove_temporary_files(file):
        """
        removes created files after processing
        :param file:
        :return:
        """
        try:
            os.remove(file)
        except FileNotFoundError:
            print('Nothing to remove.')


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 os
from playhouse.sqlite_ext import CSqliteExtDatabase
from peewee import DatabaseProxy

class PDatabaseFactory:

    def __init__(self, config):
        self.cfg = config
        self.instances = {}
        self.defaut_instance = self.cfg.get('db', 'database')
        self.sqlite_db_path = self.cfg.get('sqlite', 'path')
        self.database_proxy = DatabaseProxy()

    def get_instance(self, instance: str = None):
        if not instance:
            instance = self.defaut_instance
        if instance not in self.instances.keys():
            if instance == 'sqlite':
                instance_obj = CSqliteExtDatabase(self.sqlite_db_path, autoconnect=False)
            elif instance == 'sqlite-app-test':
                PACKAGR_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
                instance_obj = CSqliteExtDatabase(os.path.join(PACKAGR_DIR, 'mediastrends_test.db'))
            elif instance == 'memory':
                instance_obj = CSqliteExtDatabase(':memory:')
            else:
                raise ValueError("Instance %s not defined" % (instance))
            self.instances[instance] = instance_obj

        instance = self.instances[instance]
        self.database_proxy.initialize(instance)

        return instance


from prettytable import PrettyTable
import sympy as sp

sp.init_printing(use_unicode=True)

def raices_multiples(x0, tolerancia, niter):
    x = sp.symbols('x')
    #f = x**4 - 18*x**2+81
    f = x**3 - x**2 - x + 1 + sp.sin(x-1)**2
    tabla = PrettyTable(['i', 'xn', 'f(xn)', 'df(xn)', 'ddf(xn)', 'ErrorAbs', 'ErrorRel'])
    df = f.diff(x) # Primera derivada
    ddf = df.diff(x) # Segunda derivada
    contador = 0
    xn = x0
    fx = f.evalf(subs={x:xn})
    dfx = df.evalf(subs={x:xn})
    ddfx = ddf.evalf(subs={x:xn})
    tabla.add_row([contador, xn, fx, dfx, ddfx, "No hay error", "No hay error"])
    error = tolerancia + 1.0
    while contador < niter and error > tolerancia:
        xnn = xn - (fx * dfx) / (dfx**2 - fx*ddfx)
        error = abs(xnn-xn)
        xn = xnn
        fx = f.evalf(subs={x:xn})
        dfx = df.evalf(subs={x: xn})
        ddfx = ddf.evalf(subs={x: xn})
        contador = contador + 1
        tabla.add_row([contador, xn, fx, dfx, ddfx, error, abs(error/xn)])
    print(tabla)
raices_multiples(0.5, 1e-5, 100)


import sys
import pandas as pd
import numpy as np
from sklearn.preprocessing import RobustScaler
from sklearn.tree import DecisionTreeClassifier
from evaluate_model import evaluate_model

dataset = sys.argv[1]
num_param_combinations = int(sys.argv[2])
random_seed = int(sys.argv[3])

np.random.seed(random_seed)

pipeline_components = [RobustScaler, DecisionTreeClassifier]
pipeline_parameters = {}

min_impurity_decrease_values = np.random.exponential(scale=0.01, size=num_param_combinations)
max_features_values = np.random.choice(list(np.arange(0.01, 1., 0.01)) + ['sqrt', 'log2', None], size=num_param_combinations)
criterion_values = np.random.choice(['gini', 'entropy'], size=num_param_combinations)
max_depth_values = np.random.choice(list(range(1, 51)) + [None], size=num_param_combinations)

all_param_combinations = zip(min_impurity_decrease_values, max_features_values, criterion_values, max_depth_values)
pipeline_parameters[DecisionTreeClassifier] = \
   [{'min_impurity_decrease': min_impurity_decrease, 'max_features': max_features, 'criterion': criterion, 'max_depth': max_depth, 'random_state': 324089}
     for (min_impurity_decrease, max_features, criterion, max_depth) in all_param_combinations]

evaluate_model(dataset, pipeline_components, pipeline_parameters)


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


# 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


import tkinter as tk
import tkinter.font as font
from PIL import Image, ImageTk
import QuikSpace as qs
import Add_Task_School_Work as atsw
import Review_Task_School_Work as rtsw

window=""

def quikSpace():
    global window
    window.destroy()
    qs.quikspace()

def ADD_Task_School_Work():
    global window
    window.destroy()
    atsw.add_task_school_work()

def Review_Task_School_Work():
    global window
    window.destroy()
    rtsw.review_task_school_work()

def school_Work():
    global window
    window=tk.Tk()
    window.geometry("780x670")
    window.resizable(0,0)
    window.title("School/Work")
    window.config(bg="#FFD9B3")

    font1=font.Font(family="Times New Roman",size=35,weight='bold')#Created the font
    font2=font.Font(family="Times New Roman",size=15)#Created the font
    font3=font.Font(family="Courier New",size=17)

    title=tk.Label(window, text="QuikSpace",font=font1, bg="#FFD9B3")#Created the title
    title.place(x=260,y=25)

    image=Image.open("Scheduler/logo.png")#Loads the image in RAM
    image=image.resize((90,60))
    photo=ImageTk.PhotoImage(image)#Converts the image into the Tkinter image format
    image_label=tk.Label(window,image=photo)
    image_label.place(x=440,y=20)

    slogan=tk.Label(window, text="All your tasks, in one app", font=font2, bg="#FFD9B3")
    slogan.place(x=315,y=90)

    add_task=tk.Button(window, text="Add Task",width=25, height=5, highlightbackground="#B3D9FF",fg="white",command=ADD_Task_School_Work)
    add_task.place(x=285,y=170)

    review_tasks=tk.Button(window, text="Review Tasks",width=25, height=5, highlightbackground="#B3D9FF",fg="white",command=Review_Task_School_Work)
    review_tasks.place(x=285,y=320)

    main_menu=tk.Button(window, text="Main Menu",width=25, height=5, highlightbackground="#B3D9FF",fg="white",command=quikSpace)
    main_menu.place(x=285,y=470)

    label=tk.Label(window, text="School/Work", font=font2, bg="#FFD9B3")
    label.place(x=360,y=645)

    credits=tk.Label(window, text="Created By: Smyan and Rithwik", font=font3, bg="#FFD9B3")
    credits.place(x=470,y=640)

    window.mainloop()


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


from requests import get

from webapp.db import db
from webapp.question_service.models import Question_answer

def get_question_jservice(questions_quantity: int=1) -> dict:
    """
        После получения колличества запрашиваемых вопросов сервис, в свою очередь,
        запрашивает с публичного API (англоязычные вопросы для викторин)
        https://jservice.io/api/random?count=1 указанное в полученном запросе количество вопросов.
    """
    address_questions_jservice = f'https://jservice.io/api/random?count={questions_quantity}'

    questions_in_jservice = get(address_questions_jservice).json()

    return questions_in_jservice

def save_question_in_db(question: dict) -> None:
    """Сохранение данных вопроса в базу данных"""

    add_question = Question_answer(
        id_question = question['id'],
        text_question = question['question'],
        text_answer = question['answer'],
        )

    db.session.add(add_question)
    db.session.commit()

def process_check_to_valid_and_save_questions(data_questions_num: int) -> None:
    try:
        questions_in_jservice = get_question_jservice(data_questions_num)
    except:
        return print('Сайт jservice.io не доступен')
    for question in questions_in_jservice:
        """
            Если в БД имеется такой же вопрос, к публичному API с викторинами выполняются дополнительные запросы до тех пор
            пока не будет получен уникальный вопрос для викторины.
        """

        questions_in_db = Question_answer.query.filter_by(id_question=question['id']).first()

        if questions_in_db != None:
            questions_in_jservice.append(get_question_jservice())

        else:
            save_question_in_db(question)


from flask_restful import Resource
from pymongo import MongoClient

from api.starta_flode import MONGODB_CONNECTION
from statistics import median

class Statistik(Resource):
    def getFlodeStatistics(self, subjects, flode=None):
        allData = [r for r in subjects.find(flode and {'flode': flode} or {})]
        tid = [int(x['end_at'] - x['start_at']) for x in allData if x['end_at'] is not None]
        retobj = {
            'started': len(allData),
            'ended': len([x for x in allData if x['end_at'] is not None]),
            'median': tid and median(tid) or 0
        }
        return retobj

    def getFlodeB(self):
        pass

    def get(self):
        mongo_client = MongoClient(MONGODB_CONNECTION)
        # if not found
        db = mongo_client['heroku_ssj5qmzb']
        subjects = db.subjects
        statistics = {
            'total': self.getFlodeStatistics(subjects),
            'A': self.getFlodeStatistics(subjects, 'A'),
            'B': self.getFlodeStatistics(subjects, 'B')
        }

        return statistics, 200


from django.shortcuts import render, HttpResponse
from .forms import UserForm
from sms_alert.models import User

def index(request):

    if request.method == 'POST':
        name = request.POST.get('name')
        phone_number = request.POST.get('phone_number')
        country = request.POST.get('country')
        user_form = UserForm({
            'timezone': 'America/Los_Angeles',
        })
        time_zone = user_form.cleaned_data['timezone']

        temp = User(
            name = name,
            time_zone = time_zone,
            phone_number = phone_number,
            country = country
        )

        return HttpResponse("
Successfully entered
")

    else:
        form = UserForm()
        return render(request, 'sms_alert/index.html', {'form': form})

def next(request):

    if request.method == 'POST':
        form = UserForm(request.POST)
        time_zone = request.POST.get('timezone')
        name = request.POST.get('name')
        phone_number = request.POST.get('phone_number')
        country = request.POST.get('country')
        return render(request, 'sms_alert/next.html', {
            'name' : name,
            'phone_number' : phone_number,
            'country' : country,
            'time_zone' : time_zone,
        })

    else:
        return render(request, 'sms_alert/next.html')

def form_save(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        phone_number = request.POST.get('phone_number')
        country = request.POST.get('country')
        time_zone = request.POST.get('time_zone')
        awake_time = request.POST.get('awake_time')
        sleep_time = request.POST.get('sleep_time')
        print("-----------------------------------------------------", request.POST)
        temp = User(
            name=name,
            sleep_time=sleep_time,
            wake_time=awake_time,
            time_zone=time_zone,
            phone_number=phone_number,
            country=country
        )
        temp.save()
        form = UserForm()
        return render(request, 'sms_alert/index.html', {'form': form})
    else:
        form = UserForm()
        return render(request, 'sms_alert/index.html', {'form': form})


import glob
import os
import subprocess
import tempfile

rescompilerPath = 'build-desktop/bin/rescompiler.exe'
containerPath = 'data/build/game.dat'

excludeFiles = glob.glob('data/build/**/*', recursive=True) + glob.glob('data/sources/**/*', recursive=True)
dataFiles = [os.path.abspath(f) for f in glob.glob('data/**/*', recursive=True) if (not f in excludeFiles and not os.path.isdir(f))]
listFile = tempfile.NamedTemporaryFile('w+', delete=False)

print(f'Writing {len(dataFiles)} files to {listFile.name}')
listFile.write(os.linesep.join(dataFiles))
listFile.flush()
listFile.close()

resArgs = [
    os.path.abspath(rescompilerPath),
    listFile.name,
    os.path.abspath(containerPath)
]

returnCode = subprocess.call(resArgs)
print(f'Completed with code {returnCode}')


# https://blog.csdn.net/qq_32149483/article/details/112056845
import imgaug as ia
from imgaug import augmenters as iaa
import numpy as np
from typing import Tuple, List

class Img_Aug:
    def __init__(self, prob=0.2, crop=True, blur=True, superpixel=True,
                 space_trans=True, sharpen=True, emboss=True,
                 edge_detect=True, noise=True, dropout=True):
        self.prob = prob
        self.crop = crop
        self.blur = blur
        self.superpixel = superpixel
        self.space_trans = space_trans
        self.sharpen = sharpen
        self.emboss = emboss
        self.edge_detect = edge_detect
        self.noise = noise
        self.dropout = dropout

    def __call__(self):
        operations = [iaa.Affine(translate_px={"x": 15, "y": 15},
                                 scale=(0.8, 0.8),
                                 rotate=(-5, 5))]
        if self.crop:
            operations.append(iaa.Crop(percent=(0, self.prob)))
        if self.blur:
            operations.append(iaa.OneOf([iaa.GaussianBlur((0, 3.)),
                                        iaa.AverageBlur(k=(2, 7)),
                                        iaa.MedianBlur(k=(3, 11))]))
        if self.superpixel:
            operations.append(iaa.Sometimes(0.5, iaa.Superpixels(p_replace=(0.005, 0.5),
                                                                 n_segments=(16, 28))))
        if self.space_trans:
            operations.append(iaa.Sequential([iaa.ChangeColorspace(from_colorspace="RGB", to_colorspace="HSV"),
                                              iaa.WithChannels(0, iaa.Add((50, 100))),
                                              iaa.ChangeColorspace(from_colorspace="HSV", to_colorspace="RGB")]))
        if self.sharpen:
            operations.append(iaa.Sharpen(alpha=(0., 1.), lightness=(0.75, 1.5)))
        if self.emboss:
            operations.append(iaa.Emboss(alpha=(0., 1.), strength=(0., 2.)))
        if self.edge_detect:
            operations.append(iaa.OneOf([iaa.EdgeDetect(alpha=(0., 0.75)),
                                        iaa.DirectedEdgeDetect(alpha=(0., 0.75), direction=(0, 1))]))
        if self.noise:
            operations.append(iaa.AdditiveGaussianNoise(scale=(0, 128), per_channel=0.5))
        if self.dropout:
            operations.append(iaa.Dropout(p=(0.01, 0.1), per_channel=0.5))
        lenTrans = len(operations)
        seq = iaa.Sequential([iaa.SomeOf(min(5, lenTrans), operations, random_order=True)])
        return seq

class Augmentation:
    def __init__(self, seq):
        self.seq = seq

    def __call__(self, img, bbox=None, label=None, mode="x1y1x2y2") -> Tuple[np.ndarray, dict]:
        # seq_det = self.seq.to_deterministic()
        # bbox should be in x1x2y1y2 format
        if bbox is None:
            return self.seq(image=img)
        if mode == "x1y1x2y2":
            x1, y1, x2, y2 = bbox
        else:
            x1, y1, x2, y2 = bbox[0], bbox[1], bbox[0]+bbox[2], bbox[1]+bbox[3]
        bbs = ia.BoundingBoxesOnImage([ia.BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2, label=label)], shape=img.shape)
        image_aug, bbox_aug = self.seq(image=img, bounding_boxes=bbs)
        location = [bbox_aug[0].x1, bbox_aug[0].y1, bbox_aug[0].x2, bbox_aug[0].y2]
        label = bbox_aug[0].label
        shape = bbox_aug.shape
        bbox_info = {"location": location, "label": label, "shape": shape}
        return image_aug, bbox_info

if __name__ == "__main__":
    seq = Img_Aug()()
    import cv2
    img = cv2.imread("mchar_train/000000.png")
    aug_img, aug_bbox = Augmentation(seq)(img, bbox=[0.5, 0.5, 0.5, 0.5], label=0)


from src.algo import rpq
from src import Config
from tests.simple_test import simple_test

import json
import pytest
import os
import random

MAIN_TEST_DIR = \
        os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tests')
TEST_DIRS = list(map(
        lambda dir_name: os.path.join(MAIN_TEST_DIR, dir_name),
        os.listdir(MAIN_TEST_DIR)
    ))

@pytest.mark.parametrize('test_dir', TEST_DIRS)
def test_small_test(test_dir):
    simple_test(
            test_dir,
            [
                ('config.json', 'config'),
                ('data_base.txt', 'data_base'),
                ('query.txt', 'regular_query')
            ],
            rpq,
            lambda j: set(map(tuple, j))
        )

@pytest.mark.parametrize(('max_count_vertexes', 'regex'),
            [
                (max_count_vertexes, regex)
                for max_count_vertexes in [10, 40, 160]
                for regex in ['a | b', 'a* (b | $)', '(a | b) . (b* | a b)']
            ])
                #for regex in ['a | b']])
def test_big_test(max_count_vertexes, regex):
    count_edges = random.randint(1, max_count_vertexes ** 2)
    I = [random.randint(0, max_count_vertexes - 1) for _ in range(count_edges)]
    J = [random.randint(0, max_count_vertexes - 1) for _ in range(count_edges)]
    V = [random.choice(['a', 'b', 'c']) for _ in range(count_edges)]
    count_vertexes = max(I + J) + 1
    max_count_input_vertexes = random.randint(1, count_vertexes)
    max_count_output_vertexes = random.randint(1, count_vertexes)
    input_vertexes = list({random.randint(0, count_vertexes - 1)
        for _ in range(max_count_input_vertexes)})
    output_vertexes = list({random.randint(0, count_vertexes - 1)
        for _ in range(max_count_output_vertexes)})
    config = Config.from_dict(
        {
            'data_base_lists': [I, J, V],
            'regular_query_regex': regex,
            'input_vertexes': input_vertexes,
            'output_vertexes': output_vertexes
        }
      )
    result = rpq(config)
    for V_from, _ in result:
        assert V_from in input_vertexes
    for _, V_to in result:
        assert V_to in output_vertexes


from .models import (
    ActionList,
    Action,
    ExpectList,
    Expect,
    ExportDict,
    SkipIfList,
    SkipIf,
)
from .validators import validate_boolean, validate_conditionals,validate_export_dict

class ArgType:
    type = lambda value: value  # noqa
    value = None

class UsingArgType(ArgType):
    type = str

    def __call__(self, value) -> str:
        """
        1. Check if value is a string
        2. Check if value is a namespace.collection
        3. Check if namespace.collection is installed
        """
        if not isinstance(value, str):
            raise ValueError(
                f"`using` must be a string, got {type(value).__name__}"
            )
        if len(value.split(".")) != 2:
            raise ValueError(
                f"`using` must be a <namespace>.<collection>, got {value}"
            )
        # TODO: Check if collection is installed
        self.value = self.type(value)
        return value

class ActionsArgType(ArgType):
    type = ActionList

    def __call__(self, actions) -> list:
        """
        1. Check if actions is a list
        2. For each action in actions
            2.1 Check if action is a dict or a str
            2.2 Create the action instance
            2.3 Add Action to return value
        """
        value = []

        if not isinstance(actions, list):
            msg = f"`actions` must be a list, got {type(actions).__name__}"
            if isinstance(actions, dict):
                msg += (
                    f" did you mean `- {list(actions.keys())[0]}:` ?"
                )
            if isinstance(actions, str):
                msg += f" did you mean `[{actions}]` or `- {actions}` ?"
            raise ValueError(msg)

        _valid = ", ".join(Action.Config.valid_ansible_arguments)
        for action in actions:
            if isinstance(action, dict):
                # ensure this is a single key-value pair
                if len(action) != 1:
                    raise ValueError(
                        f"`action` must be single key dictionary,"
                        f" got {action.keys()}"
                    )

                action_key = list(action.keys())[0]
                if "." not in action_key:
                    raise ValueError(
                        f"`action` must be <module>.<action>, got {action_key}"
                    )
                module_name, action_name = action_key.split(".")
                action_value = action[action_key]
                if not isinstance(action_value, dict):
                    raise ValueError(
                        f"`{action_key}:` takes"
                        " {param: value, ...},"
                        f" where `param` is one of {_valid}"
                        f", but got {action_value}"
                    )

                try:
                    action = Action(
                        module_name=module_name,
                        action_name=action_name,
                        **action_value,
                    )
                except TypeError as e:
                    _invalid = set(action_value) - set(
                        Action.Config.valid_ansible_arguments
                    )

                    raise ValueError(
                        f"Unsupported parameters"
                        f" for `{action_key}: {_invalid}`"
                        f". Supported parameters include: {_valid}"
                    ) from e

            elif isinstance(action, str):
                if "." not in action:
                    raise ValueError(
                        f"`action` must be <module>.<action>, got {action}"
                    )
                module, action_name = action.split(".")
                action = Action(module_name=module, action_name=action_name)
            else:
                raise ValueError(
                    f"`actions` must be a list of dicts"
                    f" or strings, got {type(action).__name__}"
                )
            value.append(action)

        self.value = self.type(value)
        return actions

class SkipifArgType(ArgType):
    type = SkipIfList

    def __call__(self, value) -> str:
        self.value = validate_conditionals(value, "skipif", SkipIf, self.type)
        return value

class ExpectArgType(ArgType):
    type = ExpectList

    def __call__(self, value) -> list:
        self.value = validate_conditionals(value, "expect", Expect, self.type)
        return value

class ExportArgType(ArgType):
    type = ExportDict

    def __call__(self, value) -> dict:
        self.value = self.type(validate_export_dict(value))
        return value

class RequiredArgType(ArgType):

    def __call__(self, value) -> bool:
        self.value = validate_boolean(value)
        return value


import wolframalpha
import pprint
import json

class WolframAlpha:

	def __init__(self, appId):
		self.__client = wolframalpha.Client(appId)
		self.__prettyPrinter = pprint.PrettyPrinter()
		self.__pp = self.__prettyPrinter.pprint

	def question(self, query):

		if len(query.strip()) == 0:
			return "Ask me a question."

		try:
			response = self.__client.query(query.strip())
		except Exception:
			return "Help! Tell SirDavidLudwig or Raine to fix me!"

		if response['@success'] == 'true':
			# Print the response to the terminal for debugging purposes
			try:
				json.dumps(response, indent=4)
			except Exception as e:
				print(response)

			# Search for primary pod
			for pod in response['pod']:
				if "@primary" in pod and pod["@primary"] == "true":
					if type(pod['subpod']) == list:
						return pod['subpod'][0]['plaintext']
					else:
						return pod['subpod']['plaintext']

			print("No primary found")
			return response['pod'][0]['subpod']['plaintext']

		elif '@timedout' in response:
			return "I cannot tell unfortunately."

		return "I'm sorry, I don't understand what you are asking me here."


from email.mime import base
from AES_encipher import CTR_Mode
from OAEP import *
from RSA_key_gen import AES_key_gen
from utility import bit_size
import base64
from math import ceil

class data_msg:
    """ Class that holds the values used in the data transfer. """

    def __init__(self, signature: bytes = None, msg: bytes = None, symmetric_key: bytes = None, nonce_val: int = None):
        self.signature = signature
        self.msg = msg
        self.symmetric_key = symmetric_key
        self.nonce_val = nonce_val # Used in the AES_CTR mode

    def from_base64(self, base64_str: str):
        dm = data_msg()

        d = vars(dm)
        lines = base64_str.split('\n')
        idx = 0

        for k in d.keys():
            d[k] = lines[idx]
            d[k] = base64.b64decode(d[k])
            idx += 1
        self.__dict__ = d

        self.nonce_val = int.from_bytes(self.nonce_val, byteorder='big')

    def int_to_byte(self, val):

        if type(val) != type(bytes()):
            l = ceil(bit_size(val) / 8)
            val = val.to_bytes(length=l, byteorder='big')

        return val

    def get_base64_encode(self):

        self.nonce_val = self.int_to_byte(self.nonce_val)

        my_vars = vars(self)

        result = str()
        for v in my_vars.values():
            if v == None:
                result += base64.b64encode(bytes()).decode('utf-8') + '\n'
            else:
                result += base64.b64encode(v).decode('utf-8') + '\n'

        self.nonce_val = int.from_bytes(self.nonce_val, byteorder='big')
        return result

class AES_message_cipher:
    """ Does the ciphering of a message, in bytes, of any size. """

    def __init__(self, key_bit_size = 128):
        self.__block_count__ = 16 # the block bytes count

        kg = AES_key_gen(key_bit_size)
        self.key = kg.generate_key()
        self.key = self.key.to_bytes(length=kg.bit_size // 8, byteorder='big')

    def encrypt(self, msg: bytes):

        blocks_count = ceil(len(msg) / self.__block_count__)

        dm = data_msg()
        cipher_blocks = []

        cipher = CTR_Mode(self.key)

        for i in range(blocks_count):

            idx = i * self.__block_count__
            if i == blocks_count - 1:
                b = msg[idx:]
            else:
                b = msg[idx:idx + self.__block_count__]

            b = cipher.encrypt_block(data=b)
            cipher_blocks += (b)

        cipher_blocks = bytes(cipher_blocks)
        dm.msg = cipher_blocks
        dm.symmetric_key = self.key
        dm.nonce_val = cipher.nonce

        return dm

class AES_message_decipher:
    """ Deciphers a message, in bytes, of any size. """

    def __init__(self):
        self.__block_count__ = 16 # the block bytes count

    def decrypt(self, dm: data_msg):

        blocks_count = ceil(len(dm.msg) / self.__block_count__)
        cipher = CTR_Mode(dm.symmetric_key, nonce=dm.nonce_val)

        msg = []
        for i in range(blocks_count):
            idx = i * self.__block_count__

            if i == blocks_count - 1:
                b = dm.msg[idx:]
            else:
                b = dm.msg[idx:idx + self.__block_count__]

            b = cipher.decrypt_block(b)
            msg += b

        msg = bytes(msg)

        return msg


import argparse

from . import test_data
from .. import music
from .. import key_recognition

def parse_args():
    parser = argparse.ArgumentParser(
        description='Test getting key from sounds')
    parser.add_argument('--verbose_factor_threshold', '-Vft',
                        required=False,
                        help='Color red and print full mismatches list for tests with match factor lesser than VERBOSE_FACTOR_THRESHOLD',  # noqa
                        type=float)
    parser.add_argument('--verbose_duration_threshold', '-Vdt',
                        required=False,
                        help='Print only mismatches with duration greater than VERBOSE_DURATION_THRESHOLD',  # noqa
                        type=float)
    args = parser.parse_args()
    return args

class Mismatch(music.Key):
    def __init__(self, note_computed, note_model, kind_computed, kind_model,
                 timestamp, duration_ms):
        super().__init__(note_computed, timestamp, duration_ms, kind_computed)
        self.note_model = note_model
        self.kind_model = kind_model

    @property
    def key_model(self):
        if self.note_model is None:
            symbol = 'None'
        else:
            symbols = ['C', 'C#', 'D', 'D#', 'E',
                       'F', 'F#', 'G', 'G#', 'A', 'A#', 'H']
            symbol = symbols[self.note_model]
        return f"{symbol}-{self.kind_model}"

    def __str__(self):
        return f"{round(self.timestamp, 3)} - {round(self.end_timestamp, 3)}: Expected {self.key_model}, got {super().__str__()}"  # noqa

def run_tests():
    verbose_factor_threshold = parse_args().verbose_factor_threshold
    verbose_duration_threshold = parse_args().verbose_duration_threshold
    if verbose_factor_threshold is None:
        verbose_factor_threshold = 0.5
    if verbose_duration_threshold is None:
        verbose_duration_threshold = 0.1
    tests = test_data.get_all_test_models()
    print("-----------TONATIONS TEST-----------------")
    for test in tests:
        key = key_recognition.get_key(test.sounds)
        match = key == test.key
        if match:
            color = '\033[92m'
            print(f"{test.file_path}: {color} {key} matches {test.key} \033[0m")  # noqa
        else:
            color = '\033[91m'
            print(f"{test.file_path}: {color}{key} doesn't match {test.key} \033[0m")  # noqa