import sys
import os
from PIL import Image
from openpyxl import Workbook, load_workbook
from lib_py import parameter as p

def read_img_embed(cover_image_path, channel, log):

	im=Image.open(cover_image_path)
	if(p.log_detail):
		log.write("Cover image: " + cover_image_path + '\n')

	# Gray_level img
	if im.mode in ['L']:

		if channel != 'L':
			print('Channel chose to embed is:', channel)
			print('But img is in L mode. Please check channel code in parameter.')
			sys.exit(0)

		image_split = {'L':im}
		if(p.log_detail):
			log.write("Image mode: " + str(im.mode) + '\n')
			log.write("Channel chose to embed: "+ str(channel) + " channel" + '\n')

	# Color img
	elif im.mode in ['RGB', 'RGBA', 'RGBX']:

		if(p.log_detail):
			log.write("Image mode: " + str(im.mode) + '\n')
			log.write("Channel chose to embed: "+ str(channel) + " channel" + '\n')

		r, g, b = im.split()
		image_split = {'R':r, 'G':g, 'B':b}

		im = im.getchannel(channel)

	else:
		print(cover_image_path)
		print("Image mode are not yet supported.")
		print("Image mode:", im.mode)
		sys.exit(0)

	return (im, image_split)

def save_img_embed(stego_image_dir, cover_image, im, width, height, stego, channel, image_split):

	# Save output message
	idx=0
	for j in range(height):
		for i in range(width):
			im.putpixel((i, j), stego[idx])
			idx += 1

	s = "_stego_" + channel + "."
	stego_image = (cover_image.replace(".", s))

	stego_image_dir_channel = stego_image_dir + channel + '/'

	if not os.path.isdir(stego_image_dir_channel):
		os.mkdir(stego_image_dir_channel)

	stego_image_path = os.path.join(stego_image_dir_channel, stego_image)

	if(channel == 'L'):
		im.save(output_img_path)
		return output_img_path

	elif(channel == 'R'):
		out = Image.merge("RGB", (im, image_split["G"], image_split["B"]))

	elif(channel == 'G'):
		out = Image.merge("RGB", (image_split["R"], im, image_split["B"]))

	elif(channel == 'B'):
		out = Image.merge("RGB", (image_split["R"], image_split["G"], im))

	else:
		print(stego_image_path)
		print("Error channel to save.")
		print("channel chose:", channel)
		sys.exit(0)
		return

	out.save(stego_image_path)
	return stego_image_path

def read_img_extract(stego_image_path, log):

	im=Image.open(stego_image_path)

	if(p.log_detail):
		log.write("Stego image: " + stego_image_path + '\n')

	# Gray_level img
	if im.mode in ['L']:

		image_split = {'L':im}
		channel_choose = 'L'

		if(p.log_detail):
			log.write("Image mode: " + str(im.mode) + '\n')
			log.write("Channel chose to extract: "+ str(channel_choose) + " channel" + '\n')

	# Color img
	elif im.mode in ['RGB', 'RGBA', 'RGBX']:

		channel_choose = stego_image_path[-5]

		if(p.log_detail):
			log.write("Image mode: " + str(im.mode) + '\n')
			log.write("Channel chose to extract: "+ str(channel_choose) + " channel" + '\n')

		im = im.getchannel(channel_choose)

	else:
		print(stego_image_path)
		print("Image mode are not yet supported.")
		print("Image mode:", im.mode)
		sys.exit(0)

	return (im, channel_choose)


import time

from bs4 import BeautifulSoup

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

SITE_URLS = [
    'https://jobs.ashbyhq.com/docker',
    'https://jobs.ashbyhq.com/quora',
    'https://jobs.ashbyhq.com/linear',
    'https://jobs.ashbyhq.com/multiverse',
    'https://jobs.ashbyhq.com/notion',
    'https://jobs.ashbyhq.com/duolingo',
    'https://jobs.ashbyhq.com/retool',
    'https://jobs.ashbyhq.com/reddit',
    'https://jobs.ashbyhq.com/replit',
    'https://jobs.ashbyhq.com/vanmoof',
]

for URL in SITE_URLS:
    # Configure Chrome options
    chrome_options = Options()
    chrome_options.add_argument("--headless")  # Run Chrome in headless mode

    # Set path to your ChromeDriver executable
    webdriver_service = Service(r"C:\Users\yodal\Downloads\chromedriver_win32")

    # Create a new instance of the Chrome driver
    driver = webdriver.Chrome(
        service=webdriver_service, options=chrome_options)

    # Navigate to the website
    driver.get(URL)

    print('Fetching from: ', URL, '\n\n')

    # Wait for the page to load (adjust the sleep time as needed)
    time.sleep(2)

    # Get the updated HTML content after the page has loaded
    html = driver.page_source

    # Close the browser
    driver.quit()

    # Process the HTML using BeautifulSoup or any other library
    soup = BeautifulSoup(html, 'html.parser')

    departments = soup.find_all(
        'span', class_='ashby-department-heading-level')
    job_listings_by_department = soup.find_all(
        'div', class_='ashby-job-posting-brief-list')

    for department, job_listings_container in zip(departments, job_listings_by_department):
        print('Department: ', department.text, ':')
        job_listings = job_listings_container.find_all(
            'a', class_='_container_j2da7_1')
        for job_listing in job_listings:
            href = job_listing['href']
            print('Href:', href)

            h3_element = job_listing.find('h3')
            if h3_element:
                print('Job title: ', h3_element.text)
        print('\n\n')


import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import sklearn.metrics as skmet
from networkx.drawing.nx_agraph import graphviz_layout, pygraphviz_layout

def plot_rocauc(y_test, y_pred, name):
    fpr, tpr, threshold = skmet.roc_curve(y_test, y_pred)
    roc_auc = skmet.auc(fpr, tpr)
    plt.title('Receiver Operating Characteristic')
    plt.plot(fpr, tpr, 'r', label='AUC = %0.2f' % roc_auc)
    plt.legend(loc='lower right')
    plt.plot([0, 1], [0, 1], 'r--')
    plt.xlim([0, 1])
    plt.ylim([0, 1])
    plt.ylabel('True Positive Rate')
    plt.xlabel('False Positive Rate')
    plt.savefig('plots/' + name)
    plt.clf()
    plt.cla()

def autolabel(rects, ax):
    """Attach a text label above each bar in *rects*, displaying its height."""
    for rect in rects:
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')

def plot_model_degree_histograms(degrees, index_pred_mem, index_real_mem, model_name):
    degree_pred_mem = {}
    degree_pred_nonmem = {}
    print(len(index_pred_mem[model_name]), len(degrees[model_name]))
    for node, preds in index_pred_mem[model_name].items():
        degree = degrees[model_name][node]
        membership = index_real_mem[model_name][node]
        for m, l in zip(membership, preds):
            if m == 1.:
                if degree in degree_pred_mem:
                    degree_pred_mem[degree].append(l)
                else:
                    degree_pred_mem[degree] = [l]
            else:
                if degree in degree_pred_nonmem:
                    degree_pred_nonmem[degree].append(l)
                else:
                    degree_pred_nonmem[degree] = [l]

    plt_degrees = []
    plt_mempred = []
    plt_nmempred = []

    for degree, m_preds in degree_pred_mem.items():
        if degree in degree_pred_nonmem:
            nm_preds = degree_pred_nonmem[degree]
            bins = np.linspace(0, 1, 20)
            plt.hist(m_preds, bins, alpha=0.5, label='Members')
            plt.hist(nm_preds, bins, alpha=0.5, label='Non-Members')
            plt.legend(loc='upper right')
            plt.savefig('analysis_plots/' + model_name +
                        '/degree' + str(degree), bbox_inches="tight")
            plt.clf()
            plt.cla()

def create_label_ratio_plot(plt_labels, plt_scores, plt_ratio, name):
    fig, ax = plt.subplots()
    width = 0.35
    x = np.arange(len(plt_labels))
    rects1 = ax.bar(x - width/2, plt_scores, width, label='AUC')
    rects2 = ax.bar(x + width/2, plt_ratio, width, label='Label Ratio')
    ax.set_ylabel('Scores vs Label ratio in dataset')
    ax.set_xticks(x)
    ax.set_xticklabels(plt_labels)
    ax.legend()
    autolabel(rects1, ax)
    autolabel(rects2, ax)

    plt.setp(ax.get_xticklabels(), rotation=20, horizontalalignment='right')

    # fig.tight_layout()
    plt.savefig('plots/' + name, bbox_inches="tight")
    plt.clf()
    plt.cla()

def create_network_graph(max_node_labels, max_edges, model_name, num=0):
    sg = nx.Graph()

    correct_nodelist = []
    incorrect_nodelist = []
    plt_labels = {}
    for node_id, label in max_node_labels.items():
        plt_labels[node_id] = node_id
        if label == 1:
            correct_nodelist.append(node_id)
        else:
            incorrect_nodelist.append(node_id)

    sg.add_nodes_from(correct_nodelist)
    sg.add_nodes_from(incorrect_nodelist)

    pos = nx.spring_layout(sg)

    nx.draw_networkx_nodes(sg, pos, nodelist=correct_nodelist, node_color="b")
    nx.draw_networkx_nodes(
        sg, pos, nodelist=incorrect_nodelist, node_color="r")
    nx.draw_networkx_edges(sg, pos, edgelist=max_edges, width=1.0, alpha=0.5)
    nx.draw_networkx_labels(sg, pos, plt_labels, font_size=10)

    plt.savefig('analysis_plots/' + model_name +
                '/max_subgraph' + str(num), bbox_inches="tight")
    plt.clf()
    plt.cla()


import tkinter as tk
from tkinter import messagebox
import subprocess
import os

def install_packages():
    try:
        import cookiecutter
    except ImportError:
        subprocess.check_call(["python", "-m", "pip", "install", "cookiecutter"])
        import cookiecutter

    # Make sure we have the right version of cookiecutter
    if cookiecutter.__version__ < "1.4.0":
        subprocess.check_call(["python", "-m", "pip", "install", "--upgrade", "cookiecutter"])

# Make sure necessary packages are installed before running the GUI
install_packages()

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.master.title('KiCAD Project Template')
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        # Instructions
        self.instructions = tk.Text(self, height=10, width=40, wrap=tk.WORD)
        self.instructions.insert(tk.END, "Please enter the following information.\nMake sure you have created the Git Repository.")
        self.instructions.configure(state=tk.DISABLED)
        self.instructions.grid(row=0, column=0, columnspan=2, padx=10, pady=10)

        # Project Name
        self.lbl_project_name = tk.Label(self, text="Project Name")
        self.lbl_project_name.grid(row=1, column=0, padx=10, pady=5, sticky=tk.E)
        self.txt_project_name = tk.Entry(self)
        self.txt_project_name.grid(row=1, column=1, padx=10, pady=5, sticky=tk.W)

        # Repository URL
        self.lbl_repo_url = tk.Label(self, text="Repository URL")
        self.lbl_repo_url.grid(row=2, column=0, padx=10, pady=5, sticky=tk.E)
        self.txt_repo_url = tk.Entry(self)
        self.txt_repo_url.grid(row=2, column=1, padx=10, pady=5, sticky=tk.W)

        # Description
        self.lbl_description = tk.Label(self, text="Project Description")
        self.lbl_description.grid(row=3, column=0, padx=10, pady=5, sticky=tk.E)
        self.txt_description = tk.Entry(self)
        self.txt_description.grid(row=3, column=1, padx=10, pady=5, sticky=tk.W)

        # Author Name
        self.lbl_author_name = tk.Label(self, text="Author Name")
        self.lbl_author_name.grid(row=4, column=0, padx=10, pady=5, sticky=tk.E)
        self.txt_author_name = tk.Entry(self)
        self.txt_author_name.grid(row=4, column=1, padx=10, pady=5, sticky=tk.W)

        # Department Name
        self.lbl_department_name = tk.Label(self, text="Department Name")
        self.lbl_department_name.grid(row=5, column=0, padx=10, pady=5, sticky=tk.E)
        self.txt_department_name = tk.Entry(self)
        self.txt_department_name.grid(row=5, column=1, padx=10, pady=5, sticky=tk.W)

        # Run button
        self.btn_run = tk.Button(self, text="RUN", command=self.run_cookiecutter)
        self.btn_run.grid(row=6, column=0, columnspan=2, padx=10, pady=10)

    def run_cookiecutter(self):
        command = ["cookiecutter", "https://github.com/ShirkyBooi/KiCAD-Project-Template"]
        responses = ['yes', self.txt_project_name.get(), self.txt_repo_url.get(), self.txt_description.get(), self.txt_author_name.get(), self.txt_department_name.get()]
        files_to_remove = ['Makefile', 'mkdocs.yml', 'poetry.lock', 'pyproject.toml']

        try:
            cookiecutter_process = subprocess.Popen(command, stdin=subprocess.PIPE, text=True)
            for response in responses:
                cookiecutter_process.stdin.write(response + "\n")
                cookiecutter_process.stdin.flush()
            cookiecutter_process.communicate()

            for file in files_to_remove:
                os.remove(os.path.join(self.txt_project_name.get(), file))

            subprocess.run(["git", "init"], cwd=self.txt_project_name.get(), check=True)
            subprocess.run(["git", "add", "."], cwd=self.txt_project_name.get(), check=True)
            subprocess.run(["git", "commit", "-m", '"chore: initial Commit"'], cwd=self.txt_project_name.get(), check=True)
            subprocess.run(["git", "remote", "add", "origin", self.txt_repo_url.get()], cwd=self.txt_project_name.get(), check=True)
            subprocess.run(["git", "push", "-u", "origin", "main","--force"], cwd=self.txt_project_name.get(), check=True)
            subprocess.run(["git", "submodule", "add", "https://github.com/ShirkyBooi/KiCADLibraries", "hardware/KiCAD Libraries"], cwd=self.txt_project_name.get(), check=True)
            subprocess.run(["git", "commit", "-m", '"chore: added Library Submodule"'], cwd=self.txt_project_name.get(), check=True)
            subprocess.run(["git", "push"], cwd=self.txt_project_name.get(), check=True)
        except subprocess.CalledProcessError as e:
            messagebox.showerror("Error", str(e))
            root.quit()
        except Exception as e:
            messagebox.showerror("Error", str(e))
            root.quit()
        messagebox.showinfo("Success", "Project was created successfully.")
        root.quit()

root = tk.Tk()
root.geometry("350x400")
app = Application(master=root)
app.mainloop()


# coding: UTF-8
import pandas as pd
import csv,re,json,itertools
import datetime
import locale
import ConfigParser

THRESHOLD = 0.5
RED = '\033[93m'
GREEN = '\033[92m'
ENDC = '\033[0m'

inifile = ConfigParser.SafeConfigParser()
inifile.read("../config.ini")
DB_DIR = inifile.get("env","db_dir")

def collate_pred(csv_data):
    d = datetime.datetime.today()
    report = open('./../Result/Report/model1_report.txt',"a+")
    # report = open('./../../Result/Report/model1_report.txt',"a+")
    report.write('\nTHRESHOLD:\t' + str(THRESHOLD))
    report.write('\nMODEL:\tSGD')
    report.write(d.strftime("%Y-%m-%d %H:%M:%S"))

    total_year = 0.0
    balance = 0

    # f = open('./../../Result/sgd_default_prob.csv', 'r')
    # csv_data = csv.reader(f)
    for row in csv_data:
        index = 0
        benefit = 0

        s = pd.Series(row)
        num, odds = get_valid_comb(str(int(s[0])))    # s[0] -> race_id
        print(num, odds)

        res = s[1:].astype(float)
        res = res[res > THRESHOLD]
        list = res.index
        # print 'list.length: ' + str( len(list) )

        if len(list) > 0:
            l = list.tolist()
            for element in itertools.combinations(l, 2):
                index += 1
                comb = str(element[0]) + '-' + str(element[1])
                if comb in num:
                    # print comb + '\t' + str( odds[num.index(comb)] )
                    benefit += odds[num.index(comb)]

            balance  -= index * 100
        print ('race_id: ' + str(s[0]) + '\tCOMBINATION: ' + str(index) + '\tRETURN: ' + str( benefit ) )

        total = benefit - index * 100
        if total >0:
            report.write('\nrace_id: ' + str(s[0]) + '\tCOMBINATION: ' + str(index) + '\tRETURN: ' + str( benefit ) + '\tWIN')
        else :
            report.write('\nrace_id: ' + str(s[0]) + '\tCOMBINATION: ' + str(index) + '\tRETURN: ' + str( benefit ))

        balance += benefit
        total_year += 1.0

    report.write('\n======================================================================================')
    print ('BALANCE:\t' + str(balance) )
    report.write('\nBALANCE:\t' + str(balance) )
    print ('AVERAGE:\t' + str( balance/total_year ) )
    report.write('\nAVERAGE:\t' + str( balance/total_year ) )
    report.write('\n\n')
    report.close()

def get_valid_comb(race_id):

# with open(DB_DIR + 'Result/odds_dict.json', 'r') as f:
#     data = json.loads(f.read(),'utf-8')
# for key in data.keys():
#     for k in data[key].keys():
#         print k
    jf = open(DB_DIR + 'Result/odds_dict.json')
    # jf = open('./../../Data/odds_dict.json')
    data = json.load(jf, 'utf8')
    dict = data[race_id]
    # print
    # d = dict[u'単勝']
    num = []
    odds = []
    if dict.has_key(u'ワイド'):
        d = dict[u'ワイド']
        num = d[u'num']
        num = num[0].split()
        # print num
        odds = d[u'odds']
        odds = odds.split()
        odds = map(lambda x: x.replace(',',''), odds)
        odds = map(int,odds)
        # print odds
    return num, odds

if __name__ == '__main__':
    # f = open('./../../Result/sgd_default_prob.csv', 'r')
    # csv_data = csv.reader(f)
    # collate_pred(csv_data)
    get_valid_comb('201605020611')


from flask import Flask, request, Response
from flask_jwt_extended import get_jwt_identity
from flask_jwt_extended import jwt_required
from app.utils import jsonify_with_data
from app.utils import jsonify_with_error
from app.api import ct
from app import limiter
from app.service import ApiRes
from app.service import Gpt
from app.utils import logger
from app.service import Opmessage
from app.service import Opchat

@ct.route('/chat', methods=['POST'])
@jwt_required()
@limiter.limit("100/minute")
def chat():
    user_id = get_jwt_identity()
    content = request.json.get('content')
    chatId = request.json.get('chatId')
    logger.info('[gpt] user_id=%s, content=%s, chatId=%s', user_id, content, chatId)
    if len(content) == 0:
        return jsonify_with_error(ApiRes.CONTENT_EMPTY)
    try:
        rsp = Gpt.ask_chat_stream_gpt(user_id, content, chatId)
    except Exception as e:
        return jsonify_with_error(ApiRes.SERVICE_ERROR)
    return Response(rsp, mimetype='text/event-stream')

@ct.route('/get', methods=['POST'])
@jwt_required()
@limiter.limit("100/minute")
def get():
    user_id = get_jwt_identity()
    count = request.json.get('count')
    chat_id = request.json.get('chat_id')
    if count is None:
        count = 3
    logger.info('[gpt] get messages user_id=%s, chat_id=%s', user_id, chat_id)
    if chat_id is None:
        messages = Opmessage.query_history_message(user_id, count)
    else:
        messages = Opmessage.query_history_message_by_chatId(user_id, chat_id, count)
    return jsonify_with_data(ApiRes.OK, messages=messages[::-1])

@ct.route('/get/chats', methods=['POST'])
@jwt_required()
@limiter.limit("10/minute")
def getChats():
    user_id = get_jwt_identity()
    logger.info('[gpt] get chats user_id=%s', user_id)
    chats = Opchat.query_chat(user_id)
    return jsonify_with_data(ApiRes.OK, chats=chats)

@ct.route('/add/chat', methods=['POST'])
@jwt_required()
@limiter.limit("5/minute")
def addChats():
    user_id = get_jwt_identity()
    chat_name = request.json.get('chat_name')
    if chat_name is None:
        chat_name = 'NewChat'
    logger.info('[gpt] add chats user_id=%s', user_id)
    chat_id = Opchat.save_chat(user_id, chat_name)
    return jsonify_with_data(ApiRes.OK, chat_id=chat_id)

@ct.route('/update/chat', methods=['POST'])
@jwt_required()
@limiter.limit("5/minute")
def updateChat():
    user_id = get_jwt_identity()
    chat_id = request.json.get('chat_id')
    chat_name = request.json.get('chat_name')
    Opchat.update_name(chat_id, chat_name)
    return jsonify_with_data(ApiRes.OK)

@ct.route('/delete/chat', methods=['POST'])
@jwt_required()
@limiter.limit("5/minute")
def deleteChat():
    user_id = get_jwt_identity()
    chat_id = request.json.get('chat_id')
    Opchat.delete(chat_id)
    return jsonify_with_data(ApiRes.OK)


# https://www.acmicpc.net/problem/10026

import sys
sys.setrecursionlimit(10 ** 8)
n = int(input())
colors = [[*input()] for _ in range(n)]
visited = [[False] * n for _ in range(n)]

def bfs(y,x):
    color = colors[y][x]
    if color in ["R", "G"]:
        colors[y][x] = "W"
    for dy,dx in [(-1,0), (1,0), (0,1), (0,-1)]:
        Y, X = y+dy , x+dx
        if not 0<=Y<n or not 0<=X<n or visited[Y][X]:
            continue
        if color == colors[Y][X]:
            visited[Y][X] = True
            bfs(Y,X)

normal = 0
for y in range(n):
    for x in range(n):
        if not visited[y][x]:
            bfs(y,x)
            normal += 1

weak = 0
visited = [[False] * n for _ in range(n)]
for y in range(n):
    for x in range(n):
        if not visited[y][x]:
            bfs(y,x)
            weak += 1

print(normal, weak)


class Solution(object):
    def maxAreaOfIsland(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        rows = len(grid)
        cols = len(grid[0])
        maxArea = 0
        def dfs(r, c):
            if (r not in range(rows) or
                c not in range(cols) or
                grid[r][c] != 1):
                return 0
            grid[r][c] = 2
            area = 1
            area += dfs(r + 1, c)
            area += dfs(r - 1, c)
            area += dfs(r, c + 1)
            area += dfs(r, c - 1)
            return area
        for r in range(rows):
            for c in range(cols):
                if grid[r][c] == 1:
                    area = dfs(r, c)
                    maxArea = max(maxArea, area)
        print(grid)
        return maxArea


import string
import textwrap

def find_in_key_mat(c: str) -> (int, int):
    for row in range(5):
        for col in range(5):
            if c == key_mat[row][col]:
                return row, col
            if c == 'j' and key_mat[row][col] == 'i':
                return row, col

def insert_key_mat(c: str, row: int) -> int:
    if c == 'j':
        return row
    key_mat[row].append(c)
    if len(key_mat[row]) == 5:
        row += 1
    return row

def init_key_mat(k: str) -> None:
    char_in_key = set()
    n_k = k + alphabet
    row = 0

    for c in n_k:
        if c not in char_in_key:
            if c == 'j':
                if 'i' not in char_in_key:
                    char_in_key.add('i')
                    row = insert_key_mat('i', row)
            else:
                char_in_key.add(c)
                row = insert_key_mat(c, row)

def encrypt(plain_text: str) -> str:
    cypher_text = ''
    i = 0
    char_pair = []
    for _ in range(len(plain_text)):
        if plain_text[i] != plain_text[i + 1]:
            char_pair.append(plain_text[i] + plain_text[i + 1])
        else:
            char_extra = 'x' if plain_text[i] != 'x' else 'p'
            plain_text = plain_text[:i + 1] + char_extra + plain_text[i + 1:]
            char_pair.append(plain_text[i] + plain_text[i + 1])
        i += 2
        if i == len(plain_text) - 1:
            char_extra = 'x' if plain_text[i] != 'x' else 'p'
            char_pair.append(plain_text[i] + char_extra)
            break
        elif i > len(plain_text) - 1:
            break

    for pair in char_pair:
        p0 = find_in_key_mat(pair[0])
        p1 = find_in_key_mat(pair[1])

        if p0[0] == p1[0]:
            row = p0[0]
            new_col_0 = (p0[1] + 1) % 5
            new_col_1 = (p1[1] + 1) % 5
            cypher_text += key_mat[row][new_col_0] + key_mat[row][new_col_1]
        elif p0[1] == p1[1]:
            col = p0[1]
            new_row_0 = (p0[0] + 1) % 5
            new_row_1 = (p1[0] + 1) % 5
            cypher_text += key_mat[new_row_0][col] + key_mat[new_row_1][col]
        else:
            cypher_text += key_mat[p0[0]][p1[1]] + key_mat[p1[0]][p0[1]]

    return cypher_text

def decrypt(cypher_text: str) -> str:
    char_pair = textwrap.wrap(cypher_text, 2)
    plain_text = ''

    for pair in char_pair:
        p0 = find_in_key_mat(pair[0])
        p1 = find_in_key_mat(pair[1])

        if p0[0] == p1[0]:
            row = p0[0]
            new_col_0 = (p0[1] - 1) % 5
            new_col_1 = (p1[1] - 1) % 5
            plain_text += key_mat[row][new_col_0] + key_mat[row][new_col_1]
        elif p0[1] == p1[1]:
            col = p0[1]
            new_row_0 = (p0[0] - 1) % 5
            new_row_1 = (p1[0] - 1) % 5
            plain_text += key_mat[new_row_0][col] + key_mat[new_row_1][col]
        else:
            plain_text += key_mat[p0[0]][p1[1]] + key_mat[p1[0]][p0[1]]
    return plain_text

if __name__ == '__main__':
    alphabet = string.ascii_lowercase
    key_mat = [[] for _ in range(5)]
    key = 'monarchy'
    init_key_mat(key)

    result = encrypt('balloon')
    print(result)
    result2 = decrypt(result)
    print(result2)


#dependencies
import pandas as pd
from bs4 import BeautifulSoup as bs
from splinter import Browser
from webdriver_manager.chrome import ChromeDriverManager

def scrape():
    #setup splinter
    executable_path = {'executable_path': ChromeDriverManager().install()}
    browser = Browser('chrome', **executable_path, headless = False)

    #----------------------------------
    #LATEST NEWS
    #----------------------------------
    task = 'LATEST NEWS'
    print(f"Scraping {task}...")

    #setup url
    news_url = 'https://redplanetscience.com/'
    browser.visit(news_url)
    html = browser.html
    soup = bs(html, 'html.parser')

    #retrieve latest news title paragraph
    news_title = soup.find_all('div', class_ = 'content_title')[0].text
    news_p = soup.find_all('div', class_ = 'article_teaser_body')[0].text

    print(f"{task} completed.")

    #----------------------------------
    #FEATURED IMAGE
    #----------------------------------
    task = 'FEATURED IMAGE'
    print(f"Scraping {task}...")

    #setup url
    image_url = 'https://spaceimages-mars.com/'
    browser.visit(image_url)
    html = browser.html
    soup = bs(html, 'html.parser')

    #retrieve featured image
    partial_url = soup.find('a', class_ = 'showimg fancybox-thumbs')['href']
    featured_image_url = image_url + partial_url

    print(f"{task} completed.")

    #----------------------------------
    #FACTS
    #----------------------------------
    task = 'FACTS'
    print(f"Scraping {task}...")

    #url scraping w pandas
    facts_url = 'https://galaxyfacts-mars.com/'
    tables = pd.read_html(facts_url)
    df = tables[1]
    df.to_html('facts_mars.html', index = False)

    #setup url
    facts_url = 'https://galaxyfacts-mars.com/'
    tables = pd.read_html(facts_url)
    df = tables[1]
    # df.to_html('facts_mars.html', index = False)
    html_table = df.to_html(index = False)

    print(f"{task} completed.")

    #----------------------------------
    #HEMISPHERES
    #----------------------------------
    task = 'HEMISPHERES'
    print(f"Scraping {task}...")

    #setup url
    hemi_base_url = 'https://marshemispheres.com/'
    browser.visit(hemi_base_url)
    html = browser.html
    soup = bs(html, 'html.parser')

    #grabbing image containers
    hemi_data = soup.find_all('div', class_ = 'item')

    #empty list to store data
    hemi_ls_of_dicts = []
    #loop through item to pull data
    for i in range(len(hemi_data)):
        #setting up new html
        html = browser.html
        soup = bs(html, 'html.parser')

        #splinter click into each link
        hemi_text = soup.find_all('h3')[i].text
        browser.click_link_by_partial_text(hemi_text)

        #finding specific url
        subhtml = browser.html
        subsoup = bs(subhtml, 'html.parser')
        hemi_partial_url = subsoup.find('div', class_ = 'downloads').find('a')['href']

        #appending info
        temp_dict = {}
        temp_dict['title'] = hemi_text
        temp_dict['img_url'] = hemi_base_url + hemi_partial_url
        hemi_ls_of_dicts.append(temp_dict)

        #back to home page
        browser.back()

    print(f"{task} completed.")

    #----------------------------------
    #FINAL DICTIONARY
    #----------------------------------
    task = 'FINAL DICTIONARY'
    print(f"Doing {task}...")

    final_dict = {
        'news_title': news_title,
        'news_p': news_p,
        'featured_image_url': featured_image_url,
        'html_table': html_table,
        'hemi_ls_of_dicts': hemi_ls_of_dicts
    }

    print(f"{task} completed.")

    browser.quit()

    return final_dict


from Student import Student
from UNI import UNI
#-----------------------------------------
from C_IELTS import C_IELTS
from C_TOEFL import C_TOEFL
#-----------------------------------------

#Информация:
def information():
  from datetime import date
  today = date.today()
  print("  Добре дошли!")
  print("Днес е:", today)
  print("\n")

information()

#----------------------------------------------------------------------
#Видове сертификати:

#IELTS
B2_1 = C_IELTS("B2", "ENGLISH")
C1_1 = C_IELTS("C1", "ENGLISH")
C2_1 = C_IELTS("C2", "ENGLISH")

#TOEFL
B2_2 = C_TOEFL("B2", "ENGLISH")
C1_2 = C_TOEFL("C1", "ENGLISH")
C2_2 = C_TOEFL("C2", "ENGLISH")

#--------------------------------------------
#Данни на всички кандидатствали студенти:

#факултетен номер, име, фамилия, университет, оценка от зимен семестър,оценка от летен семестър, сертификат(ако е наличен):
student1 = Student(15311042, "Спас Иванов", "BFU", 6.0, 5.3)
student2 = Student(18314789, "Михаела Георгиева", "BFU", 4.0, 4.6, C1_1)
student3 = Student(18357896, "Виктория Николова", "AS-ZL-UNI", 6.0, 3.1)
student4 = Student(25674587, "Моника Златкова", "AS-ZL-UNI", 4.6, 5.7, C2_1)
student5 = Student(35648248, "Петър Петров", "BFU", 5.2, 5.7, B2_2)

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

#Разпределение на кандидатствалите студенти по университети:
BFU_students = [student1, student2, student5]
ASZL_students = [student3, student4]
#Всички студенти:
ALL_students  = [student1, student2, student3, student4, student5]

#-----------------------------------------------------------------------
#Информация за университетите включени в програмата:

# 1)Бургаски свободен университет
BFU = UNI(BFU_students, "BFU", "Галя Христозова",105)
# 2)Асен Златаров
ASZL = UNI(ASZL_students, "AS-ZL-UNI", "Магдалена Миткова",102)

#-----------------------------------------------------------------------
# Изписване на надпис:
a = "Р Е З У Л Т А Т И"
b = a.center(24, "-")
print("\t" + b )
print("\n")
#------------------------------------------------------
#Изписване на кандидатствалите студенти и техния резултат:

#Студенти от БСУ - статус(одобрен или неодобрен)
print('\x1b[6;30;42m' +' СТУДЕНТИ НА БУРГАСКИ СВОБОДЕН УНИВЕРСИТЕТ:' + '\x1b[0m'"\n")
BFU.print_uni()

#Студенти от Асен Златаров - статус(одобрен или неодобрен)
print('\x1b[6;30;42m' +' СТУДЕНТИ НА АСЕН ЗЛАТАРОВ:' + '\x1b[0m'"\n")
ASZL.print_uni()

#-----------------------------------------------------------------------------
#СТАТИСТИЧЕСКА ИНФОРМАЦИЯ:

# Изписване на надпис:
c = " СТАТИСТИЧЕСКА ИНФОРМАЦИЯ "
d = c.center(30, "*")
print("\t" + d  )
print("\n")
#--------------------------------

print("--> БУРГАСКИ СВОБОДЕН УНИВЕРСИТЕТ")
# 1)Среден успех на БСУ:
BFU.get_avg()
# 2)Брой кандидатствали студенти от БСУ:
BFU.get_num()
# 3)Процент(кандидатствали) от общия брой на БСУ:
BFU.get_percent()

#--------------------------------
print("\n")
#--------------------------------

print("--> АСЕН ЗЛАТАРОВ")
# 1)Среден успех на АСЕН ЗЛАТАРОВ:
ASZL.get_avg()
# 2)Брой кандидатствали студенти от АСЕН ЗЛАТАРОВ:
ASZL.get_num()
# 3)Процент(кандидатствали) от общия брой на АСЕН ЗЛАТАРОВ:
ASZL.get_percent()


#!/usr/bin/env python

import sys
import getopt
import cv2
import bgsubcnt
import numpy as np

 # A temporary solution to OpenCV 3.1.0 bug
cv2.ocl.setUseOpenCL(False)

def usage():
    print ("""BackgroundSubtractorCNT demo/benchmark/comparison
Usage: demo.py [params]

	-h, --help
		print this message
        --bg
		calculate also the background
	--file
		use file (default is system camera)
	--nogui
		run without GUI to measure times
	--type (default:CNT)
		bg subtraction type from - CNT/MOG2/KNN
""")

def getBGSubtractor(typeStr):
    if typeStr == "KNN":
        return cv2.createBackgroundSubtractorKNN()
    if typeStr == "MOG2":
        return cv2.createBackgroundSubtractorMOG2()
    if typeStr == "CNT":
        return bgsubcnt.createBackgroundSubtractor()
    print ("Unknown createBackgroundSubtractor type")
    sys.exit(1)

def main(argv):
    filePath = None
    hasGui = True
    bgImage = False
    typeStr = "CNT"

    try:
        opts, args = getopt.getopt(argv, "h", ["help", "bg", "file=", "nogui", "type="])
    except getopt.GetoptError:
        usage()
        sys.exit(1)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt == "--file":
            filePath = arg
        elif opt == "--bg":
            bgImage = True
        elif opt == "--nogui":
            hasGui = False
        elif opt == "--type":
            typeStr = arg

    if (filePath):
        cap = cv2.VideoCapture(filePath)
    else:
        cap = cv2.VideoCapture(0)

    if cap.isOpened() == False:
        print ("Could not initialize capturing...");
        sys.exit(2)

    fgbg = getBGSubtractor(typeStr)

    e1 = cv2.getTickCount()

    ret, frame = cap.read()
    while(ret):
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        fg = fgbg.apply(gray)
        if hasGui:
            cv2.imshow('Orig',frame)
            cv2.imshow('FG',fg)
            if bgImage:
                bg = fgbg.getBackgroundImage()
                cv2.imshow('BG',bg)
            k = cv2.waitKey(1)
            if k == 27:
                break
        ret, frame = cap.read()

    e2 = cv2.getTickCount()

    t = (e2 - e1)/cv2.getTickFrequency()
    print ("Execution took '%(val).3f' seconds." % {'val': t});

    cap.release()
    if hasGui:
        cv2.destroyAllWindows()

if __name__ == "__main__":
    main(sys.argv[1:])


#!/usr/bin/env python

from distutils.core import setup

# The original author of this library is
# Ken Robertson ken@invalidlogic.com
# This version is a fork on github maintained by
# Andre Graf andre@dergraf.org.
#
# Ken's library version number depended on
# the version number of python-bert. This is
# not suitable for an automated installation
# using pip which will automatically install
# the dependencies listed in 'install_requires'.
# For this reason I manually set the version
# number.
__version__ = '1.0.0'

setup(
    name = 'ernie',
    version = __version__,
    description = 'BERT-Ernie Library',
    author = 'Ken Robertson',
    author_email = 'ken@invalidlogic.com',
    url = 'http://github.com/krobertson/python-ernie',
    packages = ['ernie'],
    classifiers = [
        'Intended Audience :: Developers',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Topic :: Software Development :: Libraries :: Python Modules',
    ],
    install_requires = ["bert>=2.0.0"],
    dependency_links = [
        'http://github.com/samuel/python-bert/tarball/master#egg=bert-2.0.0'
    ]
)


import os
import time
import torch
import gymnasium as gym
import numpy     as np
import torch.nn  as nn

from datetime            import datetime
from torch.distributions import MultivariateNormal
from torch.distributions import Categorical

device = torch.device( "cuda" if torch.cuda.is_available( ) else "cpu" )

# The code is from:
# [REF] https://github.com/nikhilbarhate99/PPO-PyTorch
# Note that we only use this code for continuous action space.

# Compared to the DDPG and TD3 buffers,
# This buffer DO NOT include the next-state of the trajectory, and
# DO include the V(state) function's value (state_values) and logprobs which is simply log( pi( . | s ) )
class RolloutBuffer:
    def __init__( self ):
        self.actions      = [ ]
        self.states       = [ ]
        self.logprobs     = [ ]
        self.rewards      = [ ]
        self.state_values = [ ]
        self.is_terminals = [ ]

    def clear( self ):
        del self.actions[ : ]
        del self.states[ : ]
        del self.logprobs[ : ]
        del self.rewards[ : ]
        del self.state_values[ : ]
        del self.is_terminals[ : ]

class ActorCritic( nn.Module ):
    def __init__( self, state_dim, action_dim, action_std_init ):
        super( ActorCritic, self).__init__( )

        self.action_dim = action_dim

        # The array of action variance,
        self.action_var = torch.full( (action_dim,), action_std_init * action_std_init).to(device)

        # Actor Network
        self.actor = nn.Sequential(
                        nn.Linear( state_dim, 64 ),
                        nn.Tanh( ),
                        nn.Linear( 64, 64 ),
                        nn.Tanh( ),
                        nn.Linear( 64, action_dim ),
                        nn.Tanh( )  )

        # Critic Network
        self.critic = nn.Sequential(
                        nn.Linear( state_dim, 64 ),
                        nn.Tanh( ),
                        nn.Linear( 64, 64 ),
                        nn.Tanh( ),
                        nn.Linear( 64, 1 ) )

    def set_action_std( self, new_action_std ):
        self.action_var = torch.full( ( self.action_dim, ), new_action_std * new_action_std ).to( device )

    # The actor maps state input to the mean of the Multivariate Gaussian
    def act( self, state ):

        action_mean = self.actor( state )
        cov_mat = torch.diag( self.action_var ).unsqueeze( dim = 0 )
        dist = MultivariateNormal( action_mean, cov_mat )

        action = dist.sample( )
        action_logprob = dist.log_prob( action )
        state_val = self.critic( state )

        return action.detach( ), action_logprob.detach( ), state_val.detach( )

    def evaluate( self, state, action ):

        action_mean = self.actor( state )
        action_var = self.action_var.expand_as( action_mean )

        cov_mat = torch.diag_embed( action_var ).to( device )
        dist = MultivariateNormal( action_mean, cov_mat )

        # For Single Action Environments.
        if self.action_dim == 1:
            action = action.reshape( -1, self.action_dim )

        action_logprobs = dist.log_prob( action )
        dist_entropy    = dist.entropy( )
        state_values    = self.critic( state )

        return action_logprobs, state_values, dist_entropy

class PPO:
    def __init__( self, state_dim, action_dim, lr_actor, lr_critic, gamma, K_epochs, eps_clip, action_std_init = 0.6 ):

        #
        self.action_std = action_std_init

        self.gamma    = gamma
        self.eps_clip = eps_clip
        self.K_epochs = K_epochs

        self.buffer = RolloutBuffer()

        self.policy = ActorCritic( state_dim, action_dim, action_std_init ).to( device )
        self.optimizer = torch.optim.Adam( [  {'params':  self.policy.actor.parameters( ), 'lr': lr_actor  },
                                              {'params': self.policy.critic.parameters( ), 'lr': lr_critic } ] )

        self.policy_old = ActorCritic( state_dim, action_dim, action_std_init ).to( device )
        self.policy_old.load_state_dict( self.policy.state_dict( ) )

        self.MseLoss = nn.MSELoss( )

    def set_action_std( self, new_action_std ):

        self.action_std = new_action_std
        self.policy.set_action_std( new_action_std )
        self.policy_old.set_action_std( new_action_std )

    def decay_action_std( self, action_std_decay_rate, min_action_std ):

        self.action_std = self.action_std - action_std_decay_rate
        self.action_std = round( self.action_std, 4 )

        if (self.action_std <= min_action_std):
            self.action_std = min_action_std
            print("setting actor output action_std to min_action_std : ", self.action_std)

        else:
            print("setting actor output action_std to : ", self.action_std)

        self.set_action_std( self.action_std )

    def select_action(self, state):

        with torch.no_grad():
            state = torch.FloatTensor( state ).to( device )

            # The policy network maps to the "mean" of the Gaussian Distribution.
            # It samples from the action,
            action, action_logprob, state_val = self.policy_old.act( state )

        #
        self.buffer.states.append(state)
        self.buffer.actions.append(action)
        self.buffer.logprobs.append( action_logprob )
        self.buffer.state_values.append( state_val )

        return action.detach().cpu().numpy().flatten()

    def update( self ):

        # Monte Carlo estimate of returns
        rewards = [ ]
        discounted_reward = 0

        for reward, is_terminal in zip( reversed( self.buffer.rewards ), reversed( self.buffer.is_terminals ) ):
            if is_terminal:
                discounted_reward = 0

            discounted_reward = reward + (self.gamma * discounted_reward)
            rewards.insert( 0, discounted_reward )

        # Normalizing the rewards
        rewards = torch.tensor( rewards, dtype = torch.float32 ).to( device )
        rewards = ( rewards - rewards.mean( ) ) / ( rewards.std( ) + 1e-7 )

        # convert list to tensor
        old_states       = torch.squeeze( torch.stack( self.buffer.states      , dim = 0 ) ).detach( ).to( device )
        old_actions      = torch.squeeze( torch.stack( self.buffer.actions     , dim = 0 ) ).detach( ).to( device )
        old_logprobs     = torch.squeeze( torch.stack( self.buffer.logprobs    , dim = 0 ) ).detach( ).to( device )
        old_state_values = torch.squeeze( torch.stack( self.buffer.state_values, dim = 0 ) ).detach( ).to( device )

        # calculate advantages
        advantages = rewards.detach( ) - old_state_values.detach( )

        # Optimize policy for K epochs
        for _ in range( self.K_epochs ):

            # Evaluating old actions and values
            logprobs, state_values, dist_entropy = self.policy.evaluate( old_states, old_actions )

            # match state_values tensor dimensions with rewards tensor
            state_values = torch.squeeze( state_values )

            # Finding the ratio (pi_theta / pi_theta__old)
            ratios = torch.exp( logprobs - old_logprobs.detach( ) )

            # Finding Surrogate Loss
            surr1 = ratios * advantages
            surr2 = torch.clamp( ratios, 1 - self.eps_clip, 1 + self.eps_clip ) * advantages

            # final loss of clipped objective PPO
            # This also includes the entropy for the cost function
            # Minus sign since we are originally doing gradient ascent, should flipt
            # This is optimizing the function at the same time:
            # loss_PPOobj = -torch.min( surr1, surr2 )
            # loss_val = 0.5 * self.MseLoss( state_values, rewards ) #- 0.01 * dist_entropy
            # loss_PPOobj.mean( ).backward( )
            # loss_val.backward( )

            loss = -torch.min( surr1, surr2 ) + 0.5 * self.MseLoss( state_values, rewards ) - 0.01 * dist_entropy

            self.optimizer.zero_grad( )
            loss.mean( ).backward( )
            self.optimizer.step( )

        # Copy new weights into old policy
        self.policy_old.load_state_dict( self.policy.state_dict( ) )

        # clear buffer
        self.buffer.clear()

    def save(self, checkpoint_path):
        torch.save( self.policy_old.state_dict( ), checkpoint_path )

    def load( self, checkpoint_path ):
        self.policy_old.load_state_dict(  torch.load( checkpoint_path, map_location = lambda storage, loc : storage ) )
        self.policy.load_state_dict(      torch.load( checkpoint_path, map_location = lambda storage, loc : storage ) )

################################### Training ###################################
def train():
    print("============================================================================================")

    ####### initialize environment hyperparameters ######
    env_name = "Pendulum-v1"
    env = gym.make( env_name, g = 9.81 )

    # max_ep_len = env._max_episode_steps   # max timesteps in one episode, which is defined for each environment
    max_ep_len = 1000
    max_training_timesteps = int( 3e6 )   # break training loop if timeteps > max_training_timesteps

    print_freq = max_ep_len * 10          # print avg reward in the interval (in num timesteps)
    log_freq = max_ep_len * 2             # log avg reward in the interval (in num timesteps)
    save_model_freq = int(1e5)            # save model frequency (in num timesteps)

    action_std = 0.6                      # starting std for action distribution (Multivariate Normal)
    action_std_decay_rate = 0.05          # linearly decay action_std (action_std = action_std - action_std_decay_rate)
    min_action_std = 0.1                  # minimum action_std (stop decay after action_std <= min_action_std)
    action_std_decay_freq = int( 2.5e5 )  # action_std decay frequency (in num timesteps)
    #####################################################

    ## Note : print/log frequencies should be > than max_ep_len

    ################ PPO hyperparameters ################
    update_timestep = max_ep_len * 4.0    # update policy every n timesteps
    K_epochs = 80                         # update policy for K epochs in one PPO update

    eps_clip = 0.2                        # clip parameter for PPO
    gamma = 0.99                          # discount factor

    lr_actor = 0.0003                     # learning rate for actor network
    lr_critic = 0.001                     # learning rate for critic network

    random_seed = 1                       # set random seed if required (0 = no random seed)
    #####################################################

    # state space dimension
    state_dim = env.observation_space.shape[ 0 ]

    # action space dimension
    action_dim = env.action_space.shape[ 0 ]

    ###################### logging ######################

    #### log files for multiple runs are NOT overwritten
    log_dir = "PPO_logs"
    if not os.path.exists(log_dir):
          os.makedirs(log_dir)

    log_dir = log_dir + '/' + env_name + '/'
    if not os.path.exists(log_dir):
          os.makedirs(log_dir)

    #### get number of log files in log directory
    run_num = 0
    current_num_files = next( os.walk( log_dir ) )[ 2 ]
    run_num = len(current_num_files)

    #### create new log file for each run
    log_f_name = log_dir + '/PPO_' + env_name + "_log_" + str(run_num) + ".csv"

    print("current logging run number for " + env_name + " : ", run_num)
    print("logging at : " + log_f_name)
    #####################################################

    ################### checkpointing ###################
    run_num_pretrained = 0      #### change this to prevent overwriting weights in same env_name folder

    directory = "PPO_preTrained"
    if not os.path.exists( directory ):
        os.makedirs(directory)

    directory = directory + '/' + env_name + '/'
    if not os.path.exists(directory):
        os.makedirs(directory)

    checkpoint_path = directory + "PPO_{}_{}_{}.pth".format(env_name, random_seed, run_num_pretrained)
    print("save checkpoint path : " + checkpoint_path)
    #####################################################

    ############# print all hyperparameters #############
    print("--------------------------------------------------------------------------------------------")
    print("max training timesteps : ", max_training_timesteps)
    print("max timesteps per episode : ", max_ep_len)
    print("model saving frequency : " + str(save_model_freq) + " timesteps")
    print("log frequency : " + str(log_freq) + " timesteps")
    print("printing average reward over episodes in last : " + str(print_freq) + " timesteps")
    print("--------------------------------------------------------------------------------------------")
    print("state space dimension : ", state_dim)
    print("action space dimension : ", action_dim)
    print("--------------------------------------------------------------------------------------------")

    print("Initializing a continuous action space policy")
    print("--------------------------------------------------------------------------------------------")
    print("starting std of action distribution : ", action_std)
    print("decay rate of std of action distribution : ", action_std_decay_rate)
    print("minimum std of action distribution : ", min_action_std)
    print("decay frequency of std of action distribution : " + str(action_std_decay_freq) + " timesteps")

    print("--------------------------------------------------------------------------------------------")
    print("PPO update frequency : " + str(update_timestep) + " timesteps")
    print("PPO K epochs : ", K_epochs)
    print("PPO epsilon clip : ", eps_clip)
    print("discount factor (gamma) : ", gamma)
    print("--------------------------------------------------------------------------------------------")
    print("optimizer learning rate actor : ", lr_actor)
    print("optimizer learning rate critic : ", lr_critic)

    if random_seed:
        print("--------------------------------------------------------------------------------------------")
        print("setting random seed to ", random_seed)
        torch.manual_seed( random_seed )
        env.reset( seed = random_seed )
        np.random.seed( random_seed )

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

    print("============================================================================================")

    ################# training procedure ################

    # initialize a PPO agent
    ppo_agent = PPO( state_dim, action_dim, lr_actor, lr_critic, gamma, K_epochs, eps_clip, action_std )

    # track total training time
    start_time = datetime.now().replace( microsecond=0 )
    print("Started training at (GMT) : ", start_time )

    print("============================================================================================")

    # logging file
    log_f = open(log_f_name,"w+")
    log_f.write('episode,timestep,reward\n')

    # printing and logging variables
    print_running_reward = 0
    print_running_episodes = 0

    log_running_reward = 0
    log_running_episodes = 0

    time_step = 0
    i_episode = 0

    # The Total training Loop
    while time_step <= max_training_timesteps:

        state, _ = env.reset( seed = random_seed )
        current_ep_reward = 0

        # A Single trial of simulation
        for _ in range( 1, max_ep_len+1 ):

            # Select action with policy
            # Note that the state and action are appended in the buffer here.
            action = ppo_agent.select_action( state )
            state, reward, done, truncated, _ = env.step( action )

            # if done or truncated: done = True

            # Saving Reward and is_terminals
            ppo_agent.buffer.rewards.append( reward )
            ppo_agent.buffer.is_terminals.append( done )

            time_step += 1
            current_ep_reward += reward

            # Update PPO agent + Clear Buffer
            if time_step % update_timestep == 0:
                ppo_agent.update( )

            # If Continuous action space; then decay action std of ouput action distribution
            if time_step % action_std_decay_freq == 0:
                ppo_agent.decay_action_std( action_std_decay_rate, min_action_std )

            # log in logging file
            if time_step % log_freq == 0:

                # log average reward till last episode
                log_avg_reward = log_running_reward / log_running_episodes
                log_avg_reward = round(log_avg_reward, 4)

                log_f.write('{},{},{}\n'.format(i_episode, time_step, log_avg_reward))
                log_f.flush()

                log_running_reward = 0
                log_running_episodes = 0

            # Printing Average Reward
            if time_step % print_freq == 0:

                # print average reward till last episode
                print_avg_reward = print_running_reward / print_running_episodes
                print_avg_reward = round( print_avg_reward, 2 )

                print("Episode : {} \t\t Timestep : {} \t\t Average Reward : {}".format( i_episode, time_step, print_avg_reward ) )

                print_running_reward = 0
                print_running_episodes = 0

            # Save model weights
            if time_step % save_model_freq == 0:
                print("--------------------------------------------------------------------------------------------")
                print("saving model at : " + checkpoint_path )
                ppo_agent.save( checkpoint_path )
                print("model saved")
                print("Elapsed Time  : ", datetime.now().replace(microsecond=0) - start_time)
                print("--------------------------------------------------------------------------------------------")

            # break; if the episode is over
            if done:
                break

        print_running_reward   += current_ep_reward
        print_running_episodes += 1

        log_running_reward   += current_ep_reward
        log_running_episodes += 1

        i_episode += 1

    log_f.close()
    env.close()

    # print total training time
    print("============================================================================================")
    end_time = datetime.now().replace(microsecond=0)
    print("Started training at (GMT) : ", start_time)
    print("Finished training at (GMT) : ", end_time)
    print("Total training time  : ", end_time - start_time)
    print("============================================================================================")

if __name__ == '__main__':
    train()


import os

import numpy as np
import matplotlib.pyplot as plt

def plot_all_gram_matrices(content_gms, style_gms, iteration, write_dir):
    """
    gms is namedtuple, key = Tensors (B, C_i, C_i)
    """
    for layer_name, content_gm in content_gms._asdict().items():
        style_gm = getattr(style_gms, layer_name)
        content_gm, style_gm = tensor2np(content_gm), tensor2np(style_gm)
        content_title = f'[{layer_name}] Content Image Gram Matrix'
        style_title = f'[{layer_name}] Style Image Gram Matrix'

        fname = f'{layer_name}_iter{iteration:07}.png'
        w_path = os.path.join(write_dir, fname)
        plot_gram_matrix_pair(content_gm, style_gm,
                              content_title, style_title,
                              fname = w_path)
    plt.close('all')
    return None

def plot_gram_matrix_pair(content_gm, style_gm, title1, title2, fname = None):
    """
    both gm1, gm2 are np.array (1, C_i, C_i)
    """
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize= (15, 8))
    ax1.imshow(content_gm[0], vmin = 0, vmax = 0.08)
    ax1.set_title(title1)
    ax2.imshow(style_gm[0], vmin = 0, vmax = 0.08)
    ax2.set_title(title2)
    plt.tight_layout()
    if fname is not None:
        fig.savefig(fname)
        print(f'figure saved: {fname}')
    return None

def plot_image_pair(stylised_t, orig_t, iteration, write_dir):
    """
    both stylised_t, orig_t are Tensors (C, H, W)
    """
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize= (15, 8))
    stylised_img = tensor2uint8(stylised_t)
    orig_img = tensor2uint8(orig_t)
    ax1.imshow(orig_img)
    ax2.imshow(stylised_img)
    ax1.axis('off')
    ax2.axis('off')
    plt.tight_layout()

    fname = f'stylised_iter{iteration:07}.png'
    w_path = os.path.join(write_dir, fname)
    fig.savefig(w_path)
    print(f'stylised image written: {w_path}')
    plt.close('all')
    return None

def tensor2np(t):
    return t.detach().cpu().numpy()

def tensor2uint8(t):
    np_arr = t.detach().clamp(0, 255).cpu().numpy()
    return np_arr.transpose(1, 2, 0).astype(np.uint8)


import customtkinter
from tkinter import messagebox
from tkinter import *

customtkinter.set_appearance_mode("dark")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"

class App(customtkinter.CTk):
        def __init__(self):
                super().__init__()

                self.title("Diet Check")
                self.iconbitmap("C:/Users/Safiya/Desktop/meehad17/dietcheck/favicon.ico")
                self.geometry(f"{400}x{380}")
                self.grid_columnconfigure(1, weight=1)
                self.grid_columnconfigure((2, 3), weight=0)
                self.grid_rowconfigure((0, 1, 2), weight=1)

                self.sidebar_frame = customtkinter.CTkFrame(self, width=140, corner_radius=0)
                self.sidebar_frame.grid(row=0, column=0, columnspan=2, sticky="nsew")
                self.sidebar_frame.grid_columnconfigure(2, weight=1)

                sugarlevel = StringVar()
                sugarlevel.set('')

                customtkinter.CTkLabel(self.sidebar_frame, text="Welcome to Diet Check", font=customtkinter.CTkFont(size=23, weight="bold")).grid(row=0,column=0,columnspan=2)
                customtkinter.CTkLabel(self, text="\nWhat is your sugar level: ",font=customtkinter.CTkFont(size=13, weight="normal")).grid(row=1,column=0)

                self.sugarlevel = customtkinter.CTkEntry(self,textvariable=sugarlevel,placeholder_text="For eg:123,etc")
                self.sugarlevel.grid(row=1, column=1)

                customtkinter.CTkButton(self, text='SUBMIT', command=self.Submit).grid(row=2,column=0,columnspan=2,ipadx=135)
                customtkinter.CTkLabel(self, text='\nYou should have Foods that is high in:',font=customtkinter.CTkFont(size=13, weight="normal")).grid(row=3,column=0)

                self.diet = customtkinter.CTkEntry(self)
                self.diet.grid(row=3,column=1)

                customtkinter.CTkButton(self, text='VIEW MORE',command=self.more).grid(row=4,column=0,columnspan=2,ipadx=135)
                customtkinter.CTkLabel(self, text='\nFor example  :\n',font=customtkinter.CTkFont(size=13, weight="normal")).grid(row=5,column=0)
                self.d = customtkinter.CTkEntry(self)

                self.d.grid(row=5,column=1)

                customtkinter.CTkButton(self, text='RESET',command=self.Reset).grid(row=6,column=1)
                customtkinter.CTkButton(self, text='HISTORY',command=self.Reset).grid(row=7,column=1)
                customtkinter.CTkButton(self, text='HELP', command=self.Help).grid(row=8,column=1)
                customtkinter.CTkButton(self, text='EXIT', command=self.Exit,fg_color='red',hover_color='dark red').grid(row=9,column=1)

                self.appearance_mode_label = customtkinter.CTkLabel(self, text="Appearance Mode:", anchor="w")
                self.appearance_mode_label.grid(row=6, column=0, padx=20, pady=(10, 0))
                self.appearance_mode_optionemenu = customtkinter.CTkOptionMenu(self, values=[ "Dark","Light", "System"],command=self.change_appearance_mode_event)
                self.appearance_mode_optionemenu.grid(row=7, column=0, padx=20, pady=(10, 10))
                self.scaling_label = customtkinter.CTkLabel(self, text="UI Scaling:", anchor="w")
                self.scaling_label.grid(row=8, column=0, padx=20, pady=(10, 0))
                self.scaling_optionemenu = customtkinter.CTkOptionMenu(self, values=["80%", "90%", "100%", "110%", "120%"],command=self.change_scaling_event)
                self.scaling_optionemenu.grid(row=9, column=0, padx=20, pady=(10, 20))

        def Submit(self):
                try:
                        global x
                        x=self.sugarlevel.get()
                        int(x)
                        if float(x) < 69:
                                messagebox.showwarning('Diet Check','You should consult a nearest doctor you are in critical condition')
                                self.diet.insert(0,'carbohydrates and foods high in protein')
                        elif float(x) < 79:
                                self.diet.insert(0,'carbohydrates and foods high in protein')
                        elif float(x) > 240:
                                messagebox.showwarning('Diet Check','You should consult a nearest doctor you are in critical condition')
                                self.diet.insert(0,'Magneisum,fiber and Protein')
                        elif float(x) > 110:
                                self.diet.insert(0,'Magneisum,fiber and Protein')
                        else:
                                messagebox.showinfo('Diet Check','You have normal sugarlevel maintain this sugar level')
                                return True
                except:
                        messagebox.showwarning('Diet Check','Type in number')
                        self.sugarlevel.delete(0 , END)
                        self.diet.delete(0 , END)
                        return False
                        raise

        def more(self):
                if float(x) < 79:
                        self.d.insert(0,'Rice,Bread,Fruits and Sugar, and Meats,Fish,Eggs,Milk and Diary products, and Oily foods')
                elif float(x) > 110:
                        self.d.insert(0,'Raw, Cooked, or Roasted Vegetables, Melon or Berries')
                else:
                        self.d.insert(0,'')
                        return

        def Exit(self):
                self.qExit = messagebox.askyesno('Diet Check','do you want to exit the program')
                if self.qExit > 0:
                        app.destroy()
                        return

        def Reset(self):
                self.sugarlevel.delete(0 , END)
                self.diet.delete(0 , END)
                self.d.delete(0, END)
                return

        # def Help(self):
        #         root2=customtkinter.CTkToplevel()
        #         root2.title("Diet Check")
        #         root2.iconbitmap('C:/Users/Safiya/Desktop/meehad17/dietcheck/favicon.ico')
        #         def Exit2():
        #                 root2.destroy()
        #                 return
        #         customtkinter.CTkLabel(root2, text='HELP').pack()#,customtkinter.CTkFont(size=12, weight="normal")).pack()
        #         customtkinter.CTkLabel(root2, text='''Type your sugarlevel in the textbox near where it asks what is your sugarlevel then click submit and
        #         it will show what nutrients you are rpyeqired but if you dont know what food ypu should consume to get these nutrients
        #         click view more it will show food items with these nutrients.
        #         If you enter wrong sugarlevel by mistake dont worry just click reset and it will reset your entry.
        #         If you want to exit program click exit and click yes then the program will close.
        #         (NOTE:You should consult a nearest doctor in critical condition as this program only advices diet in uncritical condition)
        #         Have a nice day''').pack()#,customtkinter.CTkFont(size=8, weight="normal")).pack()
        #         customtkinter.CTkButton(root2, text='OK', command=Exit2()).pack()

        def Help(self):
                root2=customtkinter.CTkToplevel()
                root2.title("Diet Check")
                root2.configure(background="white")
                root2.iconbitmap('C:/Users/Safiya/Desktop/meehad17/dietcheck/favicon.ico')
                def Exit2():
                        root2.destroy()
                        return
                customtkinter.CTkLabel(root2, text='HELP').pack()
                customtkinter.CTkLabel(root2, text='''Type your sugarlevel in the textbox near where it asks what is your sugarlevel then click submit and
                        it will show what nutrients you are rpyeqired but if you dont know what food ypu should consume to get these nutrients
                        click view more it will show food items with these nutrients.
                        If you enter wrong sugarlevel by mistake dont worry just click reset and it will reset your entry.
                        If you want to exit program click exit and click yes then the program will close.
                        (NOTE:You should consult a nearest doctor in critical condition as this program only advices diet in uncritical condition)
                                Have a nice day''').pack()
                customtkinter.CTkButton(root2, text='OK',command=Exit2).pack()

        def change_appearance_mode_event(self, new_appearance_mode: str):
                customtkinter.set_appearance_mode(new_appearance_mode)

        def change_scaling_event(self, new_scaling: str):
                new_scaling_float = int(new_scaling.replace("%", "")) / 100
                customtkinter.set_widget_scaling(new_scaling_float)

if __name__ == "__main__":
        app = App()
        app.mainloop()


import unittest
from app.services import RabbitMQ, Broker, MemoryBroker
from app.services.broker import JsonSerializer, Message
from app.core.config import settings

class TestSerializer(unittest.TestCase):
    def setUp(self) -> None:
        self.serializer = JsonSerializer()

    def test_encode(self):
        message = {
            'key': 'value'
        }

        encoded_message = self.serializer.encode(message)
        assert encoded_message == b'{"key": "value"}'

    def test_decode(self):
        encoded_message = b'{"key": "value"}'
        decoded_message = self.serializer.decode(encoded_message)
        assert decoded_message == {'key': 'value'}

class TestMemoryBroker(unittest.IsolatedAsyncioTestCase):
    def setUp(self) -> None:
        self.broker = MemoryBroker(delay=0.1)

    def process_message(self, message: dict) -> dict:
        assert message.get('key') == 'value'
        return message

    async def test_publish_and_consume(self):
        await self.broker.publish('queue_name', Message(body={'key': 'value'}))
        await self.broker.consume(None, queue_name='queue_name', on_message=self.process_message)

class TestRabbitMQBroker(unittest.IsolatedAsyncioTestCase):
    async def asyncSetUp(self):
        self.broker: Broker = RabbitMQ(
            settings.rabbitmq.address,
            settings.rabbitmq.port,
            settings.rabbitmq.username,
            settings.rabbitmq.password
        )

    async def test_publish_message(self):
        await self.broker.publish(
            queue_name='test_authentication',
            message=Message({'message': 'test'})
        )


import aiohttp
import asyncio

from lxml import etree

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.119 Safari/537.36'
}

# 获取网页（文本信息）
async def fetch(session, url):
    async with session.get(url, headers=headers) as response:
        return await response.text(encoding='utf-8')

# 获取每一页的所有图片路径
async def url_parse(html):
    print(html)
    exit()
    selector = etree.HTML(html)
    url_list = selector.xpath('//ul[@class="ali"]//li//img/@src')
    print(url_list)
    exit()
    return url_list

# 进行图片的下载
async def down_img(session, url_list):
    for each_url in url_list:
        print('程序正在采集%s' % each_url)
        async with session.get(each_url, headers=headers) as response:
            img_response = await response.read()
            with open('./image/%s.jpg' % time.time(), 'wb') as file:
                file.write(img_response)

# 开始执行抓取
async def start(urlList):
    for url_i in urlList:
        async with aiohttp.ClientSession() as session:
            html = await fetch(session, url_i)  # 得到每一页的html
            url_list = await url_parse(html)  # 解析得到每一页的图片url
            await down_img(session, url_list) # 进行图片的下载

if __name__ == '__main__':
    each_url = "http://www.ivsky.com/tupian/ziranfengguang/index_{page}.html"
    full_urllist = [each_url.format(page=i) for i in range(1, 20)]
    asyncio.run(start(full_urllist))


""" Orbital Mechanics for Engineering Students Problem 1.2
Question:
Given four vectors A, B, C, and D and the identities from Problem 1.1, show
    (A x B) . (C x D) = (A . C)(B . D) - (A . D)(B. C)
Written by: J.X.J. Bannwarth
"""
from sympy.vector import CoordSys3D
from sympy.vector import Vector
from sympy import symbols
from sympy import simplify

# Title
print("Orbital Mechanics for Engineering Students Problem 1.2")

# Coordinate system
N = CoordSys3D('N')
i, j, k = N.base_vectors()

# Define vectors
ax, ay, az = symbols("ax ay az")
bx, by, bz = symbols("bx by bz")
cx, cy, cz = symbols("cx cy cz")
dx, dy, dz = symbols("dx dy dz")
A = ax * i + ay * j + az * k
B = bx * i + by * j + bz * k
C = cx * i + cy * j + cz * k
D = dx * i + dy * j + dz * k

# Analytical
print("(1) Answer")
print("(A x B) . (C x D)")
print("A . [B x (C x D)] - interchangeability")
print("A . [C(B . D) - D(B . C)] - bac-cab")
print("(A . C)(B . D) - (A . D)(B . C) - expand")

# Check the equations work out
print("\n(2) Check")
LHS = simplify((A.cross(B)).dot(C.cross(D)))
RHS = simplify(A.dot(C) * B.dot(D) - A.dot(D) * B.dot(C))
print("(A x B) . (C x D) =", LHS)
print("(A . C)(B . D) - (A . D)(B . C) =", RHS)
if simplify(LHS - RHS) == 0:
    print("(A x B) . (C x D) == (A . C)(B . D) - (A . D)(B. C)")


# -*- coding: utf8 -*-
from Jeu.script import *
from Xml.handlers import *
class TireScript(Script):
    def __init__(self,t,perso):
        super(TireScript,self).__init__(t[0],t[1])
        self.sprites[0].setContainer(self)
        tire_collide.add(self.sprites[0])
        cs = perso.currentSprite()
        self.x=perso.x+(cs.display_rect.width-self.sprites[0].display_rect.width)/2
        self.y=perso.y-self.sprites[0].display_rect.height
        self.perso = perso
        self.first=True
    def update(self):
       if(self.first):
           self.sons[0].play()
           self.first=False
       self.y-=5
       if(self.y+self.sprites[0].display_rect.height <= self.perso.side.top):
           return True
       self.sprites[0].setCoord(self.x,self.y)
       self.sprites[0].update()
       return False
import pygame.sprite
tire_collide=pygame.sprite.Group()
scripts=[]
def collide(sprites):
    collided=[]
    for s in sprites:
        collided.append(s.container)
    for c in collided:
        if(c in scripts):
            scripts.remove(c)
            tire_collide.remove(c.sprites[0])
def newScript(app,perso):
    script = getScriptFromXML("..\data\Persos\LoadRan\Scripts\TireScript.xml",app.display.backbuffer)
    ts = TireScript(script,perso)
    scripts.append(ts)

def update():
    finished=[]
    for s in scripts:
        if(s.update()):
            finished.append(s)
    for f in finished:
        scripts.remove(f)
        tire_collide.remove(f.sprites[0])

def load(app,perso):
    getScriptFromXML("..\data\Persos\LoadRan\Scripts\TireScript.xml",app.display.backbuffer)


# -*- coding: utf-8 -*-
"""
Local settings

- Run in Debug mode

- Use mailhog for emails

- Add Django Debug Toolbar
- Add django-extensions as app
"""

import socket
import os
from .common import *  # noqa

# DEBUG
# ------------------------------------------------------------------------------
DEBUG = env.bool('DJANGO_DEBUG', default=True)
TEMPLATES[0]['OPTIONS']['debug'] = DEBUG

ALLOWED_HOSTS = env.list('DJANGO_ALLOWED_HOSTS', default=[
    'localhost', '127.0.0.1', 'webmarks.net'])
if not DEBUG:
    print ("env variable DJANGO_DEBUG is False !!!")
    # start django with --insecure for static file

# SECRET CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
# Note: This key only used for development and testing.
SECRET_KEY = env('DJANGO_SECRET_KEY',
                 default='r(f_)4bp@%ritjy#gq19f0_z+c8+#zr=0b@)w8)_-f=+)*k0j0')

# Mail settings
# ------------------------------------------------------------------------------

EMAIL_PORT = 1025

EMAIL_HOST = env("EMAIL_HOST", default='localhost')

# CACHING
# ------------------------------------------------------------------------------
# CACHES = {
#     'default': {
#         'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
#         'LOCATION': ''
#     }
# }
# CACHING
# ------------------------------------------------------------------------------
REDIS_LOCATION = "redis://{}:{}/0".format(
    env('REDIS_ENDPOINT_ADDRESS', default='127.0.0.1'),
    env('REDIS_PORT', default=6379)
)

# Heroku URL does not pass the DB number, so we parse it in
CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': REDIS_LOCATION,
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
            'IGNORE_EXCEPTIONS': True,  # mimics memcache behavior.
                                        # http://niwinz.github.io/django-redis/latest/#_memcached_exceptions_behavior
        }
    }
}

# django-channels
# ------------------------------------------------------------------------------
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "asgiref.inmemory.ChannelLayer",
        "ROUTING": "config.routing.channel_routing",
    },
}

# CHANNEL_LAYERS = {
#     "default": {
#         "BACKEND": "asgi_redis.RedisChannelLayer",
#         "ROUTING": "config.routing.channel_routing",
#         # "CONFIG": {
#         #     "hosts": [("redis-channel-1", 6379), ("redis-channel-2", 6379)],
#         # },
#     },
# }

# django-extensions
# ------------------------------------------------------------------------------
INSTALLED_APPS += ('django_extensions', )

# CELERY
# In development, all tasks will be executed locally by blocking until the
# task returns
CELERY_ALWAYS_EAGER = False
# END CELERY

# Your local stuff: Below this line define 3rd party library settings
# ------------------------------------------------------------------------------


#coding=utf8
'''
Anna Bonazzi, 16/08/2017

Script to make a plot of corpus word frequency evolution over time by language.
Word frequency is taken from the corpus.vrt file, one text at a time.

Works both for corpora with a "language" attribute and for corpora with different percentages for each language
'''
# VARIABLES FOR USER TO CHANGE:

corpus = '/home/bonz/Documents/Corpora/geothermie_test3.vrt'

#selected_sources = '/home/bonz/Documents/Corpus_work/Quellen/pbv_quellen_juli.txt' # Comment out if not needed

keyword = 'Geothermie'

regex_fr = '(G|g)(e|é)o(t|th)erm.*?'
regex_de = '(G|g)(e|é)o(t|th)erm.*?'
regex_en = '(G|g)(e|é)o(t|th)erm.*?'
regex_it = '(G|g)(e|é)o(t|th)erm.*?'

# Time window to search
min_year = 2007; max_year = 2017

frequency = 'per_million' # Options: 'absolute', 'per_million'
unit = 'word' # Options: 'word', 'pos', 'lemma'

# Overall plot attributes
myfont = 'DejaVu Sans' # Arial
fontcolor = '#185a92' # Pick colors from http://htmlcolorcodes.com/
#--------------------------
# To time the script
from datetime import datetime
startTime = datetime.now()
import os, glob, re
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
#--------------------------

# 1) Collects data

# Prepares dictionary to save word freq by language
de = {}; fr = {}; it = {}; en = {}
dic = {}
text_counter = 0
all_words = {}
# Temporary text chunk to be analyzed
chunk = []

# Optional ist of sources to be searched for specific queries (e.g. media)
sources = []
try:
	with open (selected_sources, 'r') as f:
		for word in f:
			sources.append(word.strip('\n'))
except:
	pass

# Goes through the corpus one text at at time
with open (corpus, 'r') as f:
	print ('  Searching corpus. Go have some coffee...')
	for line in f:
		if '</text>' not in line:
			if '<' not in line:
				units = {'word' : 0, 'pos' : 1, 'lemma' : 2}
				word = line.split("\t")[units[unit]] # 0 wordform, 1 pos, 2 lemma
				chunk.append(word) # Fills temporary text chunk with lemmas
			elif '<text' in line:
				chunk.append(line)
		else: # Meets text end
			# Searches for selected lang and class combination
			regex = re.search('.*?class="(.*?)".*?date_published="(.*?)".*?source="(.*?)".*?', ''.join(chunk))
			if regex:
				regex2 = re.search('language="(.*?)"', ''.join(chunk))
				if regex2:
					lang = regex2.group(1)
				else:
					languages = re.findall('(de=".*?)".*?(en=".*?)".*?(fr=".*?)".*?(it=".*?)".*?(other=".*?)"', ''.join(chunk))
					# Makes lang - percent pairs
					lang_dic = {}
					if languages:
						for l in list(languages[0]):
							lang_dic[l.split('="')[0]] = float(l.split('="')[1])
						# Sorts percents to find the highest
						languages = [float(l.split('="')[1]) for l in list(languages[0])]
						languages.sort()
						candidates = []
						for l in lang_dic:
							if lang_dic[l] == languages[-1]:
								candidates.append(l)
								# If there are several options, 'other' does not get picked
								lang = candidates[0]
								if len(candidates) > 1 and 'other' in candidates:
									if lang == 'other':
										lang = candidates[1]
									if len(candidates) > 1 and 'en' in candidates and 'other' not in candidates:
										if lang == 'en':
											lang = candidates[1]
								if len(candidates) > 2 and 'en' in candidates and 'other' in candidates:
									if lang == 'en':
										lang = candidates[1]
										if len == 'other':
											lang == [2]

				cl = regex.group(1); source = regex.group(3)
				try:
					l = len(selected_sources)
					print (l)
				except:
					sources = []
					sources.append(source)
				# Counts words by language
				if lang in all_words:
					all_words[lang] += 1
				else:
					all_words[lang] = 1

				# Looks only in selected sources
				if source in sources:
					if '-' in regex.group(2):
						date = regex.group(2)
						# Checks date: right format, right time window
						if len(date.split('-')[0]) == 4 and int(date.split('-')[0]) >= min_year and int(date.split('-')[0]) <= max_year and int(date.split('-')[0]) < 9999:
							if int(date.split('-')[1]) > 7: # Makes half-years
								year = str(date.split('-')[0]) + ".2"
							else:
								year = str(date.split('-')[0]) + ".1"

							#------------------
							# Counts searchword frequency per language or year
							def fill_dic(dic, year, lang, reg):
								for word in chunk:
									regex2 = re.search(reg, word)
									if regex2:
										if year in dic:
											dic[year] += 1
										else:
											dic[year] = 1
								return dic
							#------------------
							if lang == 'fr':
								fr = fill_dic(fr, year, lang, regex_fr)
							if lang == 'de':
								de = fill_dic(de, year, lang, regex_de)
							if lang == 'en':
								en = fill_dic(en, year, lang, regex_en)
							if lang == 'it':
								it = fill_dic(it, year, lang, regex_it)

			chunk = [] # Resets empty text chunk
			text_counter += 1
			if '00000' in str(text_counter):
				print ("\tText " + str(text_counter))

#------------------------------------
# 2) Starts making plot

print ('Starting plot')
# Sorts dictionaries and fills x/y lists
def plot_line(dic, linecolor, lang_tot):
	# Fills x/y axes
	x = []
	y = []
	xlist = []
	# Absolute Frequency
	if frequency == 'absolute':
		ytitle = 'Absolute Frequenz'
		for i in range (2007, 2018):
			if str(i + 0.1) in dic:
				xlist.append(str(i))
				y.append(dic[str(i + 0.1)])
			else:
				xlist.append(str(i))
				y.append(0)
			if str(i + 0.2) in dic:
				xlist.append('')
				y.append(dic[str(i + 0.2)])
			else:
				xlist.append('')
				y.append(0)
		for i in range (1, len(xlist) + 1):
			x.append(i)
	else:
		# Frequency per million
		ytitle = 'Frequenz per 100'
		for i in range (min_year, max_year + 1):
			if str(i + 0.1) in dic:
				xlist.append(str(i))
				freq = int(dic[str(i + 0.1)]) * 100 / lang_tot
				y.append(float("{:.5f}".format(freq)))
			else:
				xlist.append(str(i))
				y.append(0)
			if str(i + 0.2) in dic:
				xlist.append('')
				freq = int(dic[str(i + 0.2)]) * 100 / lang_tot
				y.append(float("{:.5f}".format(freq)))
			else:
				xlist.append('')
				y.append(0)
		for i in range (1, len(xlist) + 1):
			x.append(i)

	# Plots the line
	plt.plot(x, y, color=linecolor, linewidth = 3,  marker='o', linestyle='-') # label =

	return xlist, x, y, ytitle
#------------------

xlist, x, y, ytitle = plot_line(fr, '#bbcbdb', all_words['fr'])
plot_line(de, '#9ebd9e', all_words['de'])
plot_line(en, '#dd855c', all_words['en'])
plot_line(it, '#745151', all_words['it'])

# Set ax ticks
plt.xticks(x, xlist, fontname = myfont, fontsize = '16') # xlist,
plt.yticks(fontname = myfont, fontsize = '16')

# Get current figure and then add subplot - to do additional format stuff
fig = plt.gcf()
ax = fig.add_subplot(111)
'''
# Set datapoint labels:
pointlabels = ['60\nMio.', '114.8\nMio.', '166.6    \nMio.', '331.6\n   Mio.     ', '    358\n     Mio.', '  N/A', '457.9\nMio.', '1.03\nMia.']
for i, j in zip(x, y):
	# Pick element from desired label list through index of current i
	pointlabel = pointlabels[x.index(i)]
	ax.annotate(pointlabel, xy=(i,j), xytext=(-25,15), textcoords='offset points', fontname = myfont, fontsize = '16')
'''

# Set ax labels
#plt.xlabel('Releases', color = fontcolor, fontname = myfont, fontsize = '20', position = (0.5, 0.5))

plt.ylabel(ytitle, color = fontcolor, fontname = myfont, fontsize = '20', position = (-1, 0.5))

# Set title
plt.title(keyword + '\n', color = fontcolor, fontname = myfont, fontsize = '24')

# Set subtitle (use suptitle - moves up with \n)
plt.suptitle('\n\nEntwicklung je nach Sprache (' + xlist[0] + '-' + xlist[-2] + ')', color = fontcolor, fontname = myfont, fontsize = '16')# , position = (-1, 0.5))

# Set legend
leg = plt.legend(loc='best', frameon=False, facecolor = 'r',  labels = ['Fr', 'De', 'En', 'It']) # edgecolor = 'b', # Don't have to specify them unless to change them

# Change all legend
for text in leg.get_texts():
    text.set_fontsize(16)
    text.set_fontname(myfont)
    text.set_color(fontcolor)

# Change legend elements individually
#text = leg.get_texts() # get lines, patches, title, set_frame_on(True|False), set_title
#text[0].set_color('c')

 # Set / edit edge
plt.box(on=True) # Or nothing, to make the box disappear

# Set / edit grid
plt.grid()
ax.grid(color='paleturquoise', linestyle='-.', linewidth=0.5) # Requires ax = fig.add_subplot(111)

# Set background
# fig.set_facecolor('c') # Color around the plot
#ax.set_facecolor('#f5ffff') # Needs box to work

# Edit plot size
fig.set_size_inches(12,6.5)# Needs fig = plt.gcf()
plt.tight_layout() # Adjusts subplot params for the subplot(s) to fit in the figure area

# Show plot
plt.show()
# Save plot
fig.savefig('/home/bonz/Documents/Corpus_work/Images/100-3_entwickl_geo_medien.png')

#--------------------------
# To time the script
time = datetime.now() - startTime
print ("\n(Script running time: " + str(time) + ")")


def get_rank_lotto(is_including_bnusNo):
    from app.controllers import connect_db
    is_including_bnusNo = int(is_including_bnusNo)
    rank = {}
    conn = connect_db()
    curs = conn.cursor()
    try:
        if is_including_bnusNo:
            sql = """select drwtNo1,drwtNo2,drwtNo3,drwtNo4,drwtNo5,drwtNo6,bnusNo from lotto_draw_info"""
        else:
            sql = """select drwtNo1,drwtNo2,drwtNo3,drwtNo4,drwtNo5,drwtNo6 from lotto_draw_info"""

        curs.execute(sql)
        rows = curs.fetchall()

        for t in rows:
            for i in t:
                if i in rank:
                    rank[i] += 1
                else:
                    rank[i] = 1

        # sort by desc
        rank = sorted(rank.items(), reverse=True, key=lambda item: item[1])

        # print(rank)
    except ValueError:
        print(ValueError)
    finally:
        curs.close()
        conn.close()
    return rank


from collections import defaultdict

class Solution(object):
    def coinChange(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int
        """
        # Edge: Amount = 0
        if amount == 0:
            return 0

        coinHash = defaultdict(bool)
        for coin in coins:
            coinHash[coin] = True

        dp = [None] * (amount+1)
        for i in range(len(dp)):
            if coinHash[i]:
                dp[i] = 1
                continue
            options = []
            for coin in coins:
                if i-coin > 0 and dp[i-coin] != None:
                    options.append(dp[i-coin] + 1)
            if len(options) > 0:
                dp[i] = min(options)

        if dp[-1] == None:
            return -1
        return dp[-1]