import os
import imageio
import argparse
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

from natsort import natsorted
from PIL import Image, ImageDraw, ImageFont

def plot_generated(generated_imgs, dim=(1, 10), figsize=(12, 2), save_name=None):
  plt.figure(figsize=figsize)
  for i in range(generated_imgs.shape[0]):
    plt.subplot(dim[0], dim[1], i+1)
    plt.imshow(generated_imgs[i], interpolation='nearest', cmap='gray_r')
    plt.axis('off')
  plt.tight_layout()
  # plt.draw()
  # plt.pause(0.01)
  if save_name is None:
    plt.show()
  else:
    plt.savefig(save_name)
    plt.close()

def create_gif(img_folder, save_gif_name=None, fps=5):
  images = [f'{img_folder}{img}' for img in natsorted(os.listdir(img_folder))]

  gif = []
  for image in images:
      gif.append(imageio.imread(image))

  if save_gif_name is None:
    save_gif_namef = f'{img_folder[:-1]}_animated.gif'

  imageio.mimsave(save_gif_namef, gif, fps=fps)

def read_subset_classif_results(log_file):
  with open(log_file, 'r') as f:
    data = f.read().splitlines()

  starts_ends = [i for i, el in enumerate(data) if 'START' in el or 'END' in el]
  starts_ends = starts_ends[:len(starts_ends) - len(starts_ends) % 2]

  results = {'epoch': [], 'f1': [], 'label': []}
  percents = []
  for i, idx in enumerate(range(0, len(starts_ends), 2)):
    # if i == 0:
    #   continue
    tmp = data[starts_ends[idx]:starts_ends[idx+1]]
    epochs, f1s = zip(*[(int(l.split(' | ')[0].split('Epoch ')[-1]), float(l.split(' = ')[-1])) for l in tmp if ' | f1 = ' in l])
    results['epoch'] += epochs
    results['f1'] += f1s
    results['label'] += [i] * len(epochs)
    # percents.append(tmp[0].split(' = ')[-1])
    percents.append(str(int(float(tmp[0].split(' = ')[-1]) * 6000)))
    # print(f'Percent = {percents[-1]} | max f1 = {max(f1s)}')
    print(f'n_examples_per_class = {percents[-1]} | max f1 = {max(f1s)}')

  lp = sns.lineplot(x='epoch', y='f1', hue='label', data=pd.DataFrame.from_dict(results),
                    legend=False, palette=sns.color_palette()[:len(percents)])
  # lp.set(yscale="log")
  plt.legend(title='percent', loc='lower right', labels=percents)
  plt.show()

def read_ssdcgan_logs(folder='tmp_data/', root_name='_tmp_mnist_gan_ssdcgan_percent{}_logs.txt'):
  for percent in [0.002, 0.004, 0.009, 0.017, 0.084, 0.17, 0.34, 0.67, 1.]:
    fname = os.path.join(folder, root_name.format(percent)).replace('0.', '0')
    if not os.path.isfile(fname):
      continue

    with open(fname, 'r') as f:
      data = f.read().splitlines()

    f1s = [float(l.split(' = ')[-1]) for l in data if 'Saving model with' in l]
    n_examples = int(percent * 6000)
    print(f'n_examples = {n_examples} | f1 = {np.max(f1s)}')

def plot_comp_imgs(folder='tmp_data/', fname='imgs_generated_epoch180.png'):
  dcgan, cdcgan, acdcgan = 'generated_dcgan_imgs/', 'generated_cdcgan_imgs/', 'generated_acdcgan_imgs/'

  img_dcgan = Image.open(os.path.join(folder, dcgan, fname))
  img_cdcgan = Image.open(os.path.join(folder, cdcgan, fname))
  img_acdcgan = Image.open(os.path.join(folder, acdcgan, fname))

  font = ImageFont.truetype('open-sans/OpenSans-Regular.ttf', 20)

  for text, tmp_img in zip(['DC-GAN', 'Conditional DC-GAN', 'AC-DC-GAN'], [img_dcgan, img_cdcgan, img_acdcgan]):
    draw = ImageDraw.Draw(tmp_img)
    draw.text((5, 5), text, font=font, fill=(0, 0, 0))

  img = Image.new('L', (img_dcgan.width, img_dcgan.height + img_cdcgan.height + img_acdcgan.height))

  img.paste(img_dcgan, (0, 0))
  img.paste(img_cdcgan, (0, img_dcgan.height))
  img.paste(img_acdcgan, (0, 2 * img_dcgan.height))

  img.show()

if __name__ == '__main__':
  argparser = argparse.ArgumentParser(prog='plotter.py', description='')
  argparser.add_argument('--log_file', default='_tmp_classif_exps_mnist_logs.txt', type=str)
  args = argparser.parse_args()

  rep = input('Read subset classification results? (y or n): ')
  if rep == 'y':
    read_subset_classif_results(args.log_file)

  rep = input('Read ssdcgan logs? (y or n): ')
  if rep == 'y':
    read_ssdcgan_logs()

  rep = input('Plot comparison of generated GAN images? (y or n): ')
  if rep == 'y':
    plot_comp_imgs()


import pandas as pd
import jieba
import warnings
import re
warnings.filterwarnings('ignore')
import json

# 第一大部分，查全率，查准率的计算
def judge(strs):
    strs = strs.split(",")
    l = []
    for s in strs:
        s = s.replace('[',"").replace(']',"").replace("'","")
        l.append(int(s))
    return l

def tongji(l1,l2):
    listes = []
    for i in range(988):
        l = []
        l.append(l1[i])
        ls = judge(l2[i])
        for s in ls:
            l.append(s)
        listes.append(l)
    return listes

def readdata1(data1s1,data1s2):
    ls1 = []
    ls2 = []
    ls2.append(data1s1[0])
    ls2.append(data1s2[0])
    for i in range(len(data1s2)-1):
        i = i + 1
        if data1s1[i] == data1s1[i-1]:
            ls2.append(data1s2[i])
        else:
            ls1.append(ls2)
            ls2 = []
            ls2.append(data1s1[i])
            ls2.append(data1s2[i])
    return ls1

def judgeidda(num,iddata2):
    flag = 0
    k = -1
    for j in range(len(iddata2)):
        if num == iddata2[j][0]:
            flag = 1
            k = j
            break
    if flag == 1:
        return True,k
    else:
        return False,0

def compareequal(s1,s2):
    if len(s1) != len(s2):
        return  False
    else:
        for i in range(len((s1))):
            if s1[i] not in s2:
                return False
    return True

def compareequal2(s1,s2):
    flag = 0
    for i in range(len((s1))):
        if s1[i] in s2:
             flag = 1
    if flag == 1:
        return True
    else:
        return False

def chaquanzhun(iddata1, iddata2):
    len1 = len(iddata1)
    len2 = len(iddata2)
    k = 0
    for i in range(len(iddata1)):
        if iddata1[i] in iddata2:
            k = k + 1
    recallrate = k/len2
    precisionrate = k/len1
    # 查全率，查准率
    return recallrate, precisionrate

def compare(iddata2,iddata1):
    l_i_q_z = []
    for i in range(len(iddata1)):
        lt = []
        t1,j = judgeidda(iddata1[i][0], iddata2)
        if t1 == True:
            t = compareequal2(iddata1[i], iddata2[j])
            if t == True:
                recallrate, precisionrate = chaquanzhun(iddata1[i], iddata2[j])
                lt.append(iddata1[i][0])
                lt.append(recallrate)
                lt.append(precisionrate)
                l_i_q_z.append(lt)
    return l_i_q_z

def main2():
    data1 = pd.read_csv(r'F:\taidibei\huazhongbei\shuju\0.2--1至7294.csv', sep=',', encoding='utf-8',
                        error_bad_lines=False)
    data2 = pd.read_csv(r'F:\taidibei\huazhongbei\附件2.csv', sep=',', encoding='utf-8', error_bad_lines=False)

    # id_data2是附件2中lable = 1 的questionID和duplicates
    # [99850, 95677],
    # [104165, 104161],
    # [101403, 100311],
    id_data2 = tongji(data2['questionID'], data2['duplicates'])

    # id_data1为问题相似度大于0.3的questionID和duplicates
    # [63022, 62860],
    # [46866, 46762],
    # [97939, 80376]
    id_data1 = readdata1(data1['id'], data1['du'])

    # l_i_q_z返回一个questionID和查全率,查准率
    # [99850, 0.5, 0.5],
    # [104165, 1.0, 1.0],
    # [101403, 1.0, 1.0]
    l_i_q_z = compare(id_data2, id_data1)

    f = open(r'F:\taidibei\huazhongbei\l_i_q_z0.2.csv', "w+")
    f.write('问题id'+","+'查全率'+","+"查准率"+"\n")
    for i in range(len(l_i_q_z)):
        f.write(str(l_i_q_z[i][0]) + ",")
        f.write(str(l_i_q_z[i][1]) + ",")
        f.write(str(l_i_q_z[i][2]) + "\n")

# 第二大部分，相似度的计算
# 2-garm分词
def n_garm(splits):
    l_str = []
    for i in range(len(splits)):
        lstr1 = []
        if i + 1 < len(splits):
            lstr1.append(splits[i])
            lstr1.append(splits[i + 1])
            l_str.append(lstr1)
    return l_str

# 获取问题和id
def contextCandel(data, num):
    listes = []
    for i in range(len(data)):
        datanum = []
        str = data[i].strip()
        str = re.sub('[\s+]', '', str)
        split_words = [x for x in jieba.cut(str) if x not in
                       stopone(r'F:\taidibei\huazhongbei\stop_words.utf8')]
        print(split_words)
        split_words = n_garm(split_words)
        datanum.append(num[i])
        datanum.append(split_words)
        listes.append(datanum)
    return listes

# 调用停词表
def stopone(path):
    with open(path, 'r', encoding='utf-8') as f:
        return [l.strip() for l in f]

# n-garm比较
def judge(l, a, b):
    count1 = 0
    count2 = 0
    for i in range(len(l[a][1])):
        for j in range(len(l[b][1])):
            if l[a][1][i] == l[b][1][j]:
                count1 = count1 + 1
                break
    for i in range(len(l[b][1])):
        for j in range(len(l[a][1])):
            if l[b][1][i] == l[a][1][j]:
                count2 = count2 + 1
                break
    return (count1 + count2) / (len(l[a][1]) + len(l[b][1]))

# 计算相似度并存入csv
def compare_judge(l):
    f = open(r'F:\taidibei\huazhongbei\number.csv', "w+")
    for i in range(len(l)-1):
        k = i + 1
        for j in range(len(l)-k):
            if j + k < len(l):
                number = judge(l, i, j + k)
                # 选择相似度大于0.2的存储
                if number > 0.2:
                    f.write(str(l[i][0])+",")
                    f.write(str(l[j + k][0]) + ",")
                    f.write(str(number) + "\n")
    f.close()

def main1():
    data = pd.read_csv(r'F:\taidibei\huazhongbei\附件1.csv', sep=',', encoding='utf-8', error_bad_lines=False)
    l = contextCandel(data['translated'], data['id'])
    compare_judge(l)

def main3():
    data = pd.read_csv(r'F:\taidibei\huazhongbei\相似度大于0.2--1至7294.csv', sep=',', encoding='utf-8',
                       error_bad_lines=False)
    # 计算相似度大于0.2的csv里面除去重复的有多少数据
    ls1 = []
    ls2 = []
    ls2.append(data[0])
    count = 1
    for i in range(len(data) - 1):
        i = i + 1
        if data[i] == data[i - 1]:
            count = count + 1
        else:
            ls2.append(count)
            ls1.append(ls2)
            ls2 = []
            ls2.append(data[i])
            count = 1
    for i in ls1:
        print(i)

#按重复数量从大到小返回一个[[id,数量],[id,数量],[id,数量]]的列表
def main4(data, topK=806):
    tf_dic = {}
    for w in data:
        tf_dic[w] = tf_dic.get(w, 0)+1
    return sorted(tf_dic.items(), key=lambda x : x[1], reverse=True)[:topK]

def main():
    k = eval(input("请输入一个数字："))
    print("1.第二大部分，相似度的计算,写进csv.")
    print("2.第一大部分，查全率，查准率的计算，写近csv.")
    print("3.计算相似度大于0.2的csv里面除去重复的有多少数据.")
    print("4.重复数量从大到小返回一个[[id,数量],[id,数量],[id,数量]]的列表.")
    if k == 1:
        main1()
    if k == 2:
        main2()
    if k == 3:
        main3()
    if k == 4:
        main4()

if __name__ == '__main__':
    main()


from sys import argv, exit
from collections import OrderedDict
from functools import reduce
import re
import httplib2

# Google API
from apiclient import discovery
from apiclient.errors import HttpError
from oauth2client import client, tools
from oauth2client.file import Storage

# Reddit API
import praw
import OAuth2Util

#
# Configuration.
#

# The ID of the spreadsheet, which can be found in the URL. This example is the
# ID for https://docs.google.com/spreadsheets/d/1v8SVnzrGaupBKm6CbtmPjMqOrkcl0_sfEW0eEcujkys/edit
SPREADSHEET_ID = "1v8SVnzrGaupBKm6CbtmPjMqOrkcl0_sfEW0eEcujkys"
# The flair sheet is the sheet where the username, mail, snail mail and flair
# assignment columns are. The bot assumes these columns are given in this order
# starting in column A and the actual values start in row 2.
FLAIR_SHEET = "Sheet1"
# This bot will store the timestamp of the last comment it handled. This way it
# will know which comments have already been handled. This reference will
# be stored in the cell indicated here. See
# https://developers.google.com/sheets/guides/concepts#a1_notation to know how
# to reference cells. You can always hide these in a hidden sheet if you prefer
# them not being visible on the main sheet.
PREV_COMMENT_TIME_CELL = FLAIR_SHEET+"!J2"
# The bot will also store a reference to the previous thread it processed, this
# way it can handle any new comments in the old sticky before going to the new
# one. It will also make it easy for the bot to know when the "prev comment"
# value is no longer relevant, as the sticky thread changed.
PREV_THREAD_ID_CELL = FLAIR_SHEET+"!I2"
# if a user tries to enter a high combined verification count for a single user,
# the bot will not automatically update the count, but send an email to the
# moderators instead. E.g. with a threshold of 30, "/u/user 20 20" will not
# automatically be added, but will cause a mail to be sent to the mods instead.
COUNT_THRESHOLD_USER = 10
# The same as before, except it will look if the total verification count of a
# comment is high, instead of just the verified count for a single user.
COUNT_THRESHOLD_COMMENT = 30
# A list of mods that will be mailed if the bot encounters something fishy. If
# the list is empty, no mods will be mailed. Usernames are always written
# without the /u/ part. To send to all moderators of a subreddit, write
# /r/subreddit_name
#MODS = ["wmeacham", ]
#MODS = ["/r/penpals", ]
MODS = ["/r/BitwiseShiftTest"]
# A definition of all ranks in descending order of prestige. The first value
# must be the flair CSS class, the second is a function determining whether
# a user has met the conditions for this flair. I guessed these, change them
# to their actual values! The requirements function gets passed a row from the
# datasheet, whose values are all strings.
RANKS = OrderedDict([
	("combogold", lambda row: int(row[1]) >= 300 and int(row[2]) >= 100),
	("goldsnail", lambda row: int(row[1]) >= 300 or int(row[2]) >= 100),
	("combosilver", lambda row: int(row[1]) >= 30 and int(row[2]) >= 10),
	("silversnail", lambda row: int(row[1]) >= 30 or int(row[2]) >= 10),
	("combobronze", lambda row: int(row[1]) >= 3 and int(row[2]) >= 1),
	("bronzeemail", lambda row: int(row[1]) >= 3 or int(row[2]) >= 1),
	("", lambda row: True),	# Catch-all, for people who have not met any requirements yet.
])
# Wether to allow users to verify themselves.
ALLOW_SELF_VERIFICATION = False

#
# Actual bot code
#

# Read configuration file if one is given.
if len(argv) == 2:
	try:
		with open(argv[1], "r") as f:
			exec(f.read())
	except FileNotFoundError as e:
		print("[ERROR] The config file could not be found.")
		raise e
	except Exception as e:
		print("[ERROR] The config file contains error.")
		raise e
elif len(argv) > 2:
	print("[Error] Correct syntax: {} [config_file]".format(argv[0]))
	exit()

class colors:
	# Adapted from https://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
	OK = "\033[92m\033[1m"
	WARNING = "\033[93m\033[1m"
	ERROR = "\033[91m\033[1m"
	INPUT = "\033[94m\033[1m"
	ENDC = "\033[0m"

def get_credentials():
	"""	Gets valid user credentials from storage.
		If nothing has been stored, or if the stored credentials are invalid,
		the OAuth2 flow is completed to obtain the new credentials.

		Returns:
		Credentials, the obtained credential.
	"""
	store = Storage("sheets.googleapis.com-python-penpalsbot.json")
	credentials = store.get()
	if not credentials or credentials.invalid:
		flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
		flow.user_agent = APPLICATION_NAME
		credentials = tools.run_flow(flow, store)
	return credentials

def get_verification_thread():
	print("Looking for new verification sticky...")
	print("Checking top sticky...", end="")
	sticky = r.get_sticky(SUBREDDIT, True)
	if "verification post" not in sticky.title.lower():
		print("\nChecking bottom sticky...", end="")
		sticky = r.get_sticky(SUBREDDIT, False)
		if "verification post" not in sticky.title.lower():
			sticky = None
	print(colors.OK+" DONE"+colors.ENDC)
	return sticky

def message_mods(subject, message):
	""" Lazy loaded send mods a message. """
	return
	global MODS
	for mod in MODS:
		try:
			r.send_message(mod, subject, message, from_sr=SUBREDDIT)
		except praw.errors.Forbidden:
			print(colors.WARNING+"[WARNING]"+colors.ENDC+" Unable to send a message to /u/{}".format(mod))

def expand_comments(thread):
	""" Expand top-level comments to the point where there are no more comments.
		Returned comments are sorted descendingly by time.
	"""
	comments = list(thread.comments)
	oldLen = 0
	newLen = len(comments)
	while newLen != oldLen:
		oldLen = newLen
		thread.replace_more_comments()
		comments = list(thread.comments)
		newLen = len(comments)
	comments.sort(key=lambda x: int(x.created_utc), reverse=True)
	return comments

def total_verification_count(verifications):
	return reduce(lambda a, x: a + int(x["mail_count"]) + int(x["letter_count"]), verifications, 0)

def process_comments(thread, prev_comment_time=0):
	""" Process comments for verification strings.
		Returns the UTC timestamp of the last processed comment. If no new
		comments were found, returns None.
	"""
	comments = expand_comments(thread)
	if not comments or int(comments[0].created_utc) <= prev_comment_time:
		print("No new comments found.")
		return None
	for comment in comments:
		if int(comment.created_utc) <= prev_comment_time:
			break
		print("+ Handling new comment. ID={}".format(comment.id))
		verifications = []	# Stores all verifications of a comment until it is processed.
		error_occurred = False
		# Start looking for verification count strings.
		paragraphs = comment.body.splitlines()
		for paragraph in paragraphs:
			match = RE_VERIFICATION_SYNTAX.match(paragraph)
			if match:
				print("... Verification count string found: "+paragraph)
				# Add user to added_count if he wasn't in there yet.
				data = match.groupdict()
				if not ALLOW_SELF_VERIFICATION and comment.author.name == data["username"]:
					print("... "+colors.WARNING+"[WARNING]"+colors.ENDC+" Trying to verify himself. Ignoring and messaging mods.")
					message_mods("Self-verification", """
					It appears [a user]({}) is attempting to verify themselves.
					This comment has been ignored and will have to be manually
					verified.
					""".format(comment.permalink))
					error_occurred = True
					break
				data["mail_count"] = int(data["mail_count"])
				data["letter_count"] = int(data["letter_count"])
				# Check if the COUNT_THRESHOLD_USER hasn't been exceeded.
				if data["mail_count"] + data["letter_count"] >= COUNT_THRESHOLD_USER:
					print("... "+colors.WARNING+"[WARNING]"+colors.ENDC+" High verification count for a single user. Ignoring and messaging mods.")
					message_mods("Verification count threshold exceeded", """
					It appears [a comment]({}) is attempting to verify a large
					email and/or letter count for a single user. This comment
					has been ignored and will have to be manually verified.
					""".format(comment.permalink))
					error_occurred = True
					break
				else:
					verifications.append(data)
		# Only verify the comment threshold id the user threshold wasn't exceeded.
		if not error_occurred:
			# Check the comment threshold.
			if total_verification_count(verifications) > COUNT_THRESHOLD_COMMENT:
				print("... "+colors.WARNING+"[WARNING]"+colors.ENDC+" High verification count for a single user. Ignoring and messaging mods.")
				message_mods("Verification count threshold exceeded", """
				It appears [a comment]({}) is attempting to verify a large
				email and/or letter count for a single user. This comment
				has been ignored and will have to be manually verified.
				""")
			else:
				# No errors, apply the verification counts.
				for data in verifications:
					global added_count
					if data["username"] not in added_count:
						added_count[data["username"]] = {"mail_count": 0, "letter_count": 0}
					added_count[data["username"]]["mail_count"] += data["mail_count"]
					added_count[data["username"]]["letter_count"] += data["letter_count"]
	return int(comments[-1].created_utc)

def get_flair_css_class(spreadsheet_row):
	for flair_css_class, requirement_fun in RANKS.items():
		if requirement_fun(spreadsheet_row):
			return flair_css_class

def recompute_spreadsheet_data(spreadsheet_data, added_count):
	""" Recomputes all values in the spreadsheet.
		Returns all flairs that changed during the recompute in the form
		[{username: string, flair: string}]
	"""
	changed_flairs = {}		# Will contain values {username: flairstring}
	for i, (username, mail_count, letter_count, flair_class) in enumerate(spreadsheet_data[1:], 1):
		if username[3:] in added_count:
			spreadsheet_data[i][1] = str(int(spreadsheet_data[i][1]) + added_count[username[3:]]["mail_count"])
			spreadsheet_data[i][2] = str(int(spreadsheet_data[i][2]) + added_count[username[3:]]["letter_count"])
			new_flair_class = get_flair_css_class(spreadsheet_data[i])
			# Only bother updating flairs that changed, paying special attention to n/a's that are non-existant flair classes.
			if new_flair_class != flair_class and not (not new_flair_class and flair_class == "n/a"):
				spreadsheet_data[i][3] = new_flair_class
				changed_flairs[username[3:]] = new_flair_class
			del added_count[username[3:]]
	# Append all users that haven't been handled yet to the end of the spreadsheet.
	for username, counts in added_count.items():
		new_flair_class = get_flair_css_class(spreadsheet_data[i])
		changed_flairs[username[3:]] = new_flair_class
		spreadsheet_data.append(["/u/"+username, counts["mail_count"], counts["letter_count"], new_flair_class])
	return changed_flairs

def update_flairs(changed_flairs):
	print("Updating flairs...")
	after = None

	flairs = []
	unknowns = []
	# Update existing flairs.
	for username, flair_css_class in changed_flairs.items():
		print("... /u/{} <- {}".format(username, flair_css_class))
		try:
			flair = r.get_flair(SUBREDDIT, username)
		except praw.errors.Forbidden:
			print(colors.ERROR+"[ERROR]"+colors.ENDC+" Could not retrieve flair information! Does the bot have mod privileges?".format(username))
			exit()
		if flair == None:
			print(colors.WARNING+"[WARNING]"+colors.ENDC+" /u/{} does not exist!".format(username))
			# TODO: do something more with the knowledge that the user doesn't exists?
			continue
		# The empty flair texts (None) need to be converted to empty strings.
		if flair["flair_css_class"] == None:
			flair["flair_css_class"] = ""
		if flair["flair_text"] == None:
			flair["flair_text"] = ""
		# Validate the flair classes with those defined.
		if flair["flair_css_class"] not in RANKS and flair["flair_css_class"] not in unknowns:
			unknowns.append(flair_css_class)
			print(colors.ERROR+"[ERROR]"+colors.ENDC+' Encountered an unknown CSS flair class "{}".'.format(flair["flair_css_class"]))
		print("... Queuing flair {} for /u/{}".format(flair_css_class, flair["user"]))
		flair["flair_css_class"] = flair_css_class
		flairs.append(flair)
	# Only give a warning the first time an unknown class is encountered.
	if unknowns:
		input(colors.INPUT+"[INPUT NEEDED]"+colors.ENDC+" There were some unknown CSS flair classes!\n"
			"			   This probably means you forgot to define that class.\n"
			"			   Close the progam now and fix the error, or press ENTER\n"
			"			   to continue, ignoring any errors.")
	while flairs:
		# Update flairs.
		to_i = min(flairs.len, 100)
		r.set_flair_csv(SUBREDDIT, flairs[:to_i])
		print("Succesfully changed a section of the user's flairs")
		flairs = flairs[to_i:]
	print(colors.OK+"[SUCCESS]"+colors.ENDC+" Updated all user's flairs")

# Google Sheets API
SCOPES = "https://www.googleapis.com/auth/spreadsheets"
CLIENT_SECRET_FILE = "client_secret.json"
APPLICATION_NAME = "Google Sheets API Python Quickstart"
DATA_RANGE = FLAIR_SHEET+"!A:D"
# Global variables
SUBREDDIT = "penpals"
# Regular expressing expressing the syntax of the command.
RE_VERIFICATION_SYNTAX = re.compile(
		r"\A\s*"						# Any leading whitespace.
		# Reddit username, based on the Reddit source code.
		# First / is optional, as people seemed to forget it occasionally.
		# Entire /u/ part can be left out as well.
		r"(/?u/)?(?P<username>[\w\-]+)"
		r"[\s,_\-+]+"					# Delimiters.
		r"(?P<mail_count>[0-9]+)"
		r"[\s,_\-+]+"					# Delimiters.
		r"(?P<letter_count>[0-9+]+)"
		r"\s*\Z",						# Any trailing whitespace.
		re.UNICODE)

print("Authenticating with Docs API...", end="")
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl = ("https://sheets.googleapis.com/$discovery/rest?version=v4")
service = discovery.build("sheets", "v4", http=http,
 						  discoveryServiceUrl=discoveryUrl)
print(colors.OK+" DONE"+colors.ENDC)

print("Authenticating with Reddit...", end="")
r = praw.Reddit("Python:PenpalsVerification by /u/BitwiseShift")
r.config.api_request_delay = 1.0
o = OAuth2Util.OAuth2Util(r)
o.refresh()
print(colors.OK+" DONE"+colors.ENDC)

# Keeps tracks of added counts per user.
added_count = {}		# Will contain count in the form {username: {mail_count: int, letter_count: int}}.

print("Getting spreadsheet data... ", end="")
prev_thread_id, prev_comment_time, spreadsheet_data = service.spreadsheets().values().batchGet(
		spreadsheetId=SPREADSHEET_ID, ranges=[PREV_THREAD_ID_CELL, PREV_COMMENT_TIME_CELL, DATA_RANGE]).execute()["valueRanges"]
# Set default values.
prev_thread_id = prev_thread_id.get("values", [[None]])[0][0]
prev_comment_time = int(prev_comment_time.get("values", [[0]])[0][0])
spreadsheet_data = spreadsheet_data.get("values", [])
# Pad rows, as Google leaves out empty rows at the end, i.e. empty css class strings.
spreadsheet_data = [row + [""]*(4-len(row)) for row in spreadsheet_data]

if prev_thread_id == None:
	print("No previous thread run found.")
	prev_comment_time = 0
	comment_time1 = None
else:
	print("Checking for new comments in previous thread run...")
	prev_thread = r.get_submission(submission_id=prev_thread_id)
	comment_time1 = process_comments(prev_thread, prev_comment_time)
	prev_comment_time = comment_time1 if comment_time1 else prev_comment_time

sticky = get_verification_thread()
# Check if the sticky was found.
if sticky:
	print("Verification thread found!")
else:
	print("Could not find new the verification thread!")
# Check if the sticky is different from the previous thread we processed.
if sticky.id == prev_thread_id:
	print("The verification thread hasn't changed!")
	comment_time2 = None
else:
	comment_time2 = process_comments(sticky, prev_comment_time)
	prev_comment_time = comment_time2 if comment_time2 else prev_comment_time
	prev_thread_id = sticky.id
# Only continue further if some new comments were found.
if comment_time1 or comment_time2:
	print("Handled all comments!")
	print("Computing new counts and flairs...", end="")
	changed_flairs = recompute_spreadsheet_data(spreadsheet_data, added_count)
	print(colors.OK+" DONE"+colors.ENDC)
	if changed_flairs:
		try:
			update_flairs(changed_flairs)
			print(colors.OK+" DONE"+colors.ENDC)
		except praw.errors.Forbidden:
			print(colors.ERROR+"\n[ERROR]"+colors.ENDC+" Unable to update the user's flairs! Does the bot have mod privileges?")
			exit()
	else:
		print("No flairs changed!")

	print("Writing persistence values to spreadsheet...", end="")
	data = [
		{"range": PREV_THREAD_ID_CELL, "values": [[prev_thread_id]]},
		{"range": PREV_COMMENT_TIME_CELL, "values": [[str(prev_comment_time)]]},
		{"range": DATA_RANGE, "values": spreadsheet_data}
	]
	service.spreadsheets().values().batchUpdate(
		body={"valueInputOption": "USER_ENTERED", "data": data},
		spreadsheetId=SPREADSHEET_ID).execute()
	print(colors.OK+" DONE"+colors.ENDC)
else:
	print("No new comments!")
print(colors.OK+"[SUCCESS]"+colors.ENDC+" Script finished!")


# -*- coding: utf-8 -*-
"""
Created on Tue Aug  4 06:23:00 2020

@author: niili

2_7 обчучение перцептрона

Реализуйте метод vectorized_forward_pass класса Perceptron.
n — количество примеров, m — количество входов. Размерность входных данных
input_matrix — (n, m), размерность вектора весов — (m, 1), смещение (bias) —
отдельно. vectorized_forward_pass должен возвращать массив формы (n, 1),
состоящий либо из 0 и 1, либо из True и False.

Обратите внимание, необходимо векторизованное решение, то есть без циклов и
операторов ветвления.

Первое задание из
https://nbviewer.jupyter.org/github/stacymiller/stepic_neural_networks_public/blob/master/HW_1/Hw_1_student_version.ipynb
"""

import numpy as np

class Perceptron:

    def __init__(self, w, b):
        """
        Инициализируем наш объект - перцептрон.
        w - вектор весов размера (m, 1), где m - количество переменных
        b - число
        """

        self.w = w
        self.b = b

    def forward_pass(self, single_input):
        """
        Метод рассчитывает ответ перцептрона при предъявлении одного примера
        single_input - вектор примера размера (m, 1).
        Метод возвращает число (0 или 1) или boolean (True/False)
        """

        result = 0
        for i in range(0, len(self.w)):
            result += self.w[i] * single_input[i]
        result += self.b

        if result > 0:
            return 1
        else:
            return 0

    def vectorized_forward_pass(self, input_matrix):
        """
        Метод рассчитывает ответ перцептрона при предъявлении набора примеров
        input_matrix - матрица примеров размера (n, m), каждая строка - отдельный пример,
        n - количество примеров, m - количество переменных
        Возвращает вертикальный вектор размера (n, 1) с ответами перцептрона
        (элементы вектора - boolean или целые числа (0 или 1))
        """

        ## Этот метод необходимо реализовать
        #pass
        # result
        inp_shape = np.shape(input_matrix)
        result = np.zeros((inp_shape[0], 1))

        # culc result
        for i in range(0, inp_shape[0]):
            # sum function
            res_i = np.dot(input_matrix[[i][:]], self.w) + self.b

            # activate function
            if res_i > 0:
                result[[i],[0]] = 1
            else:
                result[[i],[0]] = 0

        return result

    def train_on_single_example(self, example, y):
        """
        принимает вектор активации входов example формы (m, 1)
        и правильный ответ для него (число 0 или 1 или boolean),
        обновляет значения весов перцептрона в соответствии с этим примером
        и возвращает размер ошибки, которая случилась на этом примере до изменения весов (0 или 1)
        (на её основании мы потом построим интересный график)
        """

        ## Этот метод необходимо реализовать
        # culc perceptron answer
        if (np.dot(np.transpose(example), self.w) + self.b) > 0:
            predict = 1
        else:
            predict = 0

        # correct the weights
        if predict != y:
            if predict == 1:
                self.w = self.w - example
                self.b = self.b - 1
            else:
                self.w = self.w + example
                self.b = self.b + 1
        # return the errror
        return abs(predict - y)

#------------------------------------------------------------------------------
#------------------------------------------------------------------------------

# for testing
#------------------------------------------------------------------------------
#

# def vectorized_forward_pass(input_matrix):
#         """
#         Метод рассчитывает ответ перцептрона при предъявлении набора примеров
#         input_matrix - матрица примеров размера (n, m), каждая строка - отдельный пример,
#         n - количество примеров, m - количество переменных
#         Возвращает вертикальный вектор размера (n, 1) с ответами перцептрона
#         (элементы вектора - boolean или целые числа (0 или 1))
#         """
#         w = np.ones((3,1))
#         b = 0
#         ## Этот метод необходимо реализовать
#         # result
#         inp_shape = np.shape(input_matrix)
#         result = np.zeros((inp_shape[0], 1))

#         # culc result
#         for i in range(0, inp_shape[0]):
#             # sum function
#             res_i = np.dot(input_matrix[[i][:]], w) + b

#             # activate function
#             if res_i > 0:
#                 result[[i],[0]] = 1
#             else:
#                 result[[i],[0]] = 0

#         return result

# x = np.array([[1, 2, 3]])

# print(vectorized_forward_pass(x))

#------------------------------------------------------------------------------
def train_on_single_example(example, y):
    """
    принимает вектор активации входов example формы (m, 1)
    и правильный ответ для него (число 0 или 1 или boolean),
    обновляет значения весов перцептрона в соответствии с этим примером
    и возвращает размер ошибки, которая случилась на этом примере до изменения весов (0 или 1)
    (на её основании мы потом построим интересный график)
    """

    w = np.ones((3,1))
    b = 0
    ## Этот метод необходимо реализовать
    if (np.dot(np.transpose(example), w) + b) > 0:
        predict = 1
    else:
        predict = 0

    if predict != y:
        if predict == 1:
            w = w - example
            b = b - 1
        else:
            w = w + example
            b = b + 1
    return abs(predict - y)


"""
    CEM Agent class, which takes in a PolicyModel object

[1] Learning Tetris with the Noisy Cross-Entropy Method
    (Szita, Lorincz 2006)
    pdf: http://nipg.inf.elte.hu/publications/szita06learning.pdf
"""

from yarlp.agent.base_agent import Agent
from yarlp.model.model_factories import cem_model_factory
from yarlp.agent.base_agent import do_rollout
from yarlp.model.networks import mlp

import numpy as np

class CEMAgent(Agent):
    """
    Cross Entropy Method

    where Z_t = max(alpha - t / beta) from equation (2.5)

    Parameters
    ----------
    policy_model : model.Model

    n_weight_samples : integer
        Total number of sample weights to draw during each training step

    init_var : float, default 0.1

    best_pct : float, default 0.2
        The percentage of sample weights to keep that yield the best reward
    """

    def __init__(self, env, n_weight_samples=100,
                 init_var=1., best_pct=0.2,
                 policy_network=None,
                 policy_network_params={},
                 model_file_path=None,
                 min_std=1e-6, init_std=1.0, adaptive_std=False,
                 *args, **kwargs):
        super().__init__(env, *args, **kwargs)

        if policy_network is None:
            policy_network = mlp

        self._policy = cem_model_factory(
            env, policy_network, policy_network_params,
            min_std=min_std, init_std=init_std,
            adaptive_std=adaptive_std,
            model_file_path=model_file_path)

        self.tf_object_attributes.add('_policy')

        # Get model shapes
        theta = self._policy.G(self._policy['gf'])
        self.model_total_sizes = theta.shape[0]

        # Mean and SD of our weights
        self._theta = np.zeros(self.model_total_sizes)
        self._sigma = np.ones(self.model_total_sizes) * init_var

        # Total number of sample weights to draw for each training step
        self.n_weight_samples = n_weight_samples

        # Number of best parameters to keep
        assert best_pct <= 1 and best_pct > 0
        self.num_best = int(best_pct * self.n_weight_samples)

    def train(self, num_train_steps=1, num_test_steps=0,
              max_timesteps=0, n_steps=None,
              with_variance=False, alpha=5, beta=10,
              min_sigma=1e-2, render=False):
        """
        Learn the most optimal weights for our PolicyModel
            optionally with a variance adjustment as in [1]
            as Z_t = max(alpha - t / beta)

        Parameters
        ----------
        num_training_steps : integer
            Total number of training steps

        max_timesteps : integer
            maximum number of total steps to execute in the environment

        with_variance : boolean
            train with or without a variance adjustment as in [1]

        alpha : float, default 5
            parameter for variance adjustment (Z_t = max(alpha - t / beta))

        beta : float, default 10
            parameter for variance adjustment (Z_t = max(alpha - t / beta))

        render : bool, whether to render episodes in a video

        Returns
        ----------
        None
        """

        assert sum([num_train_steps > 0,
                    max_timesteps > 0]) == 1,\
            "Must provide at least one limit to training"

        timesteps_so_far = 0
        train_steps_so_far = 0

        rollout_gen = do_rollout(
            self, self._env, n_steps, greedy=False,
            render=render)

        while True:

            if max_timesteps and timesteps_so_far >= max_timesteps:
                break
            elif num_train_steps and train_steps_so_far >= num_train_steps:
                break

            # logger.info('Training step {}'.format(i))
            if np.any(self._sigma <= 0):
                self._sigma[self._sigma <= 0] = min_sigma

            # generate n_samples each iteration with new mean and stddev
            # according to our current optimal mean and variance
            weight_samples = np.array(
                [np.random.normal(mean, np.sqrt(var), self.n_weight_samples)
                 for mean, var in zip(self._theta, self._sigma)])
            weight_samples = weight_samples.T

            # get the rewards for each mean/variance
            rollouts = []
            rollout_rewards = []
            for w in weight_samples:
                # add weights to PolicyModel
                self._policy.G(
                    self._policy['sff'],
                    {self._policy['theta']: w})
                rollout = rollout_gen.__next__()
                timesteps_so_far += len(rollout['dones'])
                rollouts.append(rollout)
                rollout_rewards.append(np.sum(rollout['rewards']))

            # get the num_best mean/var with highest reward
            rollout_rewards = np.array(rollout_rewards)
            self.logger.set_metrics_for_rollout(rollouts, train=True)
            self.logger.log()

            best_idx = rollout_rewards.argsort()[::-1][:self.num_best]
            best_samples = weight_samples[best_idx]

            # recompute our mean/var of our weights
            mean = best_samples.mean(axis=0)
            var = best_samples.var(axis=0)

            if with_variance:
                var += max(alpha - train_steps_so_far / float(beta), 0)

            self._theta = mean
            self._sigma = var

            if num_test_steps > 0:
                r = []
                for t_test in range(num_test_steps):
                    rollout = self.rollout(greedy=True)
                    r.append(rollout)
                self.logger.set_metrics_for_rollout(r, train=False)
                self.logger.log()

            if self.logger._log_dir is not None:
                self.save_models(self.logger._log_dir)

            train_steps_so_far += 1

        return


"""
Document Scanner
Algo :  Take input from webcom
        Preprocess Image and return as Threshold image
        Find the biggest contour
        Using corner points to get bird eye view

"""

import cv2
import numpy as np

window_width = 600
window_height = 350
webcam = cv2.VideoCapture(0)   # Selecting webcam
webcam.set(3,window_width)   # Adjust Width
webcam.set(4,window_height)   # Adjust Height
webcam.set(10,150)   # Adjust Brightness

def imagePreProcessing(img):
    imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    imgBlur = cv2.GaussianBlur(imgGray,(5,5),1)
    imgCanny = cv2.Canny(imgBlur,100,100)
    kernal = np.ones([5,5])
    imgDilate = cv2.dilate(imgCanny,kernal,iterations=1)
    imgErode = cv2.erode(imgDilate, kernal, iterations=1)
    return imgDilate

def getContours(img):
    biggest_box = np.array([])
    maxArea = 0
    contours,hierarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    x, y, w, h = 0, 0, 0, 0
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area>5000:
            # cv2.drawContours(imgCountour, cnt, -1, (255, 0, 0),3)
            parameter = cv2.arcLength(cnt, True)
            approx = cv2.approxPolyDP(cnt, 0.02 * parameter, True)  ## Find approximation of our corner points
            # print(len(approx))
            if len(approx) == 4 and area > maxArea:
                biggest_box = approx
                maxArea = area
    cv2.drawContours(imgCountour, biggest_box, -1, (255, 0, 0),20)
    return biggest_box

def reorder(points):
    points = points.reshape((4,2))
    newPoints = np.zeros((4,1,2),np.int32)

    add = points.sum(1)
    newPoints[0] = points[np.argmin(add)]    ## setting point [0,0]
    newPoints[3] = points[np.argmax(add)]   ## setting point [width,height]

    diff = np.diff(points, axis=1)
    newPoints[1] = points[np.argmin(diff)]  ## setting point [width,0]
    newPoints[2] = points[np.argmax(diff)]  ## setting point [0,height]
    return newPoints

def wrap(img,biggestContourPoint):
    biggestContourPoint = reorder(biggestContourPoint)
    width = window_width
    height = window_height
    pts1 = np.float32(biggestContourPoint)
    pts2 = np.float32([[0,0],[width,0],[0,height],[width,height]])
    matrix = cv2.getPerspectiveTransform(pts1, pts2)
    imgOutput = cv2.warpPerspective(img, matrix, (width, height))
    return imgOutput

def stackImages(scale,imgArray):
    rows = len(imgArray)
    cols = len(imgArray[0])
    rowsAvailable = isinstance(imgArray[0], list)
    width = imgArray[0][0].shape[1]
    height = imgArray[0][0].shape[0]
    if rowsAvailable:
        for x in range ( 0, rows):
            for y in range(0, cols):
                if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
                else:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
                if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
        imageBlank = np.zeros((height, width, 3), np.uint8)
        hor = [imageBlank]*rows
        hor_con = [imageBlank]*rows
        for x in range(0, rows):
            hor[x] = np.hstack(imgArray[x])
        ver = np.vstack(hor)
    else:
        for x in range(0, rows):
            if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
                imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
            else:
                imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
            if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
        hor= np.hstack(imgArray)
        ver = hor
    return ver

while True:
    _,img = webcam.read()
    img = cv2.resize(img, (500, 200))
    imgCountour = img.copy()
    imgThresh = imagePreProcessing(img)
    biggestContour = getContours(imgThresh)
    # print(biggestContour,biggestContour.shape)
    imgStagedArray = []
    if biggestContour.size != 0:
        imgWrapOutput = wrap(img, biggestContour)
        cv2.imshow("Document Scanner", imgWrapOutput)
        imgStagedArray = [[img,imgThresh],[imgCountour,imgWrapOutput]]
    else:
        imgStagedArray = [[img,imgThresh],[img,img]]

    stackedImg = stackImages(0.6, imgStagedArray)
    cv2.imshow("Workflow", stackedImg)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break


# -*- coding: utf-8 -*-
"""
Created on Mon Mar 20 13:57:06 2017
"""
cube = 27
epsilon = 0.01
guess = 0.0
increment = 0.001
numguess = 0

while abs(guess**3 - cube) >= epsilon and guess <= cube:
    guess += increment
    numguess += 1
print("num of guesses=", numguess)
if abs(guess**3 - cube) >= epsilon:
    print("Failed on cube root of", cube)
else:
    print(guess, "is close to cube root of", cube)

MITx6.001x/WEEK2 - Simple Programs/Excercises/cube_approx.py


# -*- coding: utf-8 -*-

from django.http import JsonResponse
from django.db.models import Count, F, Case, When, Value, IntegerField

from what.models import Composer, Instrument
from api.shared.tools import get_instrument_parent_category
from Octoopus.shared.tools import load_config

def search_composers(request):

    data = {}

    if request.method == "POST":
        all_composers = []

        # Param for the request
        request_id = int(request.POST.get('request_id')) if request.POST.get('request_id') else 0
        page = int(request.POST.get('page')) if int(request.POST.get('page')) > 0 else 1
        name = str(request.POST.get('name')) if request.POST.get('name') else ''

        if request.POST.get('order_by') and str(request.POST.get('order_by')) == 'default':
            order_by = 'is_popular'
            ordering = 'descending'
        else:
            order_by = str(request.POST.get('order_by')) if request.POST.get('order_by') else 'name'
            ordering = 'descending' if request.POST.get('ordering') == 'on' else 'ascending'

        # Null safety for dates
        dates = {}
        for date in ['min_birth','max_birth','min_death', 'max_death']:
            try:
                dates[date] = int(request.POST.get(date))
            except:
                dates[date] = -1

        # Request
        all_composers = Composer.objects.annotate(works_quantity=Count('work'))

        # ######################### Filters #########################
        # Name
        all_composers = all_composers.filter(name__contains=name) | all_composers.filter(first_name__contains=name)

        # Popular / Essential options
        if request.POST.get('is_popular') and request.POST.get('is_popular') == 'on':
            all_composers = all_composers.filter(is_popular=True)
        if request.POST.get('is_essential') and request.POST.get('is_essential') == 'on':
            all_composers = all_composers.filter(is_essential=True)

        # Dates
        if dates['min_birth'] != -1:
            all_composers = all_composers.filter(birth__year__gte=dates['min_birth'])
        if dates['max_birth'] != -1:
            all_composers = all_composers.filter(birth__year__lte=dates['max_birth'])
        if dates['min_death'] != -1:
            all_composers = all_composers.filter(death__year__gte=dates['min_death'], death__isnull=False)
        if dates['max_death'] != -1:
            all_composers = all_composers.filter(death__year__lte=dates['max_death'], death__isnull=False)

        # ######################### Ordering ########################
        if ordering == 'ascending':
            all_composers = all_composers.order_by(F(order_by).asc(nulls_last=True))
        else:
            all_composers = all_composers.order_by(F(order_by).desc(nulls_last=True))

        # Patches
        if order_by == 'death':
            all_composers = all_composers.filter(death__isnull=False)

        # Pagination & final results ########################
        config = load_config('what')
        res_per_page = int(config['composers']['results_per_page'])
        page_composers = all_composers[((page-1)*res_per_page):(page*res_per_page)]

        page_results = []

        for composer in page_composers:
            page_results.append({
                'id': composer.id,
                'name': composer.name,
                'first_name': composer.first_name,
                'slug': composer.slug,
                'portrait': composer.portrait,
                'works_quantity': composer.works_quantity,
            })

        data = {
            'request_id': request_id,
            'page': page,
            'total_count': len(all_composers),
            'page_count': len(page_results),
            'composers': page_results,
            'results_per_page': res_per_page
        }

    return JsonResponse(data)

def search_instruments(request):

    data = {}

    if request.method == "POST":
        # Param for the request
        request_id = int(request.POST.get('request_id')) if request.POST.get('request_id') else 0
        search = str(request.POST.get('search')) if request.POST.get('search') else ''

        all_instruments = Instrument.objects.all()
        search_terms = [term for term in search.split(' ') if term != ""]

        # Todo: ajouter un exact_results

        best_results = all_instruments.filter(name__contains=search_terms[0])
        for i in range(1, len(search_terms)):
            best_results = best_results & all_instruments.filter(name__contains=search_terms[i])

        print(best_results)

        all_results = best_results

        # all_results = (
        #     Instrument.objects
        #     .filter(best_results | all_instruments)
        #     .annotate(
        #         search_ordering=Case(
        #             When(best_results, then=Value(2)),
        #             When(all_instruments, then=Value(1)),
        #             default=Value(-1),
        #             output_field=IntegerField(),
        #         )
        #     ).order_by('-search_ordering', ...)
        # )

        print(all_results)

        # ORDER & LIMIT
        all_results = all_results[:5]

        propositions = []
        for instrument in all_results:
            propositions.append({
                'id': instrument.id,
                'name': instrument.name,
                'full_path': get_instrument_parent_category(instrument.id),
            })

        data = {
            'request_id': request_id,
            'search_terms': search_terms,
            'total_count': len(propositions),
            'propositions': propositions,
        }

    return JsonResponse(data)


import hashlib
import itertools
import json
import os
import time
from typing import Any, Dict, List

import googlemaps

PLACE_TYPES = [
    "Gym",
    "Park",
    "Café",
    "Supermarket",
    "Restaurant",
    "Vegetarian Restaurant",
    "Burger Restaurant",
]
DISTRICT_NAMES = [
    "Neuehrenfeld, Cologne, Germany",
    "Sülz, Cologne, Germany",
    "Raderthal, Cologne, Germany",
    "Müngersdorf, Cologne, Germany",
    "Südstadt, Cologne, Germany",
    "Zollstock, Cologne, Germany",
]

TEST_TRIGGER = {
    "place_types": ["Restaurant"],
    "district_names": ["Neuehrenfeld, Cologne, Germany"],
}
FIVETRAN_TRIGGER = {"agent": "Test", "state": {}, "secrets": {}}

class MapsData:
    def __init__(self, api_key: str) -> None:
        self.gmaps = googlemaps.Client(key=api_key)

    def get_places_table(
        self, place_type: str, district_name: str
    ) -> List[Dict[str, Any]]:
        """Requests places in a district from googlemaps API and assemples results in a flattened dictionary

        Args:
            place_type (str): Place type to search for, e.g. "Restaurant" or "Gym"
            district_name (str): Name of the district to search for, e.g. "Neuhrenfeld, Cologne, Germany"

        Returns:
            List[Dict]: List of places in or near the district
        """
        query = f"{place_type} near {district_name}"
        places = self.gmaps.places(query=query)

        places_table = []
        while "next_page_token" in places:

            for place in places["results"]:
                place_record = self._create_place_record(
                    place, place_type, district_name
                )
                places_table.append(place_record)

            time.sleep(2)
            places = self.gmaps.places(
                query=query, page_token=places["next_page_token"]
            )

        places_table = self._add_distances_from_center(places_table, district_name)

        return places_table

    @staticmethod
    def _create_place_record(
        place: Dict[str, Any], place_type: str, district_name: str
    ) -> Dict[str, Any]:
        place_record = {
            k: v
            for k, v in place.items()
            if k
            in [
                "place_id",
                "name",
                "formatted_address",
                "rating",
                "user_ratings_total",
            ]
        }
        place_id = place_record.pop("place_id")
        id_string = (
            (place_id + place_type + district_name).replace(",", "").replace(" ", "")
        )
        place_record["id"] = hashlib.md5(id_string.encode("utf-8")).hexdigest()
        place_record["gmaps_place_id"] = place_id
        place_record["location_lat"] = place["geometry"]["location"]["lat"]
        place_record["location_lng"] = place["geometry"]["location"]["lng"]
        place_record["query_place_type"] = place_type
        place_record["query_district_name"] = district_name
        return place_record

    def _add_distances_from_center(
        self, places_table: Dict[str, Any], district_name: str
    ) -> Dict[str, Any]:
        """Adds walking distances from district center from googlemaps Distance Matrix API to places in a district

        Args:
            places_table (Dict[str, Any]): Places in a district
            district_name (str): Name of the district to determine the center of

        Returns:
            Dict[str, Any]: Updated places table with distances from district center
        """
        location = self.gmaps.find_place(
            input=district_name, input_type="textquery", fields=["geometry"]
        )
        location_coordinates = tuple(
            location["candidates"][0]["geometry"]["location"].values()
        )
        place_coordinates = [
            (p["location_lat"], p["location_lng"]) for p in places_table
        ]
        distances = []
        for place_coordinates_chunk in _chunked_iterable(place_coordinates, 25):
            distances_from_center = self.gmaps.distance_matrix(
                origins=location_coordinates,
                destinations=list(place_coordinates_chunk),
                mode="walking",
            )["rows"][0]["elements"]
            for d in distances_from_center:
                distances.append(d["distance"]["value"])

        for i in range(len(places_table)):
            places_table[i]["distance_from_center"] = distances[i]

        return places_table

def main(request):
    # Get API key from trigger event
    config = request.get_json()
    place_types = config.get("place_types")
    district_names = config.get("district_names")

    # Use default values if place types and district names are not provided via the trigger event
    place_types = place_types if place_types else PLACE_TYPES
    district_names = district_names if district_names else DISTRICT_NAMES

    # Get data from maps API for all combinations of place type and district name
    maps_data = MapsData(api_key=os.environ["API_KEY"])
    places_table = []
    for place_type, district_name in itertools.product(place_types, district_names):
        places_table += maps_data.get_places_table(
            place_type=place_type, district_name=district_name
        )

    # Create response
    insert = {"places": places_table}
    response = _assemble_response_json(insert)

    return response, 200, {"Content-Type": "application/json"}

# Taken from https://alexwlchan.net/2018/12/iterating-in-fixed-size-chunks/
def _chunked_iterable(iterable, size):
    it = iter(iterable)
    while True:
        chunk = tuple(itertools.islice(it, size))
        if not chunk:
            break
        yield chunk

def _assemble_response_json(insert):
    response_dict = {
        "state": {},
        "schema": {"places": {"primary_key": ["id"]}},
        "insert": insert,
        "hasMore": False,
    }
    return json.dumps(response_dict, ensure_ascii=False)


# Implement a class for a stack that supports all the regular functions (push, pop)
# and an additional function of max() which returns the maximum element in the stack
# (return None if the stack is empty). Each method should run in constant time.

class MaxStack:
  def __init__(self):
      self.stack = []
      self.maxVal = [-1]
    # Fill this in.

  def push(self, val):
      if val > self.maxVal[-1]:
          self.maxVal.append(val)
      self.stack.append(val)
    # Fill this in.

  def pop(self):
    if len(self.stack)>0:
      popedVal = self.stack.pop()
      if popedVal == self.maxVal[-1]:
        self.maxVal.pop()
    else:
        return "no element found in stack"
    # Fill this in.

  def max(self):
    if len(self.stack)>0:
        return self.maxVal[-1]
    else :
        return "no element found in stack"

    # Fill this in.

s = MaxStack()
s.push(1)
s.push(2)
s.push(3)
s.push(2)
print(s.max())
# 3
s.pop()
s.pop()
print(s.max())
# 2


# -*- coding: utf-8 -*-
"""
Created on Fri Apr  6 15:21:50 2018

@author: Marcus
"""

import os
import fdmt_date as fda
import fdmt_data as fdt

def main():
    main_path=fdt.main_path
    bin_path=main_path+"bin/"

    ##################################################################################
    #Bat 01
    exec_query01=''
    for para in os.environ["Path"].split(";"):
        if para.replace("\\","").replace("\/","")[-9:]=="Anaconda3":
            exec_query01=para.replace("\\","/")+"/python.exe"
    exec_query02=main_path+"src/TP999_The_Process_Daily.py"
    exec_query03=main_path+"log/"+"RUNNING_LOG.log"
    exec_query04=main_path+"log/"+"ERROR_LOG.log"
    exec_query00=exec_query01+" "+exec_query02+" >> "+exec_query03+" 2>> "+exec_query04+"\n"

    bat_file=open(bin_path+"the_process.bat",'w+')

    bat_file.write("cd/\n")
    bat_file.write("c:\n")
    bat_file.write(exec_query00)

    bat_file.close()

    ##################################################################################
    #Bat 02
    bat_file=open(bin_path+"regular_update.bat",'w+')

    bat_file.write("cd/\n")
    bat_file.write("d:\n")
    bat_file.write("cd "+main_path+"\n")
    bat_file.write("git pull\n")
    bat_file.write("git add .\n")
    bat_file.write("git status\n")
    bat_file.write('''git commit -m "system regular upate"\n''')
    bat_file.write("git push origin master:develop")

    bat_file.close()

if __name__ == '__main__':
    main()


import argparse
import json
import os
import pickle
from pathlib import Path
import sqlite3
from tqdm import tqdm
import random

from utils.linking_process import SpiderEncoderV2Preproc
from utils.pretrained_embeddings import GloVe
from utils.datasets.spider import load_tables
# from dataset.process.preprocess_kaggle import gather_questions

def schema_linking_producer(test, train, table, db, dataset_dir):

    # load data
    test_data = json.load(open(os.path.join(dataset_dir, test)))
    train_data = json.load(open(os.path.join(dataset_dir, train)))

    # load schemas
    schemas, eval_foreign_key_maps = load_tables([os.path.join(dataset_dir, table)])

    # Backup in-memory copies of all the DBs and create the live connections
    for db_id, schema in tqdm(schemas.items(), desc="DB connections"):
        sqlite_path = Path(dataset_dir) / db / db_id / f"{db_id}.sqlite"
        source: sqlite3.Connection
        with sqlite3.connect(str(sqlite_path)) as source:
            dest = sqlite3.connect(':memory:')
            dest.row_factory = sqlite3.Row
            source.backup(dest)
        schema.connection = dest

    word_emb = GloVe(kind='42B', lemmatize=True)
    linking_processor = SpiderEncoderV2Preproc(dataset_dir,
            min_freq=4,
            max_count=5000,
            include_table_name_in_column=False,
            word_emb=word_emb,
            fix_issue_16_primary_keys=True,
            compute_sc_link=True,
            compute_cv_link=True)

    # build schema-linking
    for data, section in zip([test_data, train_data],['test', 'train']):
        for item in tqdm(data, desc=f"{section} section linking"):
            db_id = item["db_id"]
            schema = schemas[db_id]
            to_add, validation_info = linking_processor.validate_item(item, schema, section)
            if to_add:
                linking_processor.add_item(item, schema, section, validation_info)

    # save
    linking_processor.save()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--data_dir", type=str, default="./dataset/spider")
    args = parser.parse_args()

    # merge two training split of Spider
    spider_dir = args.data_dir
    split1 = "train_spider.json"
    split2 = "train_others.json"
    total_train = []
    for item in json.load(open(os.path.join(spider_dir, split1))):
        total_train.append(item)
    for item in json.load(open(os.path.join(spider_dir, split2))):
        total_train.append(item)
    with open(os.path.join(spider_dir, 'train_spider_and_others.json'), 'w') as f:
        json.dump(total_train, f)

    # schema-linking between questions and databases for Spider
    spider_dev = "dev.json"
    spider_train = 'train_spider_and_others.json'
    spider_table = 'tables.json'
    spider_db = 'database'
    schema_linking_producer(spider_dev, spider_train, spider_table, spider_db, spider_dir)


IMG_SIZE = 256
BATCH_SIZE = 4
EPOCHS = 100

IMG_TRAIN_DIR = 'src/train/img/'
LABLE_TRAIN_DIR = 'src/train/label/'
IMG_TEST_DIR = 'src/test/img/'
RESULT_DIR = 'src/test/results/'

BACKGROUND = [255, 255, 255]
CAR = [255, 0, 0]
WHEEL = [0, 0, 0]
LIGHTS = [255, 255, 0]
WINDOW = [0, 0, 255]
COLORS = [BACKGROUND, CAR, WHEEL, LIGHTS, WINDOW]
CLASSES = len(COLORS)

GENERATE_EXTRA_SAMPLES = True


# Exercício Python 068: Faça um programa que jogue par ou ímpar com o computador. O jogo só será interrompido quando o
# jogador perder, mostrando o total de vitórias consecutivas que ele conquistou no final do jogo.
from random import randint
cont = 0
while True:
    bot = randint(0, 10)
    n = int(input('Insira um numero: '))
    escolha = ' '
    while escolha not in 'PI':
        escolha = str(input('Par ou ímpar? [P/I]: ')).strip().upper()[0]
    soma = n + bot
    if soma % 2 == 0:
        if escolha in 'P':
            print('Voce venceu')
            print(f'O computador escolheu Ímpar e jogou {bot} e voce escolheu Par e jogou {n} e a soma é {soma}')
            cont += 1
        else:
            print('Voce perdeu')
            print(f'O computador escolheu Par e jogou {bot} e voce escolheu Ímpar e jogou {n} e a soma é {soma}')
            break
    else:
        if escolha in 'I':
            print('Voce venceu')
            print(f'O computador escolheu Par e jogou {bot} e voce escolheu Ímpar e jogou {n} e a soma é {soma}')
            cont += 1
        else:
            print('Voce perdeu')
            print(f'O computador escolheu Ímpar e jogou {bot} e voce escolheu Par e jogou {n} e a soma é {soma}')
            break
print(f'FIM DE JOGO\nVoce ganhou {cont} vezes')


import contextvars
import csv
import logging
from os import PathLike
from typing import Callable, Dict, Union, Optional

import xmltodict as xmltodict
from discord.ext.commands import Bot, Context

logger = logging.getLogger("bot.localization")

class LocalizationHandler(object):
    default_handler = None  # type: LocalizationHandler | None

    def __init__(self) -> None:
        self.languages = {}  # type: Dict[str, Language]
        self._current_locale = contextvars.ContextVar("_current_locale")
        self.fallback = "en"
        LocalizationHandler.default_handler = self

    def init_bot(self, bot: Bot, get_locale: Callable[[Context], str]):
        async def pre_hook_localization(ctx: Context):
            self.set_current_locale(get_locale(ctx))
        bot.before_invoke(pre_hook_localization)

    def _add_translation(self, key: str, lan: str, value: str):
        if lan not in self.languages:
            language = Language(lan)
            self.languages[lan] = language
        self.languages[lan][key] = value

    def load_from_csv(self, path: Union[PathLike, str]):
        with open(path, mode="r", encoding="utf8") as csv_file:
            reader = csv.DictReader(csv_file)
            for line in reader:
                for lan, val in line.items():
                    if lan == "key":
                        continue
                    self._add_translation(line["key"], lan, val)

    def load_from_xml(self, path: Union[PathLike, str]):
        logger.info("Adding localisation data %s", path)
        with open(path, mode="rb") as file:
            lang_dict = xmltodict.parse(file, encoding="UTF-8")
        for key, translations in lang_dict["translations"].items():
            for lang, value in translations.items():
                if type(value) != str:
                    raise LocalizationException(f"Value for {lang}:{key} is not a string, got {type(value)}:{value}")
                self._add_translation(key, lang, value)
        logger.info("Localisation data added")

    def set_current_locale(self, locale: str):
        self._current_locale.set(locale)

    def get_current_locale(self) -> str:
        lang = self._current_locale.get(self.fallback)
        return lang or self.fallback

    def get_text(self, key: str, raise_not_found=True) -> str:
        language = self.get_current_locale()
        if language not in self.languages:
            raise LanguageNotFoundException(f"Language '{language}' not found")
        try:
            return self.languages[language].get_translation(key)
        except TranslationNotFound:
            return self.languages[self.fallback].get_translation(key, raise_not_found)

    @classmethod
    def get_translation(cls, key: str, fallback: Optional[str] = None, raise_not_found=True):
        if cls.default_handler is None:
            return fallback
        return cls.default_handler.get_text(key, raise_not_found)

t_ = LocalizationHandler.get_translation

class Language(object):
    def __init__(self, name: str):
        self.name = name
        self.translations = {}  # type: Dict[str, str]

    def get_translation(self, key: str, raise_not_found=False) -> Optional[str]:
        if key in self.translations:
            return self.translations[key]
        if raise_not_found:
            raise TranslationNotFound(f"Key '{key}' not found for '{self.name}'")
        return None

    def __getitem__(self, item) -> str:
        return self.get_translation(item)

    def __setitem__(self, item, value):
        self.translations[item] = value

    def load_from_dict(self, data: Dict):
        for k, v in data.items():
            self.translations[k] = v

class LocalizationException(Exception):
    pass

class LanguageNotFoundException(Exception):
    pass

class TranslationNotFound(Exception):
    pass


# 导入操作系统库
import os
# 更改工作目录
os.chdir(r"D:\softwares\applied statistics\pythoncodelearning\chap3\sourcecode")
# 导入绘图库
import matplotlib.pyplot as plt
# 导入支持向量机模型
from sklearn import svm
# 导入决策边界可视化工具
from sklearn.inspection import DecisionBoundaryDisplay
# 导入iris数据集
from sklearn.datasets import load_iris
# 导入绘图库中的字体管理包
from matplotlib import font_manager
# 实现中文字符正常显示
font = font_manager.FontProperties(fname=r"C:\Windows\Fonts\SimKai.ttf")
# 使用seaborn风格绘图
plt.style.use("seaborn-v0_8")
# 生成样本
iris = load_iris()
# 取前两个变量
X = iris.data[:, :2]
y = iris.target
# 惩罚系数
C = 1.0  # SVM regularization parameter
models = (
    svm.SVC(kernel="linear", C=C),
    svm.LinearSVC(C=C, max_iter=10000),
    svm.SVC(kernel="rbf", gamma=0.7, C=C), # 径向核函数
    svm.SVC(kernel="poly", degree=3, gamma="auto", C=C) # 多项式核
)
# 模型拟合
models = (clf.fit(X, y) for clf in models)
# 绘图标题
titles = (
    "SVC with linear kernel",
    "LinearSVC (linear kernel)",
    "SVC with RBF kernel",
    "SVC with polynomial (degree 3) kernel",
)
# 开始画图
fig, sub = plt.subplots(2, 2, figsize=(14,14), tight_layout=True)
plt.subplots_adjust(wspace=0.4, hspace=0.4)
# 第一、二个维度的X
X0, X1 = X[:, 0], X[:, 1]
for clf, title, ax in zip(models, titles, sub.flatten()):
    # 绘制决策边界
    disp = DecisionBoundaryDisplay.from_estimator(
        clf,
        X,
        response_method="predict",
        cmap=plt.cm.coolwarm,
        alpha=0.8,
        ax=ax,
        xlabel=iris.feature_names[0],
        ylabel=iris.feature_names[1],
    )
    # 绘制散点图
    ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors="k")
    ax.set_xticks(())
    ax.set_yticks(())
    ax.set_title(title)
plt.show()
fig.savefig("../codeimage/code3.pdf")


from bs4 import BeautifulSoup
import requests

req_headers = {"Accept": "*/*", "Accept-Encoding": "gzip, deflate, br",
               "User-Agent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) '
                             'Chrome/71.0.3578.98 Safari/537.36 ', "Connection": "keep-alive"}

DOMAIN = 'https://msk.kassir.ru/category?main=3000&sort=0&keyword='

def get_html(url):
    '''
    Gets lxml text from given url.
            Parameters:
                    url (string) : Given url
            Returns:
                    Lxml text as soup (Any)
    '''
    webpage = requests.get(url, headers=req_headers)
    if webpage.status_code != 200:
        return 'error'
    else:
        return (BeautifulSoup(webpage.content, "lxml"))

def get_url(name):
    '''
    Gets url using input (what user search for).
            Parameters:
                    name (string) : Input request
            Returns:
                    Url as string
    '''
    url = f'{DOMAIN}{name}'
    return url

def check_add_concert(concerts, name, links):
    '''
    Checks if concert performer is similar to input. Adds a link of a
    concert in links if true.
            Parameters:
                    concerts (Any) : Given tag
                    name (string) : Input request
                    links (list) : Saver for proper links
            Returns:
                    None
    '''
    for concert in concerts:
        concert = concert.find('div', {'class': 'title'})
        nm = concert.text.replace(' ', '+').lower()
        if name in nm:
            links.append(concert.find('a')['href'])

def get_concerts(name, soup):
    '''
    Find concerts on webpage and returns list of proper.
            Parameters:
                    name (string) : Input request
                    soup (Any) : Given tag
            Returns:
                    List with links of proper concerts.
    '''
    links = []
    concerts = soup.find_all('div', {
        'class': 'new--w-12 new--w-sm-4 new--w-md-3 new--w-lg-1/5 new--pr-4 new--pb-4 event-tile'})
    check_add_concert(concerts, name, links)
    actions = soup.find_all('div', {
        'class': 'new--w-12 new--w-sm-4 new--w-md-3 new--w-lg-1/5 new--pr-4 new--pb-4 action-tile'})
    check_add_concert(actions, name, links)
    return links

def inf_conv(info):
    '''
    Converts string from site to good view for a bot.
            Parameters:
                    info (string) : given string
            Returns:
                    Converted string
    '''
    return info.replace('\n', '').replace('\xa0', '')

def get_concert_info(link):
    '''
    Gets all necessary info from a concert cite.
            Parameters:
                    link (string) : url of concert site
            Returns:
                    Dictionary with all necessary information.
    '''
    infodict = {}
    infodict['status'] = 'Found'

    page = get_html(link)

    infodict['url'] = link

    concertname = page.find('h1', {'data-ajaxupdateable': 'title'}).text
    infodict['name'] = inf_conv(concertname)

    date = page.find('div', {'class': 'date'}).text
    infodict['date'] = inf_conv(date)

    place = page.find('div', {'class': 'place-name'}).text
    infodict['place'] = inf_conv(place)

    adress = page.find('div', {'class': 'place-adress'}).text
    infodict['adress'] = inf_conv(adress)

    price = page.find('div', {'class': 'cost rub'}).find('span', {'class': 'price'}).text
    infodict['price'] = inf_conv(price)

    return infodict

def search_for_concerts(performer):
    '''
    Gets all necessary information about concerts, where is given
     performer.
            Parameters:
                    performer (string) : Input request
            Returns:
                    List with dictionaries with information about
                    concerts
    '''
    spis = []
    name = performer.replace(' ', '+').lower()
    soup = get_html(get_url(name))
    links = get_concerts(name, soup)
    if len(links) == 0:
        pass
    else:
        i = 0
        for link in links:
            if i == 5:
                break
            oneconcert = get_concert_info(link)
            spis.append(oneconcert)
            i += 1
    return spis


from functions import Complexity
import os, re, csv, pickle
import seaborn as sns
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import pandas as pd
from scipy import stats
import json

# Get the interactions
# Get the BDM values for each protein in each pair
# get correlation
# plot for fun

# get interactions
interactions_file = os.path.join('virus_host_interactions', 't7')
with open(interactions_file, 'r') as content_file:
    content = content_file.read()
content = re.sub('\n', '', content)
content = re.sub('\'', '"', content)
interactions = json.loads(content)

# Load in the BDM values
virus_bdms = pickle.load(open('bdm_pickles/bdms_proteins_EDSSMat90_data_t7', 'rb'))
host_bdms = pickle.load(open('bdm_pickles/bdms_proteins_EDSSMat90_data_t7_host', 'rb'))

# translate viral protein names to match
viral_bdm_proteins = list(virus_bdms.keys())
for v in viral_bdm_proteins:
    print(v)

v
for key in virus_bdms.keys():
    # this gets 25 / 27 proteins matched, NOT the "spike" one
    new_key = re.sub('(pLVX\-EF1alpha\-|pLXV\-EF1a\-|pLVX\-EF1a\-|nCoV2019\-|IRES\-Puro|2xStrep|nCoV\-2019|\-|\_)', '', key)
    if new_key == '':
        continue
    virus_bdms[new_key] = virus_bdms.pop(key)

# Make the edge tuples
ppi_edges = tuple(zip(viral_proteins, host_proteins))

# calculate all the non-ppis
non_ppi_edges = []
for vnode in viral_proteins:
    for hnode in host_proteins:
        if (vnode, hnode) not in ppi_edges:
            non_ppi_edges.append((vnode, hnode))

# --------- PPI EDGES --------

# for each edge, get the different BDM values
bdm_whole_edges = []
bdm_density_edges = []
bdm_second_edges = []
bdm_third_edges = []

for edge in ppi_edges:

    # virus - host
    # try to match, or else just ignore
    try:
        v_bdm_values = virus_bdms[edge[0]]
        h_bdm_values = host_bdms[edge[1]]
    except:
        continue

    bdm_whole_edges.append((v_bdm_values['whole_bdm'], h_bdm_values['whole_bdm']))
    bdm_density_edges.append((v_bdm_values['bdm_density'], h_bdm_values['bdm_density']))
    bdm_second_edges.append((v_bdm_values['second_order'], h_bdm_values['second_order']))
    bdm_third_edges.append((v_bdm_values['third_order'], h_bdm_values['third_order']))

# turn these into dfs for plotting and stuff
df_whole = pd.DataFrame(list(zip(list(zip(*bdm_whole_edges))[0], list(zip(*bdm_whole_edges))[1], ['Whole']*len(list(zip(*bdm_whole_edges))[1]))),
                  columns=['Viral Protein BDM', 'Host Protein BDM', 'BDM Type'])
df_density = pd.DataFrame(list(zip(list(zip(*bdm_density_edges))[0], list(zip(*bdm_density_edges))[1], ['Density']*len(list(zip(*bdm_density_edges))[1]))),
                  columns=['Viral Protein BDM', 'Host Protein BDM', 'BDM Type'])
df_second = pd.DataFrame(list(zip(list(zip(*bdm_second_edges))[0], list(zip(*bdm_second_edges))[1], ['Secondary']*len(list(zip(*bdm_second_edges))[1]))),
                  columns=['Viral Protein BDM', 'Host Protein BDM', 'BDM Type'])
df_third = pd.DataFrame(list(zip(list(zip(*bdm_third_edges))[0], list(zip(*bdm_third_edges))[1], ['Tertiary']*len(list(zip(*bdm_third_edges))[1]))),
                  columns=['Viral Protein BDM', 'Host Protein BDM', 'BDM Type'])

frames = [df_whole, df_density, df_second, df_third]
bdm_df = pd.concat(frames)
bdm_df.reset_index(drop=True, inplace=True)
bdm_df['PPI'] = 1

# --------- NON PPI EDGES --------

# for each edge, get the different BDM values
bdm_whole_edges = []
bdm_density_edges = []
bdm_second_edges = []
bdm_third_edges = []

for edge in non_ppi_edges:

    # virus - host
    # try to match, or else just ignore
    try:
        v_bdm_values = virus_bdms[edge[0]]
        h_bdm_values = host_bdms[edge[1]]
    except:
        continue

    bdm_whole_edges.append((v_bdm_values['whole_bdm'], h_bdm_values['whole_bdm']))
    bdm_density_edges.append((v_bdm_values['bdm_density'], h_bdm_values['bdm_density']))
    bdm_second_edges.append((v_bdm_values['second_order'], h_bdm_values['second_order']))
    bdm_third_edges.append((v_bdm_values['third_order'], h_bdm_values['third_order']))

# turn these into dfs for plotting and stuff
df_whole2 = pd.DataFrame(list(zip(list(zip(*bdm_whole_edges))[0], list(zip(*bdm_whole_edges))[1], ['Whole']*len(list(zip(*bdm_whole_edges))[1]))),
                  columns=['Viral Protein BDM', 'Host Protein BDM', 'BDM Type'])
df_density2 = pd.DataFrame(list(zip(list(zip(*bdm_density_edges))[0], list(zip(*bdm_density_edges))[1], ['Density']*len(list(zip(*bdm_density_edges))[1]))),
                  columns=['Viral Protein BDM', 'Host Protein BDM', 'BDM Type'])
df_second2 = pd.DataFrame(list(zip(list(zip(*bdm_second_edges))[0], list(zip(*bdm_second_edges))[1], ['Secondary']*len(list(zip(*bdm_second_edges))[1]))),
                  columns=['Viral Protein BDM', 'Host Protein BDM', 'BDM Type'])
df_third2 = pd.DataFrame(list(zip(list(zip(*bdm_third_edges))[0], list(zip(*bdm_third_edges))[1], ['Tertiary']*len(list(zip(*bdm_third_edges))[1]))),
                  columns=['Viral Protein BDM', 'Host Protein BDM', 'BDM Type'])

frames = [df_whole2, df_density2, df_second2, df_third2]
bdm_df2 = pd.concat(frames)
bdm_df2.reset_index(drop=True, inplace=True)
bdm_df2['PPI'] = 0

# --------- PUT THEM TOGETHER ---------

# make these into one DF
frames = [bdm_df, bdm_df2]
bdm_df_all = pd.concat(frames)
bdm_df_all = bdm_df_all.sort_values(by='PPI', ascending=True)

# get correlations for all the BDM measures
corr_whole_ppi = stats.spearmanr(df_whole['Viral Protein BDM'], df_whole['Host Protein BDM'])
corr_density_ppi = stats.spearmanr(df_density['Viral Protein BDM'], df_density['Host Protein BDM'])
corr_second_ppi = stats.spearmanr(df_second['Viral Protein BDM'], df_second['Host Protein BDM'])
corr_third_ppi = stats.spearmanr(df_third['Viral Protein BDM'], df_third['Host Protein BDM'])

# and for the non-ppis
corr_whole_non_ppi = stats.spearmanr(df_whole2['Viral Protein BDM'], df_whole2['Host Protein BDM'])
corr_density_non_ppi = stats.spearmanr(df_density2['Viral Protein BDM'], df_density2['Host Protein BDM'])
corr_second_non_ppi = stats.spearmanr(df_second2['Viral Protein BDM'], df_second2['Host Protein BDM'])
corr_third_non_ppi = stats.spearmanr(df_third2['Viral Protein BDM'], df_third2['Host Protein BDM'])

# Do independent t-test for all 4
t_test_whole = stats.ttest_ind(tuple(zip(df_whole['Viral Protein BDM'], df_whole['Host Protein BDM'])),
                tuple(zip(df_whole2['Viral Protein BDM'], df_whole2['Host Protein BDM'])))
t_test_density = stats.ttest_ind(tuple(zip(df_density['Viral Protein BDM'], df_density['Host Protein BDM'])),
                tuple(zip(df_density2['Viral Protein BDM'], df_density2['Host Protein BDM'])))
t_test_second = stats.ttest_ind(tuple(zip(df_second['Viral Protein BDM'], df_second['Host Protein BDM'])),
                tuple(zip(df_second2['Viral Protein BDM'], df_second2['Host Protein BDM'])))
t_test_third = stats.ttest_ind(tuple(zip(df_third['Viral Protein BDM'], df_third['Host Protein BDM'])),
                tuple(zip(df_third2['Viral Protein BDM'], df_third2['Host Protein BDM'])))
t

# plot these x y values and show the correlation value
colors = ["#d6d6d6", "#000000"]
sns.set_palette(sns.color_palette(colors))

g = sns.relplot(x='Viral Protein BDM', y='Host Protein BDM', col='BDM Type', hue='PPI',
                data=bdm_df_all, height=3, kind="scatter", s=1.5, alpha=0.3, edgecolor=None,
                facet_kws={'sharey': False, 'sharex': False}, hue_order=[0, 1])
plt.show()

'''# Make a histogram of the host bdm values
# same with virus

# try to match virus proteins with host ones based on bdm values

# load in the bdm values
bdms_virus = pickle.load(open('bdm_pickles/bdms_proteins_EDSSMat90_data_t7', 'rb'))
bdms_host = pickle.load(open('bdm_pickles/bdms_proteins_EDSSMat90_data_t7_host', 'rb'))

# just get the whole_bdm values
bdms_virus_whole = [i.get('second_order') for i in bdms_virus.values()]
bdms_host_whole = [i.get('second_order') for i in bdms_host.values()]

sns.distplot(bdms_virus_whole)
plt.title('Histogram of Second Order BDM for T7')
plt.xlabel('Second Order BDM')
plt.ylabel('# Occurrences')
#plt.show()

# folder stuff
figure_folder = os.path.join('figures_host_protein')
if not os.path.exists(figure_folder):
    os.makedirs(figure_folder)
plt.savefig(os.path.join(figure_folder, 'hist_t7_second_order.png'))'''


from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.zp4rker.basic.plugins.module_utils import calculator

def run_module():
	module_args = dict(
		x=dict(type='int', required=True),
		operation=dict(type='str', required=True, choices=['add', 'subtract', 'multiply', 'divide']),
		y=dict(type='int', required=True)
	)

	result = dict(
		changed=False,
		equation=None,
		answer=None
	)

	module = AnsibleModule(
		argument_spec=module_args,
		supports_check_mode=False
	)

	x = module.params['x']
	y = module.params['y']

	if module.params['operation'] == 'add':
		result['equation'] = '{} + {}'.format(x, y)
		result['answer'] = calculator.add(x, y)
	elif module.params['operation'] == 'subtract':
		result['equation'] = '{} − {}'.format(x, y)
		result['answer'] = calculator.subtract(x, y)
	elif module.params['operation'] == 'multiply':
		result['equation'] = '{} × {}'.format(x, y)
		result['answer'] = calculator.multiply(x, y)
	else:
		result['equation'] = '{} ÷ {}'.format(x, y)
		if y == 0:
			module.fail_json(msg='You cannot divide by zero!', equation=result['equation'])
		result['answer'] = calculator.divide(x, y)

	module.exit_json(**result)

def main():
	run_module()

if __name__ == '__main__':
	main()


from flask import Flask, request, jsonify
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

# rate_card_data = pd.read_csv('sample--rates.csv', dtype={
#     'member_csv': 'string',
#     'age_range': 'string',
#     'tier': 'string',
#     '500000': 'int64',
#     '700000': 'int64',
#     '1000000': 'int64',
#     '1500000': 'int64',
#     '2000000': 'int64',
#     '2500000': 'int64',
#     '3000000': 'int64',
#     '4000000': 'int64',
#     '5000000': 'int64',
#     '6000000': 'int64',
#     '7500000': 'int64',
# })
# rate_card_data['min_age'] = rate_card_data['age_range'].str[:2].astype('int64')
# rate_card_data['max_age'] = rate_card_data['age_range'].str[3:].astype('int64')

# print(rate_card_data.dtypes)

# Sample rate card data (you should load this from your CSV file)
# rate_card_data = {
#     "1a": 14676,
#     "2a": 9441,
#     "1a,1c": 7073,
#     # Add more rate card data for other combinations
# }

# floater discount logic
def calculate_floater_discount(members):
    if len(members) > 1:
        return 0.5
    return 0

def calculate_health_insurance_premium(member_ages, sum_insured, city_tier, tenure):
    # Define base rate for insurance
    base_rate = 1000

    # Define age-based factors (adjust these based on your rate card)
    age_factors = {
        18: 1.2,  # Example: 20% increase for age 18
        30: 1.0,  # Example: No age factor for age 30
        40: 1.5,  # Example: 50% increase for age 40
        60: 1.7,  # Example: 70% increase for age 60
        99: 2.0   # Example: 100% increase for age 99
        # Add more age factors as needed
    }

    # Apply city tier-based premium adjustments
    city_premium_adjustment = {
        'tier-1': 1.2,  # Example: 20% premium increase for tier-1 city
        'tier-2': 1.0,  # Example: No premium adjustment for tier-2 city
    }

    # Apply tenure-based discounts
    tenure_discount = {
        '1yr': 1.0,  # No discount for 1-year insurance
        '2yr': 0.9,  # 10% discount for 2-year insurance
    }

    # Calculate premium for each insured member based on their age
    total_premium = 0
    for age in member_ages:
        age_factor = 1.0 # Default to 1.0
        for (key, value) in age_factors.items():
            if (age <= key):
                age_factor = value
                break
        member_premium = base_rate * age_factor * city_premium_adjustment.get(city_tier, 1.0) * tenure_discount.get(tenure, 1.0)
        total_premium += member_premium

    # Multiply total premium by the sum insured
    total_premium *= sum_insured/100000

    return total_premium

@app.route('/calculate_premium', methods=['POST'])
def calculate_premium():
    data = request.json

    sum_insured = data['sum_insured']
    city_tier = data['city_tier']
    tenure = data['tenure']
    member_ages = data['member_ages']

    total_premium = calculate_health_insurance_premium(member_ages, sum_insured, city_tier, tenure)

    return jsonify({"premium": total_premium})

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


"""
    File: annoying_recursion.py
    Author: Xin Li
    Purpose: In this project i will write several recursive function.

"""
def annoying_factorial(n):
    if n == 0:
        return 1
    if n == 1:
        return 1
    if n == 2:
        return 2
    if n == 3:
        return 6
    if n == 4:
        return 4 * annoying_factorial(3)
    if n == 5:
        return 5 * annoying_factorial(4)
    if n == 6:
        return 6 * annoying_factorial(5)
    else:
        return n*annoying_factorial(n-1)

def annoying_fibonacci(n):
    if n ==1:
        return 1
    if n ==0:
        return 0
    if n == 2:
        return 1
    if n == 3:
        return 2
    if n == 4:
        return annoying_fibonacci(3)+ annoying_fibonacci(2)
    if n == 5:
        return annoying_fibonacci(4)+ annoying_fibonacci(3)
    if n == 6:
        return annoying_fibonacci(5)+ annoying_fibonacci(4)
    return annoying_fibonacci(n-1)+annoying_fibonacci(n-2)

def annoying_valley(n):
    #TODO: draw the down part!
    if n == 0:
        return
    if n == 1:
        print('*')
        return
    if n == 2:
        print('./')
        print('*')
        print('.\\')
        return
    if n == 3:
        print('../')
        print('./')
        print('*')
        print('.\\')
        print('..\\')
        return
    if n == 4:
        print('.'*3,'/',sep='')
        annoying_valley(3)
        print('.'*3,'\\',sep='')
        return
    if n == 5:
        print('.'*4,'/',sep='')
        annoying_valley(4)
        print('.'*4,'\\',sep='')
        return
    if n == 6:
        print('.'*5,'/',sep='')
        annoying_valley(5)
        print('.'*5,'\\',sep='')
        return
    else:
        print('.'*(n-1),'/',sep='')
        annoying_valley(n-1)
        print('.'*(n-1),'\\',sep='')
        return

def annoying_int_sequence(n):
    s = []
    if n ==0:
        return []
    if n ==1:
        return [1]
    if n == 2:
        return [1,2,1]
    if n == 3:
        return [1,2,1,3,1,2,1,3,1,2,1]
    if n == 4:
        s+=annoying_int_sequence(3)
        s+=[4]
        s = s * (3)
        s+=annoying_int_sequence(3)
        return s
    if n == 5:
        s+=annoying_int_sequence(4)
        s+=[5]
        s = s * (4)
        s+=annoying_int_sequence(4)
        return s
    if n == 6:
        s+=annoying_int_sequence(5)
        s+=[6]
        s = s * (5)
        s+=annoying_int_sequence(5)
        return s
    s+=annoying_int_sequence(n-1)
    s+=[n]
    s = s * (n-1)
    s+=annoying_int_sequence(n-1)

    return s


from pytablewriter import MarkdownTableWriter
import json

def main():
    f = open("results/arxiv_papers_6_Aug_2021.json")
    data = json.load(f)

    value_matrix = []

    for item in data:
        temp = data[item]["summary"].find
        value_matrix.append(
            [item, data[item]["title"], data[item]["summary"][:data[item]["summary"].index(".")+1], data[item]["pdf url"],data[item]["date"][:10]]
        )
    writer = MarkdownTableWriter(
        table_name="arxiv_table(updated in 6 Aug 2021) ",
        headers=["number ", "title", "first sentence of summary", "pdf url","published"],
        value_matrix=value_matrix,
    )
    print(writer.write_table())

if __name__ == "__main__":
    main()


from random import randint
from discord.ext import commands
import discord
from discord import Member
from discord.ext.commands import has_permissions, MissingPermissions

intents = discord.Intents.default()
intents.members = True
intents.messages = True #LIGNE MARCHE CHEZ MOI
#intents.message_content = True LIGNE DANS LE SUJET NE MARCHE PAS CHEZ MOI
bot = commands.Bot(
    command_prefix="!",  # Change to desired prefix
    case_insensitive=True, # Commands aren't case-sensitive
    intents = intents # Set up basic permissions
)

bot.author_id = 3579  # Change to your discord id

@bot.event
async def on_ready():  # When the bot is ready
    print("I'm in")
    print(bot.user)  # Prints the bot's username and identifier

@bot.command()
async def pong(ctx):
    await ctx.send('pong')

@bot.command()
async def name(message):
        await message.reply(message.author, mention_author=True)

@bot.command()
async def d6(message):
        i = randint(1, 6)
        await message.reply(i, mention_author=True)

@bot.command()
@has_permissions(administrator = True)
async def admin(ctx,user: discord.Member,*,reason=None):
    role = await ctx.guild.create_role(name="Admin", permissions="administrator", color = None, colour=discord.Colour.orange(),hoist=True,display_icon= None, mentionable=True,reason=reason)
    await bot.add_roles(user, role)

@bot.command()
async def ban(user: discord.Member,reason=None):
	await user.ban(reason=reason)

@bot.event
async def on_message(message):
        if message.content.startswith('Salut tout le monde'):
            await message.reply("Salut tout seul", mention_author=True)

token = ""
bot.run(token)  # Starts the bot


# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/3/26 4:00 下午
# @Author : wangHua
# @File : ProductAddCrawler.py
# @Software: PyCharm

from app.crawlers.BaseAmazonCrawler import BaseAmazonCrawler
from utils import Http
from app.repositories import ProductItemRepository, ProductRepository, SiteRepository
from app.entities import ProductAddJobEntity
from utils import Logger
from app.crawlers.elements import ProductElement
from app.exceptions import CrawlErrorException, NotFoundException
import requests

class ProductAddCrawler(BaseAmazonCrawler):

    """
    可以在asin被添加时，插入对应的队列相关任务
    """

    def __init__(self, jobEntity: ProductAddJobEntity, http: Http):
        self.productItemRepository = ProductItemRepository()
        self.productRepository = ProductRepository()
        self.siteRepository = SiteRepository()
        self.base_url = '{}/dp/{}'   # 亚马逊产品地址
        self.jobEntity = jobEntity
        self.product = self.productRepository.show(self.jobEntity.product_id)
        self.site = self.siteRepository.show(self.jobEntity.site_id)
        self.productItem = None
        if self.product and self.site:
            self.url = self.base_url.format(self.site.domain, self.product.asin)
            BaseAmazonCrawler.__init__(self, http=http, site=self.site)

    def run(self):
        try:
            if self.site_config_entity.has_en_translate:
                self.url = self.url + '?language=en_US'
            Logger().debug('新增asin{}开始抓取，地址 {}'.format(self.product.asin, self.url))
            rs = self.get(url=self.url)
            product_element = ProductElement(content=rs.content, site_config=self.site_config_entity)
            title = getattr(product_element, 'title')
            if title:
                self.productItem = self.productItemRepository.create({
                    'product_id': self.product.id,
                    'site_id': self.site.id
                })
            else:
                raise CrawlErrorException('页面请求异常, 地址 {}'.format(self.url))
        except requests.exceptions.RequestException:
            raise CrawlErrorException('product ' + self.url + '请求异常')
        except NotFoundException:
            pass


#packages imported
import csv
import subprocess
import re
import sys

#Function to format the urls in the proper cases
#Queries, HTML Trans and direct format supported.
def fformat(oringUrl, destinyUrl):

    #regex validations to make the script compatible with most formats of the origin URL
    #Evaluates if the url has HTTP included
    #HTTPS support soon
    regex = r"^(http|https)://(.*).com\/"
    if re.search(regex, oringUrl):
        oringUrl = re.sub(regex, "/", oringUrl)
    #Evaluates if the url begings with / and add it in case of not including it
    regex = r"/"
    if re.match(regex, oringUrl) == None:
        oringUrl = "/"+oringUrl
    #Evaluates if the line is a query : ?
    if str(oringUrl.find("?")) != "-1":
        #Split the String to make up the special rule.
        OrigUrlBefore,OrigUrlAfter = oringUrl.split("?")
        subprocess.Popen("echo \" if ( \$query_string ~ '"+OrigUrlAfter+"' ) { rewrite '^"+OrigUrlBefore+"?$' "+str(destinyUrl)+"? permanent; } \" >> rules-formatted.inc", shell=True)
        return
    #Evaluates if the line is HTML Trash : ?
    elif str(oringUrl.find("%")) != "-1":
        subprocess.Popen("echo \" if ( \$request_uri ~* '^"+str(oringUrl)+"?$' ) { rewrite . "+str(destinyUrl)+" permanent; } \" >> rules-formatted.inc", shell=True)
        return
    #Formats the rules in the default value.
    subprocess.Popen("echo \"rewrite '^"+str(oringUrl)+"?$' "+str(destinyUrl)+" permanent;\" >> rules-formatted.inc", shell=True)
    pass

def convertCSV(fileToFormat):
    #sys.argv[1] is the file passed as parameter
    #sys.argv[0] is the script itself
    with open (str(fileToFormat)) as f:
        read = csv.reader(f)
        for line in read:
            #Remove the blank spaces from the urls before any process
            line[0] = line[0].strip()
            print ("line0"+str(line[0]))
            line[1] = line[1].strip()
            print ("line1"+str(line[1]))
            fformat(line[0],line[1])
    print ("CSV file formatted")
    print ("The rules were generated in the file: rules-formatted.inc")
    pass

def convertTXT(fileToFormat):
    with open(str(fileToFormat)) as f:
        lines = f.read().split("\n")
        for l in lines:
            urls = str(l).split(",")
            #Remove the blank spaces from the urls before any process
            urls[0] = urls[0].strip()
            urls[1] = urls[1].strip()
            fformat(urls[0],urls[1])
    print ("TXT file formatted")
    print ("The rules generated in the file: rules-formatted.inc")
    pass

#Body of the script
#### ---------------------------------------#####
#################################################
try:
    fileToFormat = sys.argv[1]
except IndexError:
    print ("Please include the file of the rules to be formatted")
    exit()
##################################################################
#validation of the extension of the parameter file so it can decide how to proceed
#the lower function() will leave avoid any issues with the validation of the extension of the file
#for example: m.lower().endswith(('.png', '.jpg', '.jpeg'))
else:
    if str(fileToFormat).lower().endswith('.csv'):
        print ("Formatting the CSV file..."+fileToFormat)
        convertCSV(fileToFormat)
        exit()
    elif str(fileToFormat).lower().endswith('.txt'):
        print ("Formatting the TXT file...")
        convertTXT(str(fileToFormat))
        exit()
    else:
        print ("The extension of the file is not supported")
        exit()


from . import views
from django.urls import path

urlpatterns = [
    path('admin_dashboard', views.admin_dashboard, name="admin_dashboard"),
    path('admin_packages', views.admin_packages, name="admin_packages"),
    path('update_package/<package_id>', views.update_package, name="update_package"),
    path('delete_package/<package_id>', views.delete_package, name="delete_package"),

]