import time
import threading
import logging
import traceback
import datetime
import os
import sys
import re
import robotparser as rp
import numpy as np
import random

import util
import decide
import queries
from conn import connect

class Crawler:
    '''
    Abstract class for crawling a news source.
    '''

    ########################
    #                      #
    #  Abstract functions  #
    #                      #
    ########################

    def sleep(self):
        '''
        Abstract function for waiting between requests.
        Can add additional functionality, such as random sleep times.
        '''
        return NotImplemented

    def is_article(self, url):
        '''
        Abstract function for determining if a url is an article or not.
        Returns True if the url is an article, false otherwise.
        '''
        return NotImplemented

    def extract_date_from_url(self, url):
        '''
        Abstract function for parsing a date from a url.
        Currently crawler only works for sources with dates in their urls.
        Takes a url as a string, returns a datetime.date object.
        '''
        return NotImplemented

    ############################
    #                          #
    #  Non-Abstract functions  #
    #                          #
    ############################

    def __init__(self, base_url_string):
        '''
        Not abstract.
        Parameters:
            base_url_string, a string representing the base url for a source (eg, foxnews.com)
            article_regex, a string representing a regex which only matches for articles
        '''
        self.base_url_string = base_url_string
        self.initialize_robots()

    def initialize_robots(self):
        '''
        Not abstract.
        Initializes a robot parser for the crawler.
        Use self.robot_parser.can_fetch("*", url) to decide if allowed or not.
        '''
        base_url_string = self.base_url_string
        robot_url = util.robots_url(base_url_string)
        robot_parser = rp.RobotFileParser()
        robot_parser.set_url(robot_url)
        robot_parser.read()
        self.robot_parser = robot_parser

    def decide_next_visit(self, conn, crawl_id, bad_urls):
        '''
        Not abstract.
        Decides which url to visit next.
        Returns a dictionary visit_url with two keys
            visit_url['id'] - database if of the url to visit
            visit_url['url'] - string representation of the url to visit
        Returns None if no urls left to visit.
        Strategy is to visit anything not visited this crawl, with the following priority:
            1) base url
            2) internal pages linked from the base url
            2) articles which haven't been visited yet, sorted by date
        Currently only implemented 1) and 2)
        '''
        base_url_string = self.base_url_string

        # strategy 1 - visit base url if not visited yet (ignore previous crawls)
        base_url_id = queries.insert_url(conn, base_url_string)
        base_url = {'id': base_url_id, 'url': base_url_string}
        visited_base = decide.visited_base_url(conn, crawl_id)
        if not visited_base:
            return base_url

        # strategy 2 - visit any urls linked by the base url that haven't been visited yet (ignore previous crawls)
        urls = decide.find_unvisited_links_from_base(conn, crawl_id, base_url_string)
        urls = filter(lambda url: self.robot_parser.can_fetch("*", url['url']), urls)
        urls = filter(lambda url: url['id'] not in bad_urls, urls)
        if len(urls) > 0:
            visit_url = random.choice(urls)
            return visit_url

        # strategy 3 - visit any articles not visited yet (including previous crawls), starting with the most recent
        urls = decide.find_unvisited_internal_urls(conn, base_url_string)
        urls = filter(lambda url: self.robot_parser.can_fetch("*", url['url']), urls)
        urls = filter(lambda url: url['id'] not in bad_urls, urls)
        urls = filter(lambda url: self.is_article(url['url']), urls)
        if len(urls) > 0:
            dates = map(lambda url: self.extract_date_from_url(url['url']), urls)
            reverse_sorted_dates = np.argsort(np.array(dates))[::-1]
            last_date_index = reverse_sorted_dates[0]
            visit_url = urls[last_date_index]
            return visit_url

        return None

    def crawl(self):
        '''
        Not abstract. Begins a crawl.
        Crawls until MAX_VISITS is reached, unless:
            - self.decide_next_visit(conn) returns None
            - Five exceptions in a row
        '''
        # initialize variables
        visits = 0
        MAX_VISITS = 1000 # so we don't just keep crawling forever
        bad_urls = set() # when a url doesn't work, add url_id to bad_urls, ignore in future
        error_count = 0
        base_url_string = self.base_url_string
        conn = connect()

        # initialize logging
        initialize_logging(base_url_string)
        start_time = time.time()
        logging.info('STARTING CRAWL AT TIME: {0}'.format(util.time_string(start_time)))

        # initlialize database for this crawl
        base_url_id = queries.insert_url(conn, base_url_string)
        source_id = queries.insert_source(conn, base_url_string)
        crawl_id = queries.insert_crawl(conn, base_url_string)

        while True:
            if error_count == 5:
                logging.error('Too many exceptions in a row, exiting.')
                break
            visit_url = self.decide_next_visit(conn, crawl_id, bad_urls)
            if visit_url is None:
                logging.info('Finished crawling, no more urls to visit.')
                break
            try:
                logging.info('Visiting {}'.format(visit_url['url']))
                self.visit(conn, crawl_id, source_id, visit_url)
                error_count = 0
            except Exception as e:
                logging.error('Error when downloading {0}'.format(visit_url['url']))
                logging.error(traceback.format_exc())
                bad_urls.add(visit_url['id'])
                error_count += 1
            visits += 1
            if visits == MAX_VISITS:
                logging.info('Finished crawling, reached max visits of {}'.format(MAX_VISITS))
                break
            self.sleep()

    def visit(self, conn, crawl_id, source_id, visit_url):
        '''
        Not abstract. Visits a url during a crawl.
        Inserts all relevant information to the database for a single visit.
        Inserts article information if the url matches the article regex.
        '''
        visit_url_id = visit_url['id']
        visit_url_string = visit_url['url']

        base_url_string = self.base_url_string
        html = util.download_html(visit_url_string)
        found_links = util.extract_links(html, base_url_string)
        visit_id = queries.insert_visit(conn, crawl_id, visit_url_id)
        new_url_ids = queries.insert_urls(conn, found_links)
        queries.insert_links(conn, visit_id, new_url_ids)

        if self.is_article(visit_url_string):
            article = util.extract_article(html, visit_url_string)
            article_title = article.title
            article_text = article.text
            article_date = self.extract_date_from_url(visit_url_string)
            queries.insert_article(conn, visit_url_id, article_title, article_text, article_date, source_id)

def initialize_logging(base_url):
    log_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'logs')
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    source_str = util.extract_source(base_url)
    log_filename = 'LOG_{0}.log'.format(source_str)
    log_path = os.path.join(log_dir, log_filename)
    logging.basicConfig(filename=log_path, filemode='a', level=logging.INFO)
    stderrLogger=logging.StreamHandler()
    stderrLogger.setFormatter(logging.Formatter(logging.BASIC_FORMAT))
    logging.getLogger().addHandler(stderrLogger)
    sys.excepthook = log_unchecked_exception

def log_unchecked_exception(exctype, value, tb):
    traceback.print_tb(tb)
    log_str = '''
UNCHECKED EXCEPTION
    Type: {}
    Value: {}
    Traceback: {}'''.format(exctype, value, traceback.print_tb(tb))
    logging.error(log_str)


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
import time

driver = webdriver.Firefox()

driver.get('https://github.com/Vidoosh/Image-colorizer')
time.sleep(2)

code = driver.find_element(By.CSS_SELECTOR, '#repo-content-pjax-container > div > div > div.Layout.Layout--flowRow-until-md.Layout--sidebarPosition-end.Layout--sidebarPosition-flowRow-end > div.Layout-main > div.file-navigation.mb-3.d-flex.flex-items-start > span.d-none.d-md-flex.ml-2 > get-repo > feature-callout')
code.click()
time.sleep(1)
try:
    download = driver.find_element(By.CSS_SELECTOR, '#local-panel > ul > li:nth-child(3) > a')
    download.click()
    print("Download initiated successfully")

except NoSuchElementException as e:
    print("Download button not found")

finally:
    driver.quit()


# 数据预处理 阴影过滤
#yangzhen
#2020.4.13
#translate from matlab
"""get the shadow proportion from images
   of remote sensing"""
import numpy as np
import cv2
import os
import json
from shutil import copyfile
import argparse

def cv_imread(file_path):
    cv_img=cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),1)
    return cv_img

def cv_imwrite(filepath,img):
    cv2.imencode(".png",img)[1].tofile(filepath)

def standard(data):
    '''影像文件标准化
       输入单通道影像
       输出标准化后单通道影像'''
    mdata = data.copy()
    irow, icol = mdata.shape[0:2]
    mdata = np.reshape(mdata, [irow*icol, 1])
    temp1 = mdata - np.min(data)
    result = temp1/(np.max(data)-np.min(data))
    result = np.reshape(result, [irow, icol])
    np.seterr(divide='ignore', invalid='ignore')
    return result

def GetLight(img):
    '''计算人眼视觉特性亮度'''
    mimg = img.copy()
    B = mimg[:,:,0]
    G = mimg[:,:,1]
    R = mimg[:,:,2]
    result = 0.04*R+0.5*G+0.46*B
    return result

def GetColor(img):
    '''色度空间归一化'''
    mimg = img.copy()
    misc = mimg[:,:,0]+mimg[:,:,1]+mimg[:,:,2]
    misc[misc == 0] = 0.0000001
    mimg[:,:,0] = img[:,:,0]/misc
    mimg[:,:,1] = img[:,:,1]/misc
    result = np.abs(mimg - img)
    result = (result[:,:,0]+result[:,:,1])/2
    return result

def GetVege(img):
    '''获取植被特征'''
    mimg = img.copy()
    B = mimg[:,:,0]
    G = mimg[:,:,1]
    R = mimg[:,:,2]
    result = G-np.minimum(R, B)
    result[result<0] = 0
    return result

def GetLDV(idist, ilight, ivege):
    '''总决策'''
    idist = standard(idist)
    ilight = standard(ilight)
    ivege = standard(ivege)
    result = idist-ilight-ivege
    result[result<0]=0
    return result

def FinalTrare(img):
    '''结果后处理'''
    mimg = img.copy()
    mimg = np.uint8(standard(mimg)*255)
    T, result = cv2.threshold(mimg, 0, 255, cv2.THRESH_OTSU)
    result = cv2.medianBlur(result, 7)
    return result

def ShadowsProportion(path:{}):
    """
       阴影提取
       @@path: {},
       @path[0] 待检测阴影的影像
       @path[1] 待检测阴影的影像
       @path[2]  阴影比例阈值
       ps: 当两张影像过大时会进行分块
    """
    File_in = path[0]
    File_out = path[1]
    T = path[2]
    mpath = path[3]
    File_out2=path[4]
    if not os.path.exists(File_out):
        os.makedirs(File_out)
    if not os.path.exists(File_out2):
        os.makedirs(File_out2)
    #开始检测
    namelist=[]
    for filename in os.listdir(File_in):
        if not filename.find('.png') == -1:
            namelist.append(filename)
    n = len(namelist)
    fid = open('ShadowsProportion.txt', 'w')
    for i in range(n):
        filenamein = os.path.join(File_in, namelist[i])
        img = cv_imread(filenamein)
        #获取阴影
        img1 = img.astype(np.float)
        img1[:,:,0] = standard(img[:,:,0])
        img1[:,:,1] = standard(img[:,:,1])
        img1[:,:,2] = standard(img[:,:,2])
        idist = GetColor(img1)
        ilight = GetLight(img1)
        ivege = GetVege(img1)
        final = GetLDV(idist, ilight, ivege)
        shadow = FinalTrare(final)
        shadow = shadow/255
        #计算阴影比例并保存比例值
        S = shadow.size
        s = np.sum(sum(shadow))
        iratio = s/S
        fid.write(namelist[i] + ',' + str('%.3f' % iratio) + '\n')
        #保存阴影比例小于阈值的图片
        filenameout = os.path.join(File_out, namelist[i])
        filenameout2 = os.path.join(File_out2, namelist[i])
        mapout = mpath.replace('rawdata','noshade')
        if not os.path.exists(mapout):
            os.makedirs(mapout)
        if iratio < T:
            cv_imwrite(filenameout, img)
            copyfile(os.path.join(mpath,namelist[i]),(os.path.join(mapout,namelist[i])))
        else :
            cv_imwrite(filenameout2, img)
    fid.close()

def takejson(getjson):
    json1 = json.loads(getjson)

    path = {}
    path[0] = json1['rpath']
    path[1] = path[0].replace('rawdata','noshade')
    path[2] = json1['shadowProportion']
    path[3]=json1['mpath']
    path[4]=path[0].replace('rawdata','withshade')
    #print (path)
    ShadowsProportion(path)

    print ('Shadow filtering Completed')

if __name__ == "__main__":
    #获取输入图片路径，阴影比例阈值，输出图片路径
    # File_in = input('Please input the data file name:')
    # T = float(input('Please input the  threshold value:'))
    # File_out = input('Please input the out-img filename:')

    parser = argparse.ArgumentParser()
    parser.add_argument('--input_json', type=str, help='输入json字符串')
    args = parser.parse_args()

    # json1={'rpath':r'F:\Chicago2\metadata\谷歌影像无标注\14\14_14aligned','mpath':r'F:\Chicago2\metadata\谷歌地图无标注\14\14_14aligned','shadowProportion':0.2}
    # getjson=json.dumps(json1)
    takejson(args.input_json)


import os

import h5py
import numpy as np

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

if 'KAGGLE_BASE_URL' in os.environ:
    challenge = 'g2net-detecting-continuous-gravitational-waves'
    PATH_TO_TEST_FOLDER = os.path.join('/kaggle', 'input', challenge, 'test')
    PATH_TO_TRAIN_FOLDER = os.path.join('/kaggle', 'input', challenge, 'train')
    PATH_TO_LABEL_FILE = os.path.join('/kaggle', 'input', challenge, 'train_labels.csv')
    PATH_TO_MODEL_FOLDER = os.path.join('/kaggle', 'input', 'models')
    PATH_TO_LOG_FOLDER = os.path.join('/kaggle', 'temp', 'logs')
    PATH_TO_CACHE_FOLDER = os.path.join('/kaggle', 'working', 'cache')
    PATH_TO_SIGNAL_FOLDER = os.path.join('/kaggle', 'working', 'signal')
    PATH_TO_NOISE_FOLDER = os.path.join('/kaggle', 'working', 'noise')
    PATH_TO_DYNAMIC_NOISE_FOLDER = os.path.join(PATH_TO_NOISE_FOLDER, 'dynamic')
    PATH_TO_STATIC_NOISE_FOLDER = os.path.join(PATH_TO_NOISE_FOLDER, 'static')
    PATH_TO_SOURCE_FOLDER = os.path.join('/kaggle', 'working', 'src')
else:
    PATH_TO_TEST_FOLDER = os.path.join(os.getcwd(), 'test_data')
    PATH_TO_TRAIN_FOLDER = os.path.join(os.getcwd(), 'train_data')
    PATH_TO_MODEL_FOLDER = os.path.join(os.getcwd(), 'models_saved')
    PATH_TO_LOG_FOLDER = os.path.join(os.getcwd(), 'logs')
    PATH_TO_CACHE_FOLDER = os.path.join(os.getcwd(), 'cache')
    PATH_TO_LABEL_FILE = os.path.join(os.getcwd(), 'train_labels.csv')
    PATH_TO_SIGNAL_FOLDER = os.path.join(os.getcwd(), 'signal')
    PATH_TO_NOISE_FOLDER = os.path.join(os.getcwd(), 'noise')
    PATH_TO_DYNAMIC_NOISE_FOLDER = os.path.join(PATH_TO_NOISE_FOLDER, 'dynamic')
    PATH_TO_STATIC_NOISE_FOLDER = os.path.join(PATH_TO_NOISE_FOLDER, 'static')
    PATH_TO_TMP_FOLDER = os.path.join(os.getcwd(), 'tmp')
    PATH_TO_SOURCE_FOLDER = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))

# setup
if not os.path.isdir(PATH_TO_TRAIN_FOLDER):
    os.makedirs(PATH_TO_TRAIN_FOLDER)
if not os.path.isdir(PATH_TO_TEST_FOLDER):
    os.makedirs(PATH_TO_TEST_FOLDER)
if not os.path.isdir(PATH_TO_MODEL_FOLDER):
    os.makedirs(PATH_TO_MODEL_FOLDER)
if not os.path.isdir(PATH_TO_LOG_FOLDER):
    os.makedirs(PATH_TO_LOG_FOLDER)
if not os.path.isdir(PATH_TO_CACHE_FOLDER):
    os.makedirs(PATH_TO_CACHE_FOLDER)
if not os.path.isdir(PATH_TO_NOISE_FOLDER):
    os.makedirs(PATH_TO_NOISE_FOLDER)
if not os.path.isdir(PATH_TO_SIGNAL_FOLDER):
    os.makedirs(PATH_TO_SIGNAL_FOLDER)
if not os.path.isdir(PATH_TO_DYNAMIC_NOISE_FOLDER):
    os.makedirs(PATH_TO_DYNAMIC_NOISE_FOLDER)
if not os.path.isdir(PATH_TO_STATIC_NOISE_FOLDER):
    os.makedirs(PATH_TO_STATIC_NOISE_FOLDER)
if not os.path.isdir(PATH_TO_TMP_FOLDER):
    os.makedirs(PATH_TO_TMP_FOLDER)

if 'IS_CHARLIE' in os.environ:
    print('We are on Charlie')
    os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
    os.environ['CUDA_VISIBLE_DEVICES'] = '2'
    #os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:2000"

def print_red(*text):
    print(f'{bcolors.FAIL}{" ".join([str(t) for t in text])}{bcolors.ENDC}')

def print_blue(*text):
    print(f'{bcolors.OKCYAN}{" ".join([str(t) for t in text])}{bcolors.ENDC}')

def print_green(*text):
    print(f'{bcolors.OKGREEN}{" ".join([str(t) for t in text])}{bcolors.ENDC}')

def print_yellow(*text):
    print(f'{bcolors.WARNING}{" ".join([str(t) for t in text])}{bcolors.ENDC}')

def open_hdf5_file(path_to_file):
    result = {}
    with h5py.File(path_to_file, 'r') as hd5_file:
        base_key = list(hd5_file.keys())[0]
        result['base_key'] = base_key
        result['frequencies'] = np.array(hd5_file[f'{base_key}/frequency_Hz'])
        result['h1'] = {}
        result['l1'] = {}
        result['h1']['amplitudes'] = np.array(hd5_file[f'{base_key}/H1/SFTs'])
        result['l1']['amplitudes'] = np.array(hd5_file[f'{base_key}/L1/SFTs'])
        result['h1']['timestamps'] = np.array(hd5_file[f'{base_key}/H1/timestamps_GPS'])
        result['l1']['timestamps'] = np.array(hd5_file[f'{base_key}/L1/timestamps_GPS'])

    return result

def get_df_dynamic_noise():
    assert len(os.listdir(PATH_TO_DYNAMIC_NOISE_FOLDER)) != 0, 'There must be data in noise folder'
    return [os.path.join(PATH_TO_DYNAMIC_NOISE_FOLDER, p) for p in os.listdir(PATH_TO_DYNAMIC_NOISE_FOLDER)]

def get_df_static_noise():
    assert len(os.listdir(PATH_TO_STATIC_NOISE_FOLDER)) != 0, 'There must be data in static_noise folder'
    return [os.path.join(PATH_TO_STATIC_NOISE_FOLDER, p) for p in os.listdir(PATH_TO_STATIC_NOISE_FOLDER)]

def get_df_signal():
    assert len(os.listdir(PATH_TO_SIGNAL_FOLDER)) != 0, 'There must be data in signal folder'
    all_files = [os.path.join(PATH_TO_SIGNAL_FOLDER, p) for p in os.listdir(PATH_TO_SIGNAL_FOLDER)]
    all_files = sorted(all_files)
    offset = len(all_files) // 2
    return [(all_files[i], all_files[i+offset]) for i in range(offset)]

def normalize_image(img):
    img += abs(np.min(img))
    img /= np.max(img)
    img *= 255
    return img

if __name__ == '__main__':
    print_red('This', 'text', 'is red', 1, 23)
    print_blue('This', 'text', 'is blue', 1, 23)
    print_green('This', 'text', 'is green', 1, 23)
    print_yellow('This', 'text', 'is yellow', 1, 23)


import argparse
import gym
import random
import tensorflow as tf
import numpy as np
from tqdm import trange
from tensorflow import keras
from network import SharedModel
from subproc_env import EnvActor, SubProcessEnv

# Some parameters taken from OpenAI
# baselines implementation, since
# they're not mentioned in the paper.
num_actors = 8
# Values taken from Atari experiments
# in original paper where relevant
gae_lambda = 0.95
gamma = 0.99
base_clip_epsilon = 0.1
max_steps = 1e6
base_learning_rate = 2.5e-4
horizon = 128
batch_size = 32
optim_epochs = 3
value_loss_coefficient = 1
entropy_loss_coefficient = .01
gradient_max = 10.0
start_t = 0
checkpoint_filename = "./ppo-model.ckpt"
log_dir = "./tb_log"
SMALL_NUM = 1e-8

def main():
    env_name = "PongNoFrameskip-v4"
    # NOTE: This is currently not used since we use SubProcessEnv instead;
    # only used for getting shape of observation/acton space.
    unused_env = gym.make(env_name)
    #pobs_shape = unused_env.observation_space.shape
    # Hard-coding pre-processing step shape; could read it from an example output instead?
    obs_shape = (84, 84, 4)
    num_actions = unused_env.action_space.n
    model = SharedModel(obs_shape, num_actions)

    t = start_t
    last_save = 0
    actors = []

    for ii in range(num_actors):
        actors.append(EnvActor(SubProcessEnv(env_name), model, num_actions))

    while(t <= max_steps):
        for ii in range(horizon):
            for actor in actors:
                actor.step_env(t)
            t += 1

        for actor in actors:
            actor.calculate_horizon_advantages(t)

        # Construct randomly sampled (without replacement) mini-batches.
        obs_horizon = []
        act_horizon = []
        policy_horizon = []
        adv_est_horizon = []
        val_est_horizon = []

        for actor in actors:
            obs_a, act_a, policy_a, adv_est_a, val_est_a = actor.get_horizon(t)
            obs_horizon.extend(obs_a)
            act_horizon.extend(act_a)
            policy_horizon.extend(policy_a)
            adv_est_horizon.extend(adv_est_a)
            val_est_horizon.extend(val_est_a)

        # Normalizing advantage estimates.
        # NOTE: Adding this significantly improved performance
        # NOTE: Moved this out of each individual actor, so that advantages for the whole batch are normalized with each other.
        adv_est_horizon = np.array(adv_est_horizon)
        adv_est_horizon = (adv_est_horizon - np.mean(adv_est_horizon)) / (np.std(adv_est_horizon) + SMALL_NUM)

        num_samples = len(obs_horizon)
        indices = list(range(num_samples))
        for e in range(optim_epochs):
            random.shuffle(indices)
            ii = 0
            # TODO: Don't crash if batch_size is not a divisor of total sample count.
            while ii < num_samples:
                obs_batch = []
                act_batch = []
                policy_batch = []
                adv_batch = []
                value_sample_batch = []

                for _ in range(batch_size):
                    index = indices[ii]
                    obs_batch.append(obs_horizon[index].__array__())
                    act_batch.append(act_horizon[index].__array__())
                    policy_batch.append(policy_horizon[index].__array__())
                    adv_batch.append(adv_est_horizon[index].__array__())
                    value_sample_batch.append(val_est_horizon[index])
                    ii += 1

                def alpha_anneal(t):
                    return np.maximum(1.0 - (float(t) / float(max_steps)), 0.0)

                total_loss = model.train(np.array(obs_batch),
                            np.array(act_batch),
                            np.array(policy_batch),
                            np.array(adv_batch),
                            np.array(value_sample_batch),
                            alpha_anneal(t))

        for actor in actors:
            actor.flush(t)

        if t-last_save > 10000:
            print("Saving network")
            model.network.save(checkpoint_filename)
            last_save = t

        all_ep_rewards = []
        for actor in actors:
            all_ep_rewards.extend(actor.episode_rewards)

        if len(all_ep_rewards) >= 10:
            print("T: %d" % (t,))
            print("\tAVG Reward: %f" % (np.mean(all_ep_rewards),))
            print("\tMIN Reward: %f" % (np.amin(all_ep_rewards),))
            print("\tMAX Reward: %f" % (np.amax(all_ep_rewards),))
            for actor in actors:
                actor.episode_rewards = []

if __name__ == '__main__':
    main()


# Create your views here.
from django.shortcuts import render
from team.models import Player
from django.shortcuts import render, get_object_or_404, redirect, render_to_response
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def home(request):
    context = {'message': 'Here is a message!'}
    return render(request, "team/home.html", context)

def roster(request):
    player_list = Player.objects.all()
    paginator = Paginator(player_list, 100)
    page = request.GET.get('page')
    try:
        players=paginator.page(page)
    except PageNotAnInteger:
        players = paginator.page(1)
    except EmptyPage:
        players = paginator.page(1)
    return render(request, "team/roster.html", {'players': players})

def player(request, pk):
    player = get_object_or_404(Player, id=pk)
    return render(request, "team/player.html", {'player': player})


#! /usr/bin/python3
# -*- coding:utf-8 -*-

from flask import Flask, request, render_template, redirect
import json
import os

app = Flask(__name__)

@app.route('/')
def accueil():
    if os.path.exists("db")==False:
        os.mkdir("db")
    return render_template('index.html')

@app.route('/formule')
def reponse():
    list_qst = []
    if os.path.exists("db/questions.json") == True:
        file = open("db/questions.json", "r")
        list_qst = json.load(file)
        file.close()
    list_tache = []
    if os.path.exists("db/taches.json") == True:
        file = open("db/taches.json", "r")
        list_tache = json.load(file)
        file.close()
    return render_template('formule.html', list_qst=list_qst, list_tache=list_tache)

@app.route('/questions', methods=['POST', 'GET'])
def questions():
    list_qst = []
    if os.path.exists("db/questions.json") == True:
        file = open("db/questions.json", "r")
        list_qst = json.load(file)
        file.close()
    list_tache = []
    if os.path.exists("db/taches.json") == True:
        file = open("db/taches.json", "r")
        list_tache = json.load(file)
        file.close()

    if request.method == 'POST':
        qst = request.form['qst']
        tache = request.form['tache']
        i = 0
        for t in list_tache:
            if str(t['id']) == str(tache):
                tache = list_tache[i]
                break
            i = i+1

        file = open("db/questions.json", "w")
        txt = {
            "id": len(list_qst),
            "question": qst,
            "tache": tache
        }
        list_qst.append(txt)
        file.write(json.dumps(list_qst, indent=True))
        file.close()

    return render_template('questions.html', list_qst=list_qst, list_tache=list_tache)

@app.route('/update_qst', methods=['POST', 'GET'])
def update_qst():
    list_qst = []
    if os.path.exists("db/questions.json") == True:
        file = open("db/questions.json", "r")
        list_qst = json.load(file)
        file.close()
    list_tache = []
    if os.path.exists("db/taches.json") == True:
        file = open("db/taches.json", "r")
        list_tache = json.load(file)
        file.close()
    if request.method == 'POST':
        id = request.form['id']
        qst = request.form['qst']
        tache = request.form['tache']
        i = 0
        for t in list_tache:
            if str(t['id']) == str(tache):
                tache = list_tache[i]
                break
            i = i+1
        i = 0
        for q in list_qst:
            if str(q['id']) == str(id):
                list_qst[i]['question'] = qst
                list_qst[i]['tache'] = tache
                break
            i = i+1
        file = open("db/questions.json", "w")
        file.write(json.dumps(list_qst, indent=True))
        file.close()
    return redirect('/questions')

@app.route('/taches', methods=['POST', 'GET'])
def taches():
    list_tache = []
    if os.path.exists("db/taches.json") == True:
        file = open("db/taches.json", "r")
        list_tache = json.load(file)
        file.close()
    if request.method == 'POST':
        tache = request.form['tache']
        file = open("db/taches.json", "w")
        txt = {
            "id": len(list_tache),
            "tache": tache
        }
        list_tache.append(txt)
        file.write(json.dumps(list_tache, indent=True))
        file.close()

    return render_template('taches.html', list_tache=list_tache)

@app.route('/update_tache', methods=['POST', 'GET'])
def update_tache():
    list_tache = []
    if os.path.exists("db/taches.json") == True:
        file = open("db/taches.json", "r")
        list_tache = json.load(file)
        file.close()
    if request.method == 'POST':
        id = request.form['id']
        tache = request.form['tache']
        i = 0
        for t in list_tache:
            if str(t['id']) == str(id):
                list_tache[i]['tache'] = tache
                break
            i = i+1
        file = open("db/taches.json", "w")
        file.write(json.dumps(list_tache, indent=True))
        file.close()
        up_qst()
    return redirect('/taches')

def up_qst():
    list_qst = []
    if os.path.exists("db/questions.json") == True:
        file = open("db/questions.json", "r")
        list_qst = json.load(file)
        file.close()
    list_tache = []
    if os.path.exists("db/taches.json") == True:
        file = open("db/taches.json", "r")
        list_tache = json.load(file)
        file.close()
    i = 0
    for q in list_qst:
        for t in list_tache:
            if str(t['id']) == str(q['id']):
                list_qst[i]['tache'] = t
        i = i+1
    file = open("db/questions.json", "w")
    file.write(json.dumps(list_qst, indent=True))
    file.close()

@app.route('/add_reponse', methods=['POST', 'GET'])
def add_reponse():
    list_qst = []
    if os.path.exists("db/questions.json") == True:
        file = open("db/questions.json", "r")
        list_qst = json.load(file)
        file.close()
    list_tache = []
    if os.path.exists("db/taches.json") == True:
        file = open("db/taches.json", "r")
        list_tache = json.load(file)
        file.close()
    list_reponse = []
    if os.path.exists("db/reponses.json") == True:
        file = open("db/reponses.json", "r")
        list_reponse = json.load(file)
        file.close()
    if request.method == 'POST':
        resp = {}
        resp['nom'] = request.form['nom']
        resp['prenom'] = request.form['prenom']
        nom = str(request.form['nom']).upper()+" " + \
            str(request.form['prenom']).upper()
        resp['sexe'] = request.form['sexe']
        resp['profession'] = request.form['profession']
        for t in list_tache:
            for q in list_qst:
                if str(t['id']) == str(q['tache']['id']):
                    resp['question_'+str(q['id'])] = q
                    tmp = 'resp'+str(q['id'])
                    resp['reponse_'+str(q['id'])] = request.form[tmp]
                    tmp = 'justif'+str(q['id'])
                    resp['justification_'+str(q['id'])] = request.form[tmp]
        list_reponse.append(resp)
        file = open("db/reponses.json", "w")
        file.write(json.dumps(list_reponse, indent=True))
        file.close()
        return redirect('/success/'+nom)
    else:
        return redirect('/formule')

@app.route('/success/<nom>')
def success(nom):
    return render_template('success.html', nom=nom)

@app.route('/getData', methods=['POST', 'GET'])
def chart():
    res=[]
    if request.method == 'POST':
        list_reponse = []
        if os.path.exists("db/reponses.json") == True:
            file = open("db/reponses.json", "r")
            list_reponse = json.load(file)
            file.close()
        list_qst = []
        if os.path.exists("db/questions.json") == True:
            file = open("db/questions.json", "r")
            list_qst = json.load(file)
            file.close()
        list_tache = []
        if os.path.exists("db/taches.json") == True:
            file = open("db/taches.json", "r")
            list_tache = json.load(file)
            file.close()
        for t in list_tache:
            out = {}
            out['tache'] = t['tache']
            out['non'] = 0
            out['oui'] = 0
            for q in list_qst:
                if str(q['tache']['id']) == str(t['id']):
                    id = str(q['id'])
                    for r in list_reponse:
                        if str(r['reponse_'+id]).lower() == "non":
                            out['non'] = out['non']+1
                        elif str(r['reponse_'+id]).lower() == "oui":
                            out['oui'] = out['oui']+1
            res.append(out)
    return json.dumps(res)

if __name__ == '__main__':
    app.run(debug=True)


import twitter

class pytwitter_forecast(NebriOS):
    listens_to = ['forecast_date']

    def check(self):
        return True

    def action(self):
        auth = twitter.OAuth(shared.ttoken, shared.ttoken_secret, shared.tconsumer_key, shared.tconsumer_secret)
        t = twitter.Twitter(auth=auth)
        status = "Forecast: " + self.check_city_forecast + " is " + self.forecast_text + " with temperature "+ self.forecast_lo + " - " + self.forecast_hi + " °C for " + self.forecast_date
        try:
            t.account.verify_credentials()
            try:
                t.statuses.update(status=status)
                # uncomment to update KVP of auth status for checking
                #self.pytwitter_update = "Run"
            except:
                self.pytwitter_update = "Fail"
        except:
            self.pytwitter_auth = "Fail"


####################### IMPORT LIBRARIES ####################################
from pandas import ExcelFile, read_excel
from pandas import datetime
from sklearn.metrics import mean_squared_error
from math import sqrt
import matplotlib.pyplot as plt
import warnings
from hmmlearn.hmm import GaussianHMM
import numpy as np
import time

####################### FUNCTION TO READ FILE ################################
def readfile(coin_file, attribute):
    ''' Function to read and parse the data '''
    # Read excel file
    xls = ExcelFile(coin_file)
    # Date parser
    def parser(x):
        try:
            return datetime.strptime(str(x),'%Y-%m-%d %H:%M:%S')
        except:
            return datetime.strptime(str(x),'%Y-%m-%d')
    series = read_excel(xls, attribute, header = 0, parse_dates =[0], index_col = 0, squeeze = True, date_parser = parser)
    series = series.fillna(0)
    # Store in array
    X = series.values
    return X

###################### FUNCTION TO SPLIT THE DATA INTO TRAINING AND TEST SET #######
def train_test_split(X, fraction):
    ''' Function to split the data into training and test set '''
    # Train test split
    size = int(len(X)*fraction)
    train,test = X[0:size], X[size:len(X)]
    return train,test

##################### FUNCTION TO DEFINE GAUSSIAN HIDDEN MARKOV MODEL ###############
def GaussHMM(n_comp,cov_type,n_itr,train,num_samples_test):
    ''' Function to define Gaussian Hidden Markov Model '''
    # Reshape training data
    history = train.reshape(-1,1)
    # Gaussian hidden markov model
    hmm = GaussianHMM(n_components = n_comp, covariance_type = cov_type, n_iter = n_itr)
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        hmm.fit(history)
    # Generate samples
    samples_test, _ = hmm.sample(num_samples_test)
    return samples_test

###################### FUNCTION TO CALCULATE PREDICTIONS AND EXPECTATIONS #############
def pred_expect(test,variable, samples_test, pred, expect):
    ''' Function to calculate predictions and expectations '''
    # Loop over test data
    for i in range(len(test)):
        # Integer values
        if variable == 'int':
            if round(samples_test[i][0]) < 0:
                pred.append(0)
            else:
                pred.append(round(samples_test[i][0]))
        # decimal values
        else:
            if samples_test[i][0] < 0:
                pred.append(0)
            else:
                pred.append(samples_test[i][0])
        expect.append(test[i])
    return pred, expect

############################# FUNCTION TO CALCULATE RMSE ###############################
def rmse(coin_file, attribute,iterations,fraction,n_comp,cov_type,n_itr,var):
    ''' Function to calculate RMSE '''
    X = readfile(coin_file,attribute)
    train,test = train_test_split(X,fraction)
    rmse_test = list()
    for j in range(iterations):
        ######################## FOR FINAL RMSE ############################
        pred = list()
        expect = list()
        num_samples_test = len(test) # number of samples to be generated
        samples_test = GaussHMM(n_comp,cov_type,n_itr,train,num_samples_test)
        pred,expect = pred_expect(test,var,samples_test, pred, expect)
        rmse_test.append(sqrt(mean_squared_error(pred,expect)))
    return rmse_test

##################### PRINT AVERAGE RMSE FOR TEST DATA #####################
coin_file = '4_RLC.xlsx'    # Filename
attribute = 'exchange'         # attribute
iterations = 30                 # number of iterations for averaging rmse
fraction = 0.80             # Train - test split
n_comp = 7                    # n_components for Gaussian HMM
cov_type = 'diag'                # covariance_type for Gaussian HMM
n_itr = 1000                  # n_iter for Gaussian HMM
var = 'float'                 # Integer or Float valued attribute

start = time.time()
rmse_test = rmse(coin_file, attribute,iterations,fraction,n_comp,cov_type,n_itr,var)
end = time.time()

print('The average RMSE over %d iterations is %.3f' %(iterations,np.array(rmse_test).mean()))
print('The time taken is %.3f seconds' %(end - start))


from dumpulator import Dumpulator
from pwn import *
import inspect
import sys

"""
.rdata:010F3C7C xmmword_10F3C7C xmmword 0BACA7A0A1B6B4A5BAEAEFF6B5B1B1BDAh
.rdata:010F3C7C                                         ; DATA XREF: dga+6Er
.rdata:010F3C8C xmmword_10F3C8C xmmword 2818CF8A0A988AAE2B4A7BAE8AAA6ABEh
.rdata:010F3C8C                                         ; DATA XREF: dga+8Br
.rdata:010F3C9C word_10F3C9C    dw 0EAh                 ; DATA XREF: dga+7Ar
.rdata:010F3C9E                 align 10h
.rdata:010F3CA0 ; const char Delimiter[2]
.rdata:010F3CA0 Delimiter       db '\',0                ; DATA XREF: _main+2Eo
.rdata:010F3CA0                                         ; _main:loc_10E1EE2o
.rdata:010F3CA2                 align 4
.rdata:010F3CA4 qword_10F3CA4   dq 13B6A6F6B6A60734h    ; DATA XREF: sub_10E13A0+1Br
.rdata:010F3CAC dword_10F3CAC   dd 87F657h              ; DATA XREF: sub_10E13A0+30r
.rdata:010F3CB0 dword_10F3CB0   dd 720063h              ; DATA XREF: sub_10E13A0+DFr
.rdata:010F3CB4 dword_10F3CB4   dd 6C0075h              ; DATA XREF: sub_10E13A0+E6r
.rdata:010F3CB8 dword_10F3CB8   dd 61006Fh              ; DATA XREF: sub_10E13A0+EEr
.rdata:010F3CBC dword_10F3CBC   dd 650064h              ; DATA XREF: sub_10E13A0+F6r
.rdata:010F3CC0 dword_10F3CC0   dd 72h                  ; DATA XREF: sub_10E13A0+FEr
.rdata:010F3CC4 qword_10F3CC4   dq 8EFE6F5FBFEFFF8Eh    ; DATA XREF: sub_10E13A0+191r
.rdata:010F3CCC word_10F3CCC    dw 9Fh                  ; DATA XREF: sub_10E13A0+189r
.rdata:010F3CCE                 align 10h
.rdata:010F3CD0 xmmword_10F3CD0 xmmword 61616161616161616161616161616161h
.rdata:010F3CD0                                         ; DATA XREF: sub_10E13A0+205r
.rdata:010F3CE0 xmmword_10F3CE0 xmmword 659B537ED2F05B7D47742A227C6FFE70h
.rdata:010F3CE0                                         ; DATA XREF: sub_10E1000+13r
.rdata:010F3CF0 ; Debug Directory entries
.rdata:010F3CF0                 dd 0                    ; Characteristics
.rdata:010F3CF4                 dd 5EE8C20Ch            ; TimeDateStamp: Tue Jun 16 12:58:52 2020
.rdata:010F3CF8                 dw 0                    ; MajorVersion
.rdata:010F3CFA                 dw 0                    ; MinorVersion
.rdata:010F3CFC                 dd 0Dh                  ; Type: IMAGE_DEBUG_TYPE_POGO
.rdata:010F3D00                 dd 268h                 ; SizeOfData
.rdata:010F3D04                 dd rva aGctl            ; AddressOfRawData
.rdata:010F3D08                 dd 12BECh               ; PointerToRawData
.rdata:010F3D0C                 dd 0                    ; Characteristics
.rdata:010F3D10                 dd 5EE8C20Ch            ; TimeDateStamp: Tue Jun 16 12:58:52 2020
.rdata:010F3D14                 dw 0                    ; MajorVersion
.rdata:010F3D16                 dw 0                    ; MinorVersion
.rdata:010F3D18                 dd 0Eh                  ; Type: IMAGE_DEBUG_TYPE_ILTCG
.rdata:010F3D1C                 dd 0                    ; SizeOfData
.rdata:010F3D20                 dd 0                    ; AddressOfRawData
.rdata:010F3D24                 dd 0                    ; PointerToRawData
.rdata:010F3D28 __load_config_used dd 0B8h              ; Size
.rdata:010F3D2C                 dd 0                    ; Time stamp
.rdata:010F3D30                 dw 2 dup(0)             ; Version: 0.0
.rdata:010F3D34                 dd 0                    ; GlobalFlagsClear
.rdata:010F3D38                 dd 0                    ; GlobalFlagsSet
.rdata:010F3D3C                 dd 0                    ; CriticalSectionDefaultTimeout
.rdata:010F3D40                 dd 0                    ; DeCommitFreeBlockThreshold
.rdata:010F3D44                 dd 0                    ; DeCommitTotalFreeThreshold
.rdata:010F3D48                 dd 0                    ; LockPrefixTable
.rdata:010F3D4C                 dd 0                    ; MaximumAllocationSize
.rdata:010F3D50                 dd 0                    ; VirtualMemoryThreshold
.rdata:010F3D54                 dd 0                    ; ProcessAffinityMask
.rdata:010F3D58                 dd 0                    ; ProcessHeapFlags
.rdata:010F3D5C                 dw 0                    ; CSDVersion
.rdata:010F3D5E                 dw 0                    ; Reserved1
.rdata:010F3D60                 dd 0                    ; EditList
.rdata:010F3D64                 dd offset ___security_cookie ; SecurityCookie
.rdata:010F3D68                 dd offset ___safe_se_handler_table ; SEHandlerTable
.rdata:010F3D6C                 dd 3                    ; SEHandlerCount
.rdata:010F3D70                 dd offset ___guard_check_icall_fptr ; GuardCFCheckFunctionPointer
.rdata:010F3D74                 dd 0                    ; GuardCFDispatchFunctionPointer
.rdata:010F3D78                 dd 0                    ; GuardCFFunctionTable
.rdata:010F3D7C                 dd 0                    ; GuardCFFunctionCount
.rdata:010F3D80                 dd 100h                 ; GuardFlags
"""

def decrypt_one_string():
    string="0x13B6A6F6B6A60734"
    rez = ""
    for i in range(2,len(string),2):
        rez +=(chr(int(string[i:i+2][::-1],base=16)^ 0x1f ))
    print(rez[::-1])

def decrypt_string_two():
    string="0xE8FFFEFBF5F6EFE8F9"
    rez = ""
    for i in range(2,len(string),2):
        rez +=(chr(int(string[i:i+2],base=16)^ 0x9A ))
    print(rez[::-1])

def decrypt_string_three():
    s1 = "0x7C6D1DBD1FEF1D5DDC6CCCBC5FEF891E"
    s2 = "0x7CAD7CC86D1DDCAC1C4D1DEF0919FC"
    rez = ""
    rez2 = ""
    for i in range(2,len(s1),2):
        rez +=(chr(int(s1[i:i+2][::-1],base=16)^ 0xA2 ))
    print(rez[::-1])

    for i in range(2,len(s2),2):
        rez2 +=(chr(int(s2[i:i+2][::-1],base=16)^ 0xA2 ))
    print(rez2[::-1])

def decrypt_string_four():
    """
    Fking decrypt this after finding where tf is isdebuggerpresent // peb->isprocessdebugged???
    LABEL_16:
    v21 = dword_10F6AA8;
  }
  else
  {
    while ( 1 )
    {
      v15 = String;
      if ( *String == *v17 && *&String[4] == *(v17 + 4) && v35 == *(v17 + 8) )
        break;
      ++v20;
      ++v17;
      if ( v20 >= dwSize )
        goto LABEL_16;
    }
    v1 = (v17 + 9);
    v21 = dwSize - v20 - 9;
    dword_10F6AA8 = v21;
  }
  v22 = 0;
  if ( v21 && v21 >= 0x40 )
  {
    v23 = v1 + 32;
    v17 = v21 & 0xFFFFFFC0;
    do
    {
      v24 = *(v23 - 2);
      v23 += 64;
      v22 += 64;
      *(v23 - 6) = _mm_xor_si128(xmmword_10F3CD0, v24);
      *(v23 - 5) = _mm_xor_si128(*(v23 - 5), xmmword_10F3CD0);
      *(v23 - 4) = _mm_xor_si128(*(v23 - 4), xmmword_10F3CD0);
      *(v23 - 3) = _mm_xor_si128(*(v23 - 3), xmmword_10F3CD0);
    }
    while ( v22 < v17 );
  }
  for ( ; v22 < v21; ++v22 )
    v1[v22] ^= 0x61u;
    """
    v21 = 0
    file = open(sys.argv[1],"rb")
    v14 = file.read()
    print(hexdump(v14[v14.index(b'redaolurc')+9:]))
    s = ""
    k = v14[v14.index(b'redaolurc')+9:]
    print(type(k))
    for i in k:
        s+= chr(i ^ 0x61)
    print("=======================================")
    print(hexdump(s))
    print("!!!!!!!!!!!!!!saving final stage to .dll file!!!!!!!!!!\n")
    print("!!!!!!!!!!!!!!please stad by!!!!!!!!!!!!!!!!!!!!!!!\n")
    z = open("final_stage_payload.dll","wb")
    decrypted = bytearray()
    for i in k:
        decrypted.append(i ^ 0x61)
    z.write(decrypted)
    z.close()
    file.close()

def decrypt_string_five():
    s2="0x2818CF8A0A988AAE2B4A7BAE8AAA6ABE"
    s1="0xBACA7A0A1B6B4A5BAEAEFF6B5B1B1BDA"
    rez = ""
    for i in range(2,len(s1),2):
        rez +=(chr(int(s1[i:i+2][::-1],base=16)^ 0xC5 ))
    print(rez[::-1])

    rez2 = ""
    for i in range(2,len(s2),2):
        rez2 +=(chr(int(s2[i:i+2][::-1],base=16)^ 0xC5 ))
    print(rez2[::-1])

def anti_analysis_decryption_check():
    """
    bottom line is this :
    659B537ED2F05B7D47742A227C6FFE70

    this is actually an concatenation of the 4 byte hash-es which break down to

   | 65 9B 53 7E |D2 F0 5B 7D |47 74 2A 22| 7C 6F FE 70

    so by definition/ canonicall function call looks like this
    sub_1351000(v3, v4, v5), where args are
    003EF4A4  00715F38  &"C:\\Users\\pwn\\Desktop\\stage2_challenge_mal_analysis.dll" v5
    003EF4A8  003EF580   v4
    003EF4AC  00000000   v3
    """
    dp = Dumpulator("2nd_stage.dmp")
    prolog_start = 0x13510D0
    prolog_stop  = 0x135118A
    crc_lookup_table = []
    dp.start(begin=prolog_start,end=prolog_stop)
    print(dir(dp))
    print("!!!!!!!!!!!dumping crc lookup tablen!!!!!!!!\n")
    print("!!!!!!please stand by!!!!!!!!\n")
    for i in range(0,256):
        iterator = i * 4
        crc_lookup_table.append(hex(dp.read_ptr(0x1366290+iterator)))
    #print(crc_lookup_table)
    process_list_input = ["system","smss.exe","crss.exe",]
    v7  = "s\x00y\x00s\x00t\x00e\00m\x00\x00"
    v6 = len(v7)-1
    v1 = v7[v6]
    eax = "0xffffffff"
    ctr_eax = 2
    cnt_eax = 0
    cnt_shift_idx_eax = 0
    crn_shift_idx_eax = 2
    res_tmp = ""
    rez_final = 0
    for j in range(0,v6):
        if(cnt_shift_idx_eax == 4):
            ctr_shift_idx_eax = 2
        if(cnt_eax == 4):
            ctr_eax = 2
        print(eax[ctr_eax:ctr_eax+2])
        cur_eax = int(eax[ctr_eax:ctr_eax+2],base=16)
        print(cur_eax)
        res_tmp =  ord(v7[j]) ^ cur_eax
        print(res_tmp)
        shift_rez = cur_eax >> 8
        print(shift_rez)
        if(shift_rez == 0):
            print("aci")
            print(type(eax[crn_shift_idx_eax]))
            print(eax[crn_shift_idx_eax])
            print(eax[crn_shift_idx_eax+1])
            eax = eax.replace(eax[crn_shift_idx_eax],"0").replace(eax[crn_shift_idx_eax+1],"0")
            print(eax)
        else:
            print("aici2")
            to_reaplace_one = hex(shift_rez)[2]
            to_replace_two  = hex(shift_rez)[3]
            eax = eax.replace(eax[crn_shift_idx_eax],to_replace_one).replace(eax[crn_shft_idx_eax],to_replace_two)
        print(eax)
        print(hex(res_tmp))
        rez_final = int(eax,base=16) ^ int(crc_lookup_table[res_tmp],base=16)
        print(hex(rez_final))
        eax=hex(rez_final)
        ctr_eax+=2
        cnt_eax+=1
        crn_shift_idx_eax+=2
        cnt_shift_idx_eax+=1
        enc_process = "0x659B537ED2F05B7D47742A227C6FFE70"
        for i in range(2,len(enc_process),8):
            if(enc_process[i] != rez_final):
                print(enc_process[i:i+8])
                print("ye,we are not being debugged")

decrypt_string_four()
anti_analysis_decryption_check()


import torch
import torch.nn as nn
import torch.nn.functional as F
from .utils import weight_reduce_loss

class FocalLoss(nn.Module):
    def __init__(self,
                 use_sigmoid=True,
                 gamma=2.0,
                 alpha=0.25,
                 reduction='mean',
                 loss_weight=1.0):
        super(FocalLoss, self).__init__()
        assert use_sigmoid is True, 'Only sigmoid focal loss supported now.'
        self.use_sigmoid = use_sigmoid
        self.gamma = gamma
        self.alpha = alpha
        self.reduction = reduction
        self.loss_weight = loss_weight

    def forward(self,
                pred,
                target,
                weight=None,
                avg_factor=None):
        device = pred.device
        assert pred.shape[0] == target.shape[0]

        if len(target.shape) == 1:
            target = torch.zeros(
                pred.shape,
                dtype=torch.long,
                device=device).scatter_(
                    1, target.view(-1, 1), 1)

        pred_sigmoid = pred.sigmoid()
        pred_sigmoid = torch.clamp(pred_sigmoid, 1e-4, 1.0 - 1e-4)
        target = target.type_as(pred)
        pt = (1 - pred_sigmoid) * target + pred_sigmoid * (1 - target)
        focal_weight = (self.alpha * target + (1 - self.alpha) *
                        (1 - target)) * pt.pow(self.gamma)
        loss = F.binary_cross_entropy_with_logits(
            pred, target, reduction='none') * focal_weight

        loss = torch.where(torch.ne(target, -1.0),
                           loss, torch.zeros(loss.shape).to(device))

        loss = self.loss_weight * weight_reduce_loss(
            loss,
            weight=weight,
            reduction=self.reduction,
            avg_factor=avg_factor)

        return loss


from flask import Flask,request
import sqlite3

app = Flask(__name__)

connection = sqlite3.connect('sms.db')
curser = connection.cursor()
curser.execute('create table if not exists students (sid integer primary key,name text,age integer,address text)')
connection.close()

@app.route('/student_details')
def details():
    connection = sqlite3.connect('sms.db')
    curser = connection.cursor()
    return {'students' : list(curser.execute('select * from students'))}

@app.route('/details',methods = ['POST'])
def register():
    data = request.get_json()
    connection = sqlite3.connect('sms.db')
    curser = connection.cursor()
    curser.execute("insert into students values('{},{},{},{}')".format(data['sid'],data['name'],data['age'],data['address']))
    connection.commit()
    connection.close()
    return 'User Created Successfully.. '
app.run(port=5000)


import tkinter

win = tkinter.Tk()
win.title("hanyb")
win.geometry("400x400+200+0")

# 菜单条
menubar = tkinter.Menu(win)
win.config(menu=menubar)

# 创建一个菜单选项
menu1 = tkinter.Menu(menubar, tearoff=False)
# 给菜单选项添加内容
for item in ["Python","R","C","C++","Java","shell","c#","JS","PHP","汇编","NodeJS","quit"]:
    if item =="quit":
        # 添加一个分隔线
        menu1.add_separator()
        menu1.add_command(label=item, command=win.quit)
    else:
        menu1.add_command(label=item)

# 向菜单条上添加菜单选项
menubar.add_cascade(label="语言", menu=menu1)

# 再创建一个菜单
menu2 = tkinter.Menu(menubar, tearoff=False)
menu2.add_command(label="red")
menu2.add_command(label="white")
menu2.add_command(label="black")
menubar.add_cascade(label="color", menu=menu2)

win.mainloop()


if __name__ is not None and "." in __name__:
    from .SkylineParser import SkylineParser
    from .SkylineVisitor import SkylineVisitor
    from .Skyline import Skyline
else:
    from SkylineParser import SkylineParser
    from SkylineVisitor import SkylineVisitor
    from Skyline import Skyline

class EvalVisitor(SkylineVisitor):
    def __init__(self):
        self.identifiers = {}

    def visitRoot(self, ctx: SkylineParser.RootContext):
        l = [n for n in ctx.getChildren()]
        return self.visit(l[0])

    def visitStart(self, ctx: SkylineParser.StartContext):
        l = [n for n in ctx.getChildren()]
        if len(l) == 1:
            return self.visit(l[0])
        elif len(l) == 3:
            identifier = l[0].getText()
            skyline = self.visit(l[2])
            self.identifiers[identifier] = skyline
            return skyline

    def visitSkyline(self, ctx: SkylineParser.SkylineContext):
        l = [n for n in ctx.getChildren()]
        return self.visit(l[0])

    def visitPriority1(self, ctx: SkylineParser.Priority1Context):
        l = [n for n in ctx.getChildren()]
        skyline = self.visit(l[0])
        if len(l) >= 3:
            for i in range(2, len(l), 2):
                if hasattr(l[i], 'getRuleIndex'):
                    skyline.union(self.visit(l[i]))
                else:
                    if l[i - 1].getText() == '+':
                        skyline.right_shift(int(l[i].getText()))
                    else:
                        skyline.left_shift(int(l[i].getText()))
        return skyline

    def visitPriority2(self, ctx: SkylineParser.Priority2Context):
        l = [n for n in ctx.getChildren()]
        skyline = self.visit(l[0])
        if len(l) >= 3:
            for i in range(2, len(l), 2):
                if hasattr(l[i], 'getRuleIndex'):
                    skyline.intersection(self.visit(l[i]))
                else:
                    skyline.replication(int(l[i].getText()))
        return skyline

    def visitPriority3(self, ctx: SkylineParser.Priority3Context):
        l = [n for n in ctx.getChildren()]
        skyline = self.visit(l[len(l) - 1])
        if len(l) == 2:
            skyline.mirror()
        return skyline

    def visitPriority4(self, ctx: SkylineParser.Priority4Context):
        l = [n for n in ctx.getChildren()]
        if len(l) == 3:
            return self.visit(l[1])
        else:
            return self.visit(l[0])

    def visitIdentifier(self, ctx: SkylineParser.IdentifierContext):
        identifier = ctx.getText()
        return self.identifiers[identifier]

    def visitBuilding(self, ctx: SkylineParser.BuildingContext):
        l = [n for n in ctx.getChildren()]
        skyline = Skyline()
        skyline.building(int(l[1].getText()), int(l[3].getText()), int(l[5].getText()))
        return skyline

    def visitComposed(self, ctx: SkylineParser.ComposedContext):
        l = [n for n in ctx.getChildren()]
        skyline = self.visit(l[1])
        if len(l) >= 5:
            for i in range(3, len(l) - 1, 2):
                skyline.union(self.visit(l[i]))
        return skyline

    def visitRandom(self, ctx: SkylineParser.RandomContext):
        l = [n for n in ctx.getChildren()]
        skyline = Skyline()
        skyline.random(int(l[1].getText()), int(l[3].getText()), int(l[5].getText()), int(l[7].getText()), int(l[9].getText()))
        return skyline


students = ['Mosh', 'musa', 'uthman', 'khalil', 'mustafa']
for s in students:
    print(s)

numbers = [1, 2, 3, 45, 66, 55, 9, 900]
sum = 0
for n in numbers:
    sum = sum + n

print(sum)

name = 'Uthman'
for s in name:
     print(s)

number = [1, -2, -9, 8, 7, -6]
for n in number:
    if n >=0:
        print(n)


total = 0
count = 0
while True :
    inp = input('Enter Number: ')
    if inp == 'done' : break
    value = float(inp)
    total = total + value
    count = count + 1

average = total / count
print('Average:', average)

#This is a loop that takes the average of a bunch of
#numbers, and then spits out the total, but can be done
#better using a list

numlist = list()
#create a list
while True :
    inp2 = input('Enter a number: ')
    if inp2 == 'DONE' : break
    valuu = float(inp2)
    #input same code as before
    numlist.append(valuu)
    #rather than add to count, just append the list

avg = sum(numlist) / len(numlist)
#do regular commands to the list to make an avg sum/len
print('Your average is:' , avg)

#Instead of having to construct a count and total number,
#the list just does that for you, and you can just focus on
#getting the average by messing with the list rather than
#constructing a lot of attached strings