import telebot
from telebot import types
from aiogram.types import ReplyKeyboardRemove, \
    ReplyKeyboardMarkup, KeyboardButton, \
    InlineKeyboardMarkup, InlineKeyboardButton

import os
import django

from backend import local2 as local
from worker.models import Worker, TGBotCode

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
django.setup()

bot = telebot.TeleBot(os.environ.get('CHAT_BOT_TOKEN', local.CHAT_BOT_TOKEN))

def auth(message):
    msg = bot.send_message(message.chat.id, 'Введите код: ')
    bot.register_next_step_handler(msg, code_authorization)

def code_authorization(message):
    try:
        code = TGBotCode.objects.get(code=message.text)
    except TGBotCode.DoesNotExist:
        menu = get_menu(message)
        bot.send_message(
            message.chat.id,
            'Неверный код! Попробуйте авторизоваться еще раз!',
            reply_markup=menu
        )
    else:
        code.chat_id = message.chat.id
        code.save()
        menu = get_menu(message)
        bot.send_message(
            message.chat.id,
            f'Авторизация выполнена! Добро пожаловать, {code.user.get_full_name()}! ',
            reply_markup=menu
        )

def get_menu(message):
    menu = types.ReplyKeyboardMarkup(True, one_time_keyboard=True)
    try:
        TGBotCode.objects.get(chat_id=message.chat.id)
    except TGBotCode.DoesNotExist:
        menu.row('Авторизация')
        menu.row('Получить иформацию', 'Наставничество')
    else:
        menu.row('Выход')
        menu.row('Получить иформацию', 'Наставничество')
        menu.row('Моя информация', 'Мероприятия')
    finally:
        return menu

def make_keyboard(items, name):
    keyboard = types.InlineKeyboardMarkup()
    for item in items:
        keyboard.add(
            types.InlineKeyboardButton(
                item, callback_data=f'{name}:{item}'
            )
        )
    return keyboard


n, s, m = map(int, input().split())
v = list(map(int, input().split()))

# dp 정의
# 행에는 최대볼륨 열에는 곡의 개수
dp = [[0] * (m+1) for i in range(n+1)]
dp[0][s] = 1

for i in range(1, n+1): # 곡의 개수만큼
    for j in range(m+1): # 최대 볼륨
        if dp[i-1][j] != 0: # 볼륨 조절 가능하다면
            # print(dp[i-1][j])
            if 0<=j+v[i-1]<=m:
                dp[i][j+v[i-1]]=1
            if 0<=j-v[i-1]<=m:
                dp[i][j-v[i-1]]=1

# 볼륨 조절 불가한 경우 -1 출력을 위해 초기값
ans = -1
dp = dp[n]
# 최대값 찾기 위해 내림차순으로 for 문
for i in range(m, -1, -1):
    if dp[i]==1:
        ans = i
        break
print(ans)


with open("text.txt") as f:
    words = f.read().split()

counts = dict()

for word in words:
    if word in counts:
        counts[word] += 1
    else:
        counts[word] = 1

def get_count(x):
    return x[1]

result = sorted(counts.items(), key=get_count, reverse=True)

for key, count in result[:20]:
    print(f"{key}: {count}")


from Client0 import Client
from Seq1 import Seq
from colorama import Fore, init

init(autoreset=True)

PRACTICE = 2
EXERCISE = 5

print(f"-----| Practice {PRACTICE}, Exercise {EXERCISE} |------")

s1 = Seq()
filename = Seq.get_file2()
s1.read_fasta(filename)

frag1 = s1.strbases[:10]
frag2 = s1.strbases[11:20]
frag3 = s1.strbases[21:30]
frag4 = s1.strbases[31:40]
frag5 = s1.strbases[41:50]
frag6 = s1.strbases[51:60]
frag7 = s1.strbases[61:70]
frag8 = s1.strbases[71:80]
frag9 = s1.strbases[81:90]
frag10 = s1.strbases[91:100]

fragments_even = [frag2, frag4, frag6, frag8, frag10]
fragments_odd = [frag1, frag3, frag5, frag7, frag9]
i = 2
for f in fragments_even:
    # -- Parameters of the server to talk to
    IP = "127.0.0.1"
    PORT = 6123
    # -- Create a client object
    c = Client(IP, PORT)

    response = c.talk(Fore.YELLOW + f"Sending {i} gene to server")
    i = i + 2

p = 1
for f in fragments_odd:
    # -- Parameters of the server to talk to
    IP = "127.0.0.1"
    PORT = 8080
    # -- Create a client object
    c = Client(IP, PORT)

    response = c.talk(Fore.YELLOW + f"Sending fragment {p} gene to server")
    p = p + 2

s = 1
all_fragments = [frag1, frag2, frag3, frag4, frag5, frag6, frag7, frag8, frag9, frag10]
for t in all_fragments:
    print(f"Fragment {s}: {t}")
    s = s + 1

P2 done again/Session 9 - Practice 2/Exercise 6.py


from io import StringIO
from copy import deepcopy

class Singleton(type):
    _instances = {}

    def __call__(cls, *args, **kwargs):
        if not args and not kwargs:  # empty call
            if cls not in cls._instances:
                cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        else:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

    def clear(cls):
        try:
            del cls._instances[cls]
        except KeyError:
            pass

class ConfigurationError(Exception):
    pass

_DEFAULT_VALUE = object()

class Configuration(metaclass=Singleton):
    def __init__(self):
        self.initial_configurations_dict = dict()
        self.configurations_dict = dict()
        self.simulation_data = list()
        self.simulation_number = None

    def reset_to_original_configurations(self):
        self.configurations_dict = deepcopy(self.initial_configurations_dict)

    def __getitem__(self, key):
        # import inspect
        # print (inspect.getframeinfo(inspect.currentframe().f_back)[2])
        if key in self.configurations_dict:
            return self.configurations_dict[key]
        else:
            raise ConfigurationError("{} not found".format(key))

    def __setitem__(self, key, value):
        self.configurations_dict[key] = value

    def get(self, key, default=_DEFAULT_VALUE):
        try:
            return self.configurations_dict[key]
        except KeyError:
            if default is _DEFAULT_VALUE:
                raise
            else:
                return default

    def load_configuration_for_simulation(self, simulation):
        self.simulation_data = sorted(simulation.data)
        self.load_configurations_from_dict(simulation.configurations_dict)

    def load_configurations_from_dict(self, configurations_dict):
        self.configurations_dict = configurations_dict
        self.initial_configurations_dict = deepcopy(self.configurations_dict)

    def __str__(self):
        values_str_io = StringIO()
        print("Configurations:", end="\n", file=values_str_io)
        for (key, value) in sorted(self.configurations_dict.items()):
            value_string = ""
            if type(value) is dict:
                for (secondary_key, secondary_value) in self.configurations_dict[key].items():
                    value_string += (len(key) + 2) * " " + "{}: {}\n".format(secondary_key,
                                                                             secondary_value)  # manual justification
                value_string = value_string.strip()
            else:
                value_string = str(value)
            print("{}: {}".format(key, value_string), end="\n", file=values_str_io)

        return values_str_io.getvalue().strip()

    def clear(self):
        self.__init__()


from dataStructs import Stack
import re

def isdigit(ch):
	return re.search("\d+",ch)

def isalpha(ch):
	return re.search("\w+",ch)

def infixToPostfix(args):
	precedence = {"*":3,"/":3,"+":2,"-":2,"(":1}

	opStack = Stack() #operator stack.

	outputList = []

	for token in args:
		if isdigit(token) or isalpha(token):
			print("appending : " + token)
			outputList.append(token)
		elif token in "(":
			opStack.push(token)
		elif token == ")":
				op = opStack.pop()
				while op != "(" :
					print("appending rar : " + op)
					outputList.append(op)
					op = opStack.pop()
		elif token in "*/+-":
			while not opStack.isEmpty() and precedence[opStack.peek()] > precedence[token]:
				print("appending operator: " + token)
				outputList.append(opStack.pop())
			print("pushing : " + token)
			opStack.push(token)

		else:
			if not re.match('\s+',token):
				print("Invalid operator/operand encountered, quitting")
				break

	while not opStack.isEmpty():
		op = opStack.pop()
		print("After traversing is over appending : " + op)
		outputList.append(op)

	return " ".join(outputList)


import random as rn

gamesList = []

#read all games from txt file
file = open("gamesList.txt", "r")

#set each to list
for line in file:
    gamesList.append(line)

#find random number from index of list
gameID = rn.randint(0, len(gamesList) - 1)

#print
print("The game I decided you shall play is:")
print(gamesList[gameID])

dummy = input("This is a dummy: ")


with open('day20.txt') as file:
    lines = [line.strip() for line in file.readlines()]
    enhancement = lines[0]
    image = lines[2:]

def count(image, target):
    count = 0
    for row in image:
        for pixel in row:
            if pixel == target:
                count += 1
    return count

def enhance(image, enhancement, times):
    pad = '.'
    for _ in range(times):
        old_h = len(image)
        old_w = len(image[0])
        new_h = old_h + 2
        new_w = old_w + 2
        new_image = [[pad for _ in range(new_w)] for _ in range(new_h)]
        for y in range(-1, old_h + 1):
            for x in range(-1, old_w + 1):
                index = 0
                for ny in range(y - 1, y + 2):
                    for nx in range(x - 1, x + 2):
                        if ny >= 0 and ny < old_h and nx >= 0 and nx < old_w:
                            pixel = image[ny][nx]
                        else:
                            pixel = pad
                        index <<= 1
                        if pixel == '#':
                            index |= 1
                new_image[1 + y][1 + x] = enhancement[index]
        pad_index = 0b111111111 if pad == '#' else 0
        pad = enhancement[pad_index]
        image = new_image
    return image

print('Part 1:', count(enhance(image, enhancement, 2), '#'))
print('Part 2:', count(enhance(image, enhancement, 50), '#'))


import subprocess
import os
from unittest import mock
from unittest.mock import patch, Mock
import pytest
import logging
import sys

from src.build_java import build_java

def test_update_path_for_jdk():
    original_path = os.environ['PATH']
    build_java.update_path_for_jdk('java8')
    expected_path = '/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin:' + original_path
    assert os.environ['PATH'] == expected_path

def test_build_with_maven(mocker):
    mocker.patch('subprocess.run', return_value=subprocess.CompletedProcess(args=[], returncode=0))
    mocker.patch('sys.argv', ['script.py', 'java11', 'clean install', ''])

    build_java.main()

    subprocess.run.assert_called_once_with(['mvn', 'clean', 'install', '-DskipFormModel=false', '-DskipTests', 'clean', 'install'])

def test_build_with_custom_command(mocker):
    mocker.patch('subprocess.run', return_value=subprocess.CompletedProcess(args=[], returncode=0))
    mocker.patch('sys.argv', ['script.py', 'java11', '', 'echo "Custom command"'])

    build_java.main()

    subprocess.run.assert_called_once_with('echo "Custom command"', shell=True)

def test_allow_sh_permissions():
    with patch('subprocess.run') as mock_run:
        build_java.allow_sh_permissions("script.sh")

        mock_run.assert_called_once_with(['chmod', '+x', 'script.sh'], check=True)

    with patch('subprocess.run', side_effect=subprocess.CalledProcessError(1, ['cmd'])):
        with pytest.raises(SystemExit) as e:
           build_java.allow_sh_permissions("script.sh")

        assert e.type == SystemExit
        assert e.value.code == 1


from typing import List

# lapsolver is quite hard to install on Windows. Fortunately, scipy offers a similar, albeit slower, function.
try:
    from lapsolver import solve_dense as solver
except ImportError:
    from scipy.optimize import linear_sum_assignment as solver

from authentication.models import User
from repartitions.models import (
    Proposition,
    Wish,
    Category,
    Group,
    Campaign,
    UserCampaign,
)

import numpy as np

"""
The repartition algorithm is based on Munkres algorithm, or "Hungarian" algorithm, which, given a cost matrix,
returns a pairing between lines and columns that minimises the cost.

The implementation we use here is lapsolver's
https://github.com/cheind/py-lapsolver

However it does more than simply calling Munkres algorithm because we have two additional constraints :
- we have  "categories" of users ie subsets that have to be mixed inside of each group. For instance if we have 10 users
"A" and 10 users "B" to split into two groups we want to have twice "5 A  + 5 B" instead of one group of all As and one
group of all Bs
- we want groups to be "even" ie we don't want to be more than 1 person of difference between two groups (ex : one group
of 15 people and another of 5 people) nor inside of a group between categories (ex: 2A and 8B in one group and 8A and 2B
 in the other)

To abide by theses constraints, the algorithm is split in 2 stages:
(1) make an even repartition for a category
    determine the number of place necessary for each group
        - if there are n people and m groups, we'll have k places in each group were (k-1) * m < n <= k * m

    create the cost matrix
        - one line by user
        - one column per place in group, each column corresponding to one group is identical
        - example : 3 users, and 2 groups => k = 4
            (1, 1) (1, 1) (1, 2) (1, 2)
            (2, 1) (2, 1) (2, 2) (2, 2)
            (3, 1) (3, 1) (3, 2) (3, 2)

    run hungarian algorithm

    if there are too many people in one or several group, add a "penalty" to the group which has the most people. This
    is as if one place was removed in this group so on the next iteration it'll have one less person

    rerun the algorithm as long as the repartition isn't even

(2)  make the repartition even among categories
    repeat step (1) for each category but adding a penalty on each run corresponding to how many people each group has
    more in cumulated (either 0 or 1)

"""

def generate_line(
    uc: UserCampaign, propositions: List[Proposition], places: List[int]
) -> List[float]:
    """given a user's choices, a list of proposition and the number of places for each proposition, returns the
    corresponding line of the cost matrix"""

    if any(map(lambda x: x < 0, places)):
        raise ValueError("places {} is not valid", places)

    wishes = Wish.objects.filter(user_campaign=uc).all()
    map_wishes = {w.proposition.id: w for w in wishes}

    total_number_of_places = 0
    for n in places:
        total_number_of_places += n

    res = [0] * total_number_of_places
    i = 0
    for j, proposition in enumerate(propositions):
        for k in range(places[j]):
            if proposition.id in map_wishes:
                res[i] = map_wishes.get(proposition.id).rank
            else:
                res[i] = 0
            i += 1

    return res

def get_project_index(index, places):
    """Given an index and the number of places in each proposition, returns the index of the proposition in the
    array given to `generate_line`"""
    s = 0
    for i in range(len(places)):
        if s <= index < s + places[i]:
            return i
        s += places[i]

    if index == s:
        return len(places) - 1

    raise ValueError("index is too big considering the number of places")

# noinspection PyTypeChecker
def make_repartition_for_category(
    category: Category, propositions: List[Proposition], already_used: List[int]
) -> List[List[UserCampaign]]:
    raw_users_campaign = category.users_campaign.all()

    if len(raw_users_campaign) == 0:
        raise ValueError("No users in given group")
    user_campaigns, fixed_user_campaigns = [], []
    for uc in raw_users_campaign:
        if not uc.fixed_to:
            user_campaigns.append(uc)
        else:
            fixed_user_campaigns.append(uc)

    repartition_cardinal_diff = 2
    penalty = [0] * len(already_used)
    while repartition_cardinal_diff > 1:
        repartition = [[] for _ in propositions]
        proposition_id_to_index = {
            proposition.id: i for i, proposition in enumerate(propositions)
        }

        if len(fixed_user_campaigns) > 0:
            for uc in fixed_user_campaigns:
                repartition[proposition_id_to_index[uc.fixed_to.id]].append(uc)

        if len(user_campaigns) > 0:
            places = []
            s = 0  # Nombre de places allouées pour le groupe
            for i, proposition in enumerate(propositions):
                n = -already_used[i]
                n -= len(repartition)
                n -= penalty[i]
                s += n
                places.append(n)

            while s < len(user_campaigns):
                for i in range(len(places)):
                    s += 1
                    places[i] += 1

            cost_matrix = []
            for uc in user_campaigns:
                cost_matrix.append(generate_line(uc, propositions, places))

            # make the matrix square
            while len(cost_matrix) < len(cost_matrix[0]):
                cost_matrix.append([0] * len(cost_matrix[0]))

            links = solver(cost_matrix)

            for user_index, place_index in zip(links[0], links[1]):
                if user_index >= len(user_campaigns):
                    break

                project_index = get_project_index(place_index, places)
                uc = user_campaigns[user_index]
                repartition[project_index].append(uc)

            repartition_size = list(map(len, repartition))
            repartition_cardinal_diff = max(repartition_size) - min(repartition_size)

            penalty[np.argmax(repartition_size)] += 1
        else:
            repartition_cardinal_diff = (
                0  # there is nothing to do, avoid being stuck in an infinite loop
            )

    return repartition

def make_reparitition_proxy(
    category: Category, propositions: List[Proposition], already_used: List[int]
) -> List[List[User]]:
    """makes repartition for one category and make sure it's even"""
    group_cardinal_diff = 2
    groups = []

    already_used = np.array(already_used)
    penalty = np.array([0] * len(already_used))
    while group_cardinal_diff > 1:
        used_places = already_used + penalty
        groups = make_repartition_for_category(category, propositions, used_places)

        rep = [0] * len(used_places)
        for i in range(len(propositions)):
            rep[i] = len(groups[i])
        group_cardinal = np.array(rep) + already_used
        penalty[np.argmax(group_cardinal)] += 1
        group_cardinal_diff = max(group_cardinal) - min(group_cardinal)

    return groups

def make_reparition(campaign: Campaign) -> List[Group]:
    propositions = list(campaign.propositions.all())
    categories = list(campaign.categories.all())

    already_used = [0] * len(propositions)
    store = {}

    for i in range(len(categories)):
        category = categories[i]

        if category.users_campaign.count() == 0:
            continue

        # Check the repartition is feasible
        user_campaigns = category.users_campaign.all()
        fixed_to = {}
        for uc in user_campaigns:
            if uc.fixed_to is not None:
                fixed_to[uc.fixed_to.id] = fixed_to.get(uc.fixed_to.id, 0) + 1

        # if it's not possible for the repartition to compensate the inequalities created by fixity
        if len(fixed_to) > 0 and (max(fixed_to.values()) - 1) * len(propositions) > len(
            user_campaigns
        ):
            raise RuntimeError(
                "Impossible to find an even repartition, {} out of {} users fixed to {}".format(
                    max(fixed_to.values()),
                    category.users_campaign.count(),
                    np.argmax(fixed_to),
                )
            )

        # dispatch users
        groups = make_reparitition_proxy(category, propositions, already_used)

        for j, proposition in enumerate(propositions):
            store[proposition.id] = store.get(proposition.id, []) + groups[j]
            already_used[j] += len(groups[j])

    groups = []
    for proposition_id, users in store.items():

        proposition = Proposition.objects.get(pk=proposition_id)
        group = Group(proposition=proposition, campaign=campaign)
        group.save()
        group.users.set(users)
        groups.append(group)

    return groups


import tensorflow as tf
from tensorflow.python.framework import ops
import sys
import os
import numpy as np
from hf.core import box_8c_encoder

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
cropping_module = tf.load_op_library(os.path.join(BASE_DIR, "tf_cropping_so.so"))

def pc_crop_and_sample(pts, fts, intensities, mask, boxes, box_ind, resize):
    """
    input:
        pts:    (B, P, 3), [x, y, z],   float32
        fts:    (B, P, C), [c1,c2,...]  float32
        intensities: (B, P, 1), [i] float32
        mask:   (B, P) point mask label, bool
        boxes:  (N, 3, 8), 8 corners (x,y,z), float32
        box_ind:(N)        [b]  which batch that box belongs to, int32
        resize = R, int32
    output:
        crop_pts:   (N, R, 3)   float32
        crop_fts:   (N, R, C)   float32
        crop_intensity: (N, R, 1) float32
        crop_mask:  (N, R)      bool
        crop_ind:   (N, R)      int32,  which point to crop, complementary to box_ind
        non_empty_box_mask:  (N)         bool
    """
    return cropping_module.pc_crop_and_sample(
        pts, fts, intensities, mask, boxes, box_ind, resize
    )

ops.NoGradient("PcCropAndSample")
"""
@ops.RegisterGradient("PcCropAndSample")
def _PcCropAndSampleGrad(op, grad_crop_pts, grad_crop_fts, grad_crop_mask, grad_crop_ind, grad_non_empty_box):
    # assume grad_crop_ind == None
    pts = op.inputs[0]
    fts = op.inputs[1]
    boxes = op.inputs[3]
    box_ind = op.inputs[4]

    crop_ind = op.outputs[3]

    #grad_pts = cropping_module.pc_crop_and_sample_grad_pts(pts, box_ind, crop_ind, grad_crop_pts)
    grad_fts = cropping_module.pc_crop_and_sample_grad_fts(fts, box_ind, crop_ind, grad_crop_fts)
    #grad_boxes = cropping_module.pc_crop_and_sample_grad_boxes(grad_crop_pts, grad_crop_fts)
    return [None, grad_fts, None, None, None]
"""

if __name__ == "__main__":

    batch_size = 1
    pts = np.asarray([[[1.0, 0, 0.1], [-0.3, -0.5, -0.3]]], dtype=np.float32)
    fts = np.random.rand(batch_size, 2, 1).astype("float32")
    intensities = np.arange(batch_size * 2).reshape(batch_size, 2, 1).astype("float32")
    mask = np.random.rand(batch_size, 2).astype("bool")
    boxes_3d = np.asarray([0, 0, 0, 1, 1, 1, 3.14 / 4], dtype=np.float32)
    boxes_8co = box_8c_encoder.np_box_3d_to_box_8co(
        boxes_3d.reshape((-1, 7))
    ).transpose((0, 2, 1))

    # np.random.seed(100)
    box_ind = np.asarray([0]).astype("int32")

    pts = tf.constant(pts)
    fts = tf.constant(fts)
    intensities = tf.constant(intensities)
    mask = tf.constant(mask)
    boxes_8co = tf.constant(boxes_8co)
    box_ind = tf.constant(box_ind)
    crop_pts, crop_fts, crop_intensities, crop_mask, crop_ind, non_empty_box_mask = pc_crop_and_sample(
        pts, fts, intensities, mask, boxes_8co, box_ind, 1
    )

    non_empty_fts = tf.boolean_mask(crop_fts, non_empty_box_mask)

    with tf.Session() as sess:
        crop_pts, crop_fts, crop_intensities, crop_mask, crop_ind, non_empty_box_mask, non_empty_fts = sess.run(
            [
                crop_pts,
                crop_fts,
                crop_intensities,
                crop_mask,
                crop_ind,
                non_empty_box_mask,
                non_empty_fts,
            ]
        )

    print("crop_pts:")
    print(crop_pts)
    print("crop_fts:")
    print(crop_fts)
    print("crop_intensities:")
    print(crop_intensities)
    print("crop_mask:")
    print(crop_mask)
    print("crop_ind:")
    print(crop_ind)
    print("non_empty_box:")
    print(non_empty_box_mask)
    print("non_empty_fts:")
    print(non_empty_fts)


import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import os
import time
from datetime import date, datetime
from datetime import timedelta
def wait_for_downloads(path):
    print("Waiting for downloads", end="")
    while any([filename.endswith(".crdownload") for filename in
               os.listdir(path)]):
        time.sleep(1)
        print(".", end="")
    print("done!")

downloadPath = os.path.abspath('./../database/sp500')
os.makedirs(downloadPath, mode=0o777, exist_ok=True)  # create dir if it not exist

options = webdriver.ChromeOptions()

options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument("--allow-running-insecure-content")
options.add_argument("--window-size=1920,1080")
options.add_argument('--no-sandbox')
options.add_argument(f'user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36')
options.add_argument("--remote-debugging-address=0.0.0.0")
options.add_argument("--remote-debugging-port=9222")
options.add_argument("--disable-features=VizDisplayCompositor")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-infobars")
options.add_argument("--disable-browser-side-navigation")
options.add_experimental_option('prefs', {
"download.default_directory": downloadPath, #Change default directory for downloads
"download.prompt_for_download": False, #To auto download the file
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True #It will not show PDF directly in chrome
})

print("Init chrome driver ...")
driver = webdriver.Chrome(service = Service(executable_path= '/usr/local/bin/chromedriver'),options=options)
driver.implicitly_wait(10)
print("chrome driver ok!")

url = "https://finance.yahoo.com/quote/%5EGSPC/history?p=%5EGSPC"

driver.get(url)

tabela = driver.find_element(By.CSS_SELECTOR, "#Col1-1-HistoricalDataTable-Proxy > section > div.Pb\(10px\).Ovx\(a\).W\(100\%\) > table")
df = pd.read_html(tabela.get_attribute("outerHTML"))[0]
df = df[:-1]
df['Date'] = [datetime.strptime(str(d), "%b %d, %Y") for d in df['Date']]

if datetime.now().weekday() == 0:#segunda
    data = datetime.strftime((datetime.now() - timedelta(days=3)).date(), "%Y-%m-%d")
    dataAnterior = datetime.strftime((datetime.now() - timedelta(days=4)).date(), "%Y-%m-%d")
elif datetime.now().weekday() == 1:#terça
    data = datetime.strftime((datetime.now() - timedelta(days=1)).date(), "%Y-%m-%d")
    dataAnterior = datetime.strftime((datetime.now() - timedelta(days=4)).date(), "%Y-%m-%d")
else:
    data = datetime.strftime((datetime.now() - timedelta(days=1)).date(), "%Y-%m-%d")
    dataAnterior = datetime.strftime((datetime.now() - timedelta(days=2)).date(), "%Y-%m-%d")

valorAntigo = df.loc[df['Date'] == dataAnterior, "Adj Close**"].values[0]
valor = df.loc[df['Date'] == data, "Adj Close**"].values[0]
retorno = [float(valor)/float(valorAntigo) - 1]
data = [data]
d = {"Data":data, "Valor":retorno}
finalDf = pd.DataFrame(data=d)

finalDf.to_csv(os.path.join(downloadPath, "sp500.csv"),  sep = ",", index=False)


import csv
import os
from extract_file.extract_CSV import extract_csv_x

dir = r'/Users/peterchukwu/Desktop/CETM50'

def extract_csv_headers():
    try:
        directory = dir
        data = extract_csv_x()
        HEADER_S = next(data)
        for file_name in os.listdir(directory):
            if file_name.endswith(".csv"):
                f = os.path.join(directory, file_name)
                if os.path.isfile(f):
                    csv_file = open(f)
                    csvreader = csv.DictReader(csv_file, HEADER_S)
                    return csvreader
    except Exception as error:
        print("Data error " + str(error))


import logging
from conftest import Log_path

class LogData:
    def getLogger(self):
        logger = logging.getLogger(__name__)
        logsone = logging.FileHandler(Log_path+"/logfile.log")
        formatter = logging.Formatter("%(asctime)s :%(levelname)s :%(name)s :%(message)s")
        logsone.setFormatter(formatter)

        logger.addHandler(logsone)

        logger.setLevel(logging.DEBUG)
        return logger


import os
import json
import io
from wp import wp_database, wp_post_query
from mailing import mailer

WP_DB_CONFIG_FILE_PATH_VAR_KEY = "WP_DB_CONFIG_FILE_PATH"
DEFAULT_WP_DB_CONFIG_FILE_PATH = "resource/wp_db_config.json"

MAIL_CONFIG_FILE_PATH_VAR_KEY = "MAIL_CONFIG_FILE_PATH"
DEFAULT_MAIL_CONFIG_FILE_PATH = "resource/mail_config.json"

JINJA2_MAIL_TEMPLATE_FILE_PATH_VAR_KEY ='JINJA2_MAIL_TEMPLATE_FILE_PATH'
DEFAULT_JINJA2_MAIL_TEMPLATE_FILE_PATH = 'resource/mail_template.jinja2'

# WordPress
def get_wp_db_config():
    wpDbConfigFilePath = get_wp_db_config_file_path()
    wpDbConfig = get_json_data(wpDbConfigFilePath)
    wp_database.validate_wp_db_config(wpDbConfig)
    return wpDbConfig

def get_wp_db_config_file_path():
    return get_env_config(WP_DB_CONFIG_FILE_PATH_VAR_KEY, DEFAULT_WP_DB_CONFIG_FILE_PATH)

# Mail
def get_mail_config():
    mailConfigFilePath = get_mail_config_file_path()
    mailConfig = get_json_data(mailConfigFilePath)
    mailer.validate_mail_config(mailConfig)
    return mailConfig

def get_mail_config_file_path():
    return get_env_config(MAIL_CONFIG_FILE_PATH_VAR_KEY, DEFAULT_MAIL_CONFIG_FILE_PATH)

# Jinja 2 Template
def get_jinja2_mail_template_string():
    filePath = get_jinja2_mail_template_file_path()
    with io.open(filePath, "r") as jinja2_file:
        return jinja2_file.read()

def get_jinja2_mail_template_file_path():
    return get_env_config(JINJA2_MAIL_TEMPLATE_FILE_PATH_VAR_KEY, DEFAULT_JINJA2_MAIL_TEMPLATE_FILE_PATH)

# Config
def get_env_config(key, default):
    return default if not os.environ.get(key) else os.environ.get(key)

def get_json_data(filename):
    with io.open(filename, "r", encoding="utf-8") as json_file:
        data = json.load(json_file)
        return data

# Main
def main():
    wpDbConfig  = get_wp_db_config()
    mailConfig = get_mail_config()
    jinja2MailTemplateString = get_jinja2_mail_template_string()

    db = wp_database.get_wp_db(wpDbConfig)

    queryResult = wp_post_query.get_pending_posts(
        db=db,
        targetCategories=wpDbConfig['targetCategories'],
        targetPostStatuses=wpDbConfig['targetPostStatuses']
    )

    print(queryResult)

    mailer.send(mailConfig, queryResult, jinja2MailTemplateString)

if __name__ == '__main__':
    main()


"""

create table product (name varchar(20) primary key, price float, qty int)
drop table product # to delete the whole table

insert into product values ('Prod1', 34.56, 23)
select * from product
select * from product where qty > 20
delete from product where qty=0
update product set price=price*1.25 where name='Prod3'

commit # validate a transaction
rollback # invalidate the transaction (the insert,delete,update done previously)

sql alchemy

"""

import sqlite3

try:
    # Step 1: connection
    conn=sqlite3.connect(r"epfl.db")
    # Step 2: create a cursor object
    cursor=conn.cursor()
    # Step 3: using the cursor, execute SQL statements:
    cursor.execute("insert into product values ('prod600', 22.3, 34)")
    print(f"{cursor.rowcount} rows were returned")
    cursor.execute("commit")

    cursor.execute("select * from product")

    while True:
        row = cursor.fetchone()
        if row == None:
            break
#         print("{}, {}, {}".format(row[0], row[1], row[2]))
        print(f"{row[0]}, {row[1]}, {row[2]}")
#     res=cursor.fetchall()
#     print(res)
    print("{} rows were returned".format(cursor.rowcount))

    cursor.close()
    conn.close()
except Exception as ex:
    print(ex)


from PPlay.gameimage import *
from PPlay.gameobject import *
from PPlay.sprite import *
import pygame
import pickle

class Screen:
    def __init__(self, screen, platforms, sprite, keyboard, slime, mouse, font_screen):
        self.font_screen = font_screen
        self.window = screen
        self.platforms = platforms
        self.ground_x = platforms.get_pos_ground_x()
        self.platforms_x = platforms.get_pos_platforms_x()
        self.sprite = sprite.get_img()
        self.keyboard = keyboard
        self.background1 = platforms.get_backgrounds()[0]
        self.background2 = platforms.get_backgrounds()[1]
        self.slime = slime
        self.mouse = mouse
        # Menu BG
        self.bg_menu = GameImage("Sprites/menu/bg_menu.png")
        self.bg_menu_about = GameImage("Sprites/menu/bg_menu_about.png")
        self.bg_game_over = GameImage("Sprites/menu/bg_game_over.png")
        # Score BG
        self.bg_score = GameImage("Sprites/menu/bg_score.png")
        # Button Start
        self.bt_start = Button("Sprites/menu/bt_start_sheet.png", 437, 200)
        # Button Score
        self.bt_score = Button("Sprites/menu/bt_scores_sheet.png", 437, 300)
        # Button About
        self.bt_about = Button("Sprites/menu/bt_sobre_sheet.png", 437, 400)
        # Mode Selectors Buttons
        self.bt_mode_1 = Button("Sprites/menu/bt_mode1_sheet.png", 562, 200)
        self.bt_mode_2 = Button("Sprites/menu/bt_mode2_sheet.png", 312, 200)
        # Button Back
        self.bt_back = Button("Sprites/menu/bt_back_sheet.png", 100, 400)
        # Button Exit
        self.bt_exit = Button("Sprites/menu/bt_exit_sheet.png", 870, 400)
        self.menu_state = 0
        self.game_state = 0

    def move_camera(self, speed):
        # Moving Platforms
        for j in range(len(self.platforms_x)):
            for i in range(len(self.platforms_x[j])):
                self.platforms_x[j][i] += speed
                self.update_pos()

        # Move Mob
        for i in range(len(self.slime.slimes)):
            self.slime.slimes_x[i] += speed

        # Moving Background
        self.background1.x += (speed / 20)
        self.background2.x += (speed / 20)

    def update_pos(self):
        self.platforms.set_pos_platforms_x(self.platforms_x)
        self.slime.update_limits(self.platforms_x)

    def menu(self, player, teclado, menu_state = 0):
        self.menu_state = menu_state
        if self.menu_state == 0:
            self.bg_menu.draw()
            if self.bt_start.draw(self.mouse):
                self.game_state = 1
            if self.bt_score.draw(self.mouse):
                self.menu_state = 1
            if self.bt_about.draw(self.mouse):
                self.menu_state = 2
            if self.bt_exit.draw(self.mouse):
                exit()
            return 0
        elif self.menu_state == 1:
            self.bg_score.draw()
            pygame.font.init()
            myfont = pygame.font.SysFont("Supersonic Rocketship", 40)
            label = myfont.render(self.ranking(0, 0, 'r'), 1, (0, 0, 0))
            self.font_screen.blit(label, (430, 200))
            if self.bt_exit.draw(self.mouse):
                exit()
            if self.bt_back.draw(self.mouse):
                self.menu_state = 0
        elif self.menu_state == 2:
            self.bg_menu_about.draw()
            if self.bt_exit.draw(self.mouse):
                exit()
            if self.bt_back.draw(self.mouse):
                self.menu_state = 0
        elif self.menu_state == 3:
            self.bg_game_over.draw()
            if self.bt_exit.draw(self.mouse):
                exit()
            if self.bt_back.draw(self.mouse):
                self.menu_state = 0
            if teclado.key_pressed("SPACE"):
                self.menu_state = 0
                return 0

    def ranking(self,jogador, pontos, modo):
        # Escrevendo dentro do arquivo
        if modo == 'w':
            aux = 0
            while aux != 1:
                try:
                    # Carrega o arquivo 'Score.txt' que ja contem uma matriz, na nova matriz 'score
                    score = pickle.load(open('Score.txt', 'rb'))
                    score.append([jogador, pontos])
                    # Ordena a matriz do maior score para o menor
                    for i in range(len(score)):
                        for j in range(len(score)):
                            if score[i][1] > score[j][1]:
                                pos = score[i]
                                score[i] = score[j]
                                score[j] = pos
                    # Sobrescreve o arquivo 'Store.txt' com a matriz 'Score'...
                    arqui = pickle.dump(score, open('Score.txt', 'wb'))
                    aux += 1
                # Caso o arquivo 'Score.txt' esteja vazio.
                except EOFError:
                    score = []
                    arqui = pickle.dump(score, open('Score.txt', 'wb'))
                # Caso arquivo 'Score.txt' não exista...
                except FileNotFoundError:
                    score = []
                    arqui = pickle.dump(score, open('Score.txt', 'wb'))
        if modo == 'r':
            matriz = pickle.load(open('Score.txt', 'rb'))
            return str(matriz)

    def score(self, nome, moedas, roxinhos, mortes, w):
        pontuacao = 1500
        pontuacao = pontuacao + (moedas * 50) + (roxinhos * 35) - (mortes * 70)
        return self.ranking(self, nome, pontuacao, w)
    # Append na matriz score
    # score('arthur',65,34,0,'w')
    # Print na matriz score
    # ranking(0, 0, 'r')

class Button(Screen):
    def __init__(self, button_sheet, x, y):
        super(Button, self)
        self.bt = Sprite(button_sheet, 2)
        self.bt.set_position(x, y)
        self.bt_rect = GameObject()
        self.bt_rect.width = self.bt.width
        self.bt_rect.height = self.bt.height
        self.bt_rect.x = x
        self.bt_rect.y = y

    def draw(self, mouse):
        if mouse.is_over_object(self.bt_rect):
            self.bt.set_curr_frame(1)
            if mouse.is_button_pressed(1):
                return True
        else:
            self.bt.set_curr_frame(0)
        self.bt.draw()


from urllib import request
from urllib.parse import urljoin
from bs4 import BeautifulSoup

from dotFoods.celery import app
from dotFoods import settings

from wikiboto.utils import \
    url_name_of, \
    url_name_of_cat,\
    LINK_FILTERS, \
    bs_preprocess, \
    can_access

from wikipage.models import \
    WikiList, WikiPage, WikiCategory

from wikiboto.models import BotoFinished

from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)

@app.task(rate_limit=settings.RATE_LIMIT,
          time_limit=settings.TIME_LIMIT)
def crawl_page(url, parent=None, depth=0):
    # check if already crawled
    if BotoFinished.objects.filter(url=url).exists():
        if parent:
            try:
                page = WikiPage.objects.get(
                    url_name=url_name_of(url))
                wl, exist = WikiList.objects.get_or_create(
                    url_name=parent)
                page.lists.add(wl)
                page.save()
            except:
                logger.warning('list not added')
        return
    else:
        BotoFinished(url=url).save()

    if can_access(url):
        logger.info('Starting to crawl {0}'.format(url))
        req = request.Request(
            url, data=None,
            headers={
                'User-Agent': settings.USER_AGENT,
            })
        f = request.urlopen(req)
        process_page(url, f.read(), parent, depth)

    else:
        logger.warning('Banned to access {0}'.format(url))

@app.task
def process_page(url, raw, parent, depth):
    logger.info('Processing page {0}'.format(url))

    page = BeautifulSoup(raw, "html.parser")
    title = str(page.find(id='firstHeading').string)
    content = str(page.find(id='bodyContent'))

    if title.lower().startswith('list of') or \
       title.lower().startswith('lists of'):
        title = title.split('of ')[1]
        process_list(url, title, content, depth)

    else:
        process_article(url, title, content, parent)

@app.task
def process_list(url, title, raw_content, depth):
    if depth <= 0:
        logger.info('Reached depth limit: {0}'.format(title))
        return

    logger.info('Processing list {0}'.format(title))

    content = BeautifulSoup(
        bs_preprocess(raw_content), 'html.parser')

    url_name = url_name_of(url)
    WikiList.objects.get_or_create(
        url_name=url_name,
        defaults={'title': title})

    # remove noisy links
    link_list = [a for a in content.find_all('a')]
    for f in LINK_FILTERS:
        link_list = filter(f, link_list)

    # crawl all links of the list
    for link in link_list:
        abs_url = urljoin(url, link['href'])
        crawl_page.delay(abs_url, url_name, depth - 1)

@app.task
def process_article(url, title, raw_content, parent=None):
    logger.info('Processing article {0}'.format(title))

    content = BeautifulSoup(
        bs_preprocess(raw_content), 'html.parser')

    body = BeautifulSoup(raw_content, 'html.parser')
    for edits in body.find_all(class_='mw-editsection'):
        edits.extract()
    body.find(id='jump-to-nav').extract()
    body = BeautifulSoup(str(body), 'html.parser')

    page, exist = WikiPage.objects.get_or_create(
        url_name=url_name_of(url),
        defaults={
            'title': title,
            'origin': url})
    page.body = str(body)
    page.save()

    # add lists
    if parent:
        wl, exist = WikiList.objects.get_or_create(
            url_name=parent)
        page.lists.add(wl)

    # add categories
    for cat in content.find(id='mw-normal-catlinks')\
            .find_all('li'):
        url_name = url_name_of_cat(cat.find('a')['href'])
        if not url_name:
            continue
        wc, exist = WikiCategory.objects.get_or_create(
            url_name=url_name,
            defaults={'title': str(cat.text)})
        page.categories.add(wc)


"""
Run a nongaussian Kalman Filter twin experiment with Lorenz-63
"""

# Load modules
import numpy as np
import pickle
import mod_KalmanDA as da
import LorenzModels as LM
import matplotlib.pyplot as plt
import tikzplotlib as tpl

# Options for DA run
n_wind = 25                                # Number of DA windows for each run
obs_vars = 'xyz'                            # Observed variables, either 'xy', or 'xyz'
ml_method = 'array'                         # Nongaussian decision function, either
                                            #   'array',    selection based on observations before DA run
                                            #   'function', selection from decision function every DA window
seed = 3405                                 # Seed for random number generator, can be set to None
SV_init = [-5.0,-6.0,22.0]                  # Initial values of the Lorenz-63 run

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

def one_run():
    global n_wind
    global period_obs
    global obs_vars
    global var_obs
    global ml_method
    global ml_file
    global seed
    global SV_init

    # Load the machine learning model
    with open(ml_file,'rb') as f:
        clf = pickle.load(f)
        scaler = pickle.load(f)
        info = pickle.load(f)
        p = info['p']               # Parameters of L63, typically (10,28,8/3)
        dt = info['dt']             # Time step of L63
    rng = np.random.default_rng(seed)
    SV_init += rng.standard_normal(3)

    # Nature run
    t_max = n_wind*period_obs*dt        # End time of the Lorenz-63 run
    t = np.arange(0.0, t_max, dt)  # Evaluation time
    SV = LM.sol_L63(t, SV_init, p)
    xi_SV = np.nanmax(SV[2,:])+5.0      # Parameter of the reverse lognormal distribution

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

    # Create observation operator
    n_SV = 3
    if obs_vars == 'xyz':
        n_obs = 3
        obs_h = np.eye(n_SV)
    if obs_vars == 'xy':
        n_obs = 2
        obs_h = np.zeros((n_obs,n_SV))
        obs_h[0,0] = 1.0
        obs_h[1,1] = 1.0

    # Predict distribution from which to sample z-variable
    if obs_vars == 'xyz':
        # Predict distribution from nature run
        X_data = scaler.transform(np.transpose(SV[:2,:]))
        z_pred = clf.predict(X_data)
        # Define observation times when lognormal or reverse lognormal noise should be added
        ln_vars_ML = []
        rl_vars_ML = []
        for ii in range(t.size):
            if ii % period_obs == 0:
                if z_pred[ii] > 0.5: # Lognormal distribution predicted
                    ln_vars_ML.append([2])
                    rl_vars_ML.append([])
                elif z_pred[ii] < -0.5: # Reverse lognormal distribution predicted
                    ln_vars_ML.append([])
                    rl_vars_ML.append([2])
                else: # Gaussian distribution predicted
                    ln_vars_ML.append([])
                    rl_vars_ML.append([])
    if obs_vars == 'xy':
        # If z is not observed, ML model not necessary
        ln_vars_ML = []
        rl_vars_ML = []

    # Generate observations
    t_obs, y, R = da.gen_obs(t, SV, period_obs, obs_h, var_obs, \
        ln_vars = ln_vars_ML, rl_vars = rl_vars_ML, xi_obs = xi_SV, seed = seed, sample='mode')

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

    # DA initial guess
    init_guess = SV_init + rng.standard_normal(3)

    # Model function, of the form x = model(t,x_0)
    def model_l63(t, x0):
        y = LM.sol_L63(t, x0, p)
        return y

    # Decision function for lognormal state variables (for a single prediction)
    def f_ln_vars(SV):
        X = scaler.transform(SV[:2].reshape(1,-1))
        z_pred = clf.predict(X)
        if z_pred > 0.5:
            return [2]
        else:
            return []
    # Decision function for reverse-lognormal state variables (for a single prediction)
    def f_rl_vars(SV):
        X = scaler.transform(SV[:2].reshape(1,-1))
        z_pred = clf.predict(X)
        if z_pred < -0.5:
            return [2]
        else:
            return []

    # Create decision functions/arrays
    if ml_method == 'array':
        ln_vars = []
        rl_vars = []
        for ii in range(t_obs.size):
            ln_vars.append(f_ln_vars(y[:2,ii]))
            rl_vars.append(f_rl_vars(y[:2,ii]))
        ln_B0 = ln_vars[0]
        rl_B0 = rl_vars[0]
    if ml_method == 'function':
        ln_vars = f_ln_vars
        rl_vars = f_rl_vars
        ln_B0 = f_ln_vars(init_guess)
        rl_B0 = f_rl_vars(init_guess)

    # Store different decision functions for different (non)gaussian methods
    ln_vars_SV_OBJ = np.array([ \
        [],      # Gaussian for all t
        [2],     # Lognormal for all t
        [],      # Reverse Lognormal for all t
        ln_vars, # Gaussian - Lognormal ML
        [],      # Gaussian - Reverse Lognormal ML
        ln_vars, # Gaussian - Lognormal - Reverse Lognormal ML
        [],      # No DA (background run)
    ], dtype = object)
    rl_vars_SV_OBJ = np.array([ \
        [],      # Gaussian for all t
        [],      # Lognormal for all t
        [2],     # Reverse Lognormal for all t
        [],      # Gaussian - Lognormal ML
        rl_vars, # Gaussian - Reverse Lognormal ML
        rl_vars, # Gaussian - Lognormal - Reverse Lognormal ML
        [],      # No DA (background run)
    ], dtype = object)
    if obs_vars == 'xyz':
        ln_vars_obs_OBJ = ln_vars_SV_OBJ
        rl_vars_obs_OBJ = rl_vars_SV_OBJ
    elif obs_vars == 'xy':
        ln_vars_obs_OBJ = np.array([[],[],[],[],[],[],[-1.0]], dtype = object)
        rl_vars_obs_OBJ = np.array([[],[],[],[],[],[],[-1.0]], dtype = object)

    # Initialize analysis and background state variables
    n_meths = 7
    n_t_obs = t_obs.size           # number of observations
    n_t = n_t_obs * period_obs + 1 # number of total time steps
    X_A = np.empty((n_meths, n_SV, n_t_obs))
    X_B = np.empty((n_meths, n_SV, n_t))

    # Model error covariance matrix
    Q = np.array([ \
        [0.1491, 0.1505, 0.0007], \
        [0.1505, 0.9048, 0.0014], \
        [0.0007, 0.0014, 0.9180] \
    ])

    ln_vars_B_OBJ = np.array([ [], [2], [], ln_B0, [], ln_B0, []], dtype = object)
    rl_vars_B_OBJ = np.array([ [], [], [2], [], rl_B0, rl_B0, []], dtype = object)

    for iM in range(n_meths-1):
        # Create initial background error covariance matrix
        B_case = 'nmc'
        P_a = da.create_B_init(B_case, \
            SV_init = SV_init, \
            p = p, \
            seed = seed, \
            ln_vars=ln_vars_B_OBJ[iM], rl_vars=rl_vars_B_OBJ[iM])

        X_A[iM,:,:], X_B[iM,:,:], _, _ = da.kalman_filter( \
            init_guess, t_obs, period_obs, y, obs_h, P_a, R, Q, \
            model_l63, \
            ln_vars_SV = ln_vars_SV_OBJ[iM], ln_vars_obs = ln_vars_obs_OBJ[iM], \
            rl_vars_SV = rl_vars_SV_OBJ[iM], rl_vars_obs = rl_vars_obs_OBJ[iM], \
            xi_SV = xi_SV, xi_obs = xi_SV \
        )

    # Background run
    iM += 1
    dt_obs = t_obs[1] - t_obs[0] # Assuming evenly spaced observations
    t_true = np.linspace(t_obs[0],t_obs[-1] + dt_obs, n_t) # Assuming one prediction window
    X_B[iM,:,:] = model_l63(t_true, init_guess)
    X_A[iM,:,:] = X_B[iM,:,:-1:period_obs]

    MAE_A = np.empty(n_meths)
    MAE_B = np.empty(n_meths)
    for iM in range(n_meths):
        MAE_A[iM] = np.nanmean(np.abs((SV[:,::period_obs] - X_A[iM,:,:])))
        MAE_B[iM] = np.nanmean(np.abs((SV - X_B[iM,:,:-1])))

    return MAE_A, MAE_B, t_true, t_obs, X_A, X_B, y, SV, ln_vars_ML, rl_vars_ML, ln_vars, rl_vars

###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
###########################################################################################
meths = [ \
    'Gaussian ', \
    'Lognormal', \
    'Rev logn ', \
    'G-LogNorm', \
    'G-RevLog ', \
    'All mixed', \
    'noDA     ' \
]
n_meths = len(meths)
ml_file = './kNN_l63_w14_s1.0.pkl'    # Location of the trained ML model

period_obs = 40
var_obs = 3.0

MAE_A, MAE_B, t, t_obs, X_A, X_B, y, SV, ln_vars_ML, rl_vars_ML, ln_vars, rl_vars = one_run()

print("Finished p = "+str(period_obs)+", s = "+str(var_obs))
for iM in range(n_meths):
    print(meths[iM]+ \
        ': mean(r_a) = '+format(np.round(MAE_A[iM],3),".3f")+ \
        ', mean(r_b) = '+format(np.round(MAE_B[iM],3),".3f"))

y_gs = np.full(y.shape,np.nan)
y_ln = np.full(y.shape,np.nan)
y_rl = np.full(y.shape,np.nan)
for jj in range(t_obs.size):
    if ln_vars_ML[jj]:
        y_ln[:,jj] = y[:,jj]
    elif rl_vars_ML[jj]:
        y_rl[:,jj] = y[:,jj]
    else:
        y_gs[:,jj] = y[:,jj]
y_gs_DA = np.full(y.shape,np.nan)
y_ln_DA = np.full(y.shape,np.nan)
y_rl_DA = np.full(y.shape,np.nan)
for jj in range(t_obs.size):
    if ln_vars[jj]:
        y_ln_DA[:,jj] = y[:,jj]
    elif rl_vars[jj]:
        y_rl_DA[:,jj] = y[:,jj]
    else:
        y_gs_DA[:,jj] = y[:,jj]

# n_plot = n_meths-1
n_plot = 2
fig, ax = plt.subplots(n_plot,3,figsize=(15, 5))
vars = ['$x$', '$y$', '$z$']
meth_select = [0,5]
for iMeth in range(n_plot):
    for jj in range(3):
        # True state
        ax[iMeth,jj].plot(t[:-1],SV[jj,:],label = 'Nature')

        # Background state
        ax[iMeth,jj].plot(t[:-1],X_B[meth_select[iMeth],jj,:-1],'--',label = 'Background')

        # Observations
        if not (jj==2 and obs_vars == 'xy'):
            ax[iMeth,jj].plot(t_obs,y_gs[jj,:],'.',label = 'Gs Observations')
            ax[iMeth,jj].plot(t_obs,y_ln[jj,:],'.',label = 'LN Observations')
            ax[iMeth,jj].plot(t_obs,y_rl[jj,:],'.',label = 'RL Observations')
        if not (jj==2 and obs_vars == 'xy'):
            ax[iMeth,jj].scatter(t_obs,y_gs_DA[jj,:],s=50, facecolors='none',edgecolors='#2ca02c',label = 'Gs ML')
            ax[iMeth,jj].scatter(t_obs,y_ln_DA[jj,:],s=50, facecolors='none',edgecolors='#d62728',label = 'LN ML')
            ax[iMeth,jj].scatter(t_obs,y_rl_DA[jj,:],s=50, facecolors='none',edgecolors='#9467bd',label = 'RL ML')

        # # Analysis
        # ax[iMeth,jj].plot(t_obs,X_A[meth_select[iMeth],jj,:],'.',label = 'Analysis')

        ax[iMeth,jj].set_xlabel('$t$')
        ax[iMeth,jj].set_ylabel(vars[jj])
    ax[iMeth,0].set_title(meths[meth_select[iMeth]])
    ax[iMeth,0].set_ylim(-25,25)
    ax[iMeth,1].set_ylim(-30,30)
    ax[iMeth,2].set_ylim(0,50)
ax[0,0].legend()
# tpl.clean_figure()
# tpl.save("KF_ML_example.tex")
plt.show()

# Write to files
vars = ['x', 'y', 'z']
for var in range(3):
    with open("./values/"+vars[var]+"_true.dat",'w') as f:
        for ii in range(t[:-1].size):
            f.write(str(t[ii])+" "+str(SV[var,ii])+"\n")
    with open("./values/"+vars[var]+"_b_gs.dat",'w') as f:
        for ii in range(t[:-1].size):
            f.write(str(t[ii])+" "+str(X_B[0,var,ii])+"\n")
    with open("./values/"+vars[var]+"_b_ml.dat",'w') as f:
        for ii in range(t[:-1].size):
            f.write(str(t[ii])+" "+str(X_B[5,var,ii])+"\n")

    with open("./values/"+vars[var]+"_obs_gs.dat",'w') as f:
        for ii in range(1,t_obs.size):
            f.write(str(t_obs[ii])+" "+str(y_gs[var,ii])+"\n")
    with open("./values/"+vars[var]+"_obs_ln.dat",'w') as f:
        for ii in range(1,t_obs.size):
            f.write(str(t_obs[ii])+" "+str(y_ln[var,ii])+"\n")
    with open("./values/"+vars[var]+"_obs_rl.dat",'w') as f:
        for ii in range(1,t_obs.size):
            f.write(str(t_obs[ii])+" "+str(y_rl[var,ii])+"\n")

    with open("./values/"+vars[var]+"_mlp_gs.dat",'w') as f:
        for ii in range(1,t_obs.size):
            f.write(str(t_obs[ii])+" "+str(y_gs_DA[var,ii])+"\n")
    with open("./values/"+vars[var]+"_mlp_ln.dat",'w') as f:
        for ii in range(1,t_obs.size):
            f.write(str(t_obs[ii])+" "+str(y_ln_DA[var,ii])+"\n")
    with open("./values/"+vars[var]+"_mlp_rl.dat",'w') as f:
        for ii in range(1,t_obs.size):
            f.write(str(t_obs[ii])+" "+str(y_rl_DA[var,ii])+"\n")


def non_repeat(line: str) -> str:
    """
        the longest substring without repeating chars
    """
    if not line:
        return line

    line_uniques = []
    for i in range(len(line)):
        line_short = line[i:]
        line_new = ''
        for c in line_short:
            if c and c in line_new:
                line_uniques.append(line_new)
                line_new = c
            else:
                line_new += c
        line_uniques.append(line_new)

    return max(line_uniques,key=lambda x: len(x))

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert non_repeat('aaaaa') == 'a', "First"
    assert non_repeat('abdjwawk') == 'abdjw', "Second"
    assert non_repeat('abcabcffab') == 'abcf', "Third"
    print('"Run" is good. How is "Check"?')


# A simple number guessing game
# Could you make something cooler?
import random

# Make n a random number between 1 and 99
n = random.randint(1, 10)

# Ask user to guess the number
guess = int(input("Enter an integer from 1 to 10: "))

while True:
    if guess >= 1 or guess <= 10 :
        if guess < n or guess > n :
            print ("try again")
            guess = int(input("Enter an integer from 1 to 10: "))
        else:
            print ("you guessed it!")
            break


from db_generator import choose_variant_from_dict
import os

def store_res_in_party(cursor, account, max_time):
    cursor.execute("(select coalesce(max(party_id)+1,0) from party);")
    party_id = cursor.fetchall()[0][0]
    print("party_id = %d" % party_id)

    cursor.execute("select * from get_ordered_result();")
    for fname, sname, sum_score, spended_money in cursor.fetchall():
        remained_money = account - spended_money

        cursor.execute("insert into party values(%d, %d, %d, '%s','%s',%d,%d,%d)" % (
            party_id, account, max_time, fname, sname, sum_score, spended_money, remained_money))

    cursor.execute("update party set score=0 where remained_money < 0;")

def store_all_parties(cursor, file_name):
    abs_dir = os.path.abspath("../algorithms")
    abs_file = "%s/%s" % (abs_dir, file_name)
    os.system("touch %s" % abs_file)
    os.system("chmod 666 %s" % abs_file)

    cursor.execute("copy (select * from party order by party_id, score) to '%s/%s';" % (abs_dir, file_name))

def store_last_party(cursor,file_name):
    abs_dir = os.path.abspath("../algorithms")
    abs_file = "%s/%s" % (abs_dir, file_name)
    os.system("touch %s" % abs_file)
    os.system("chmod 666 %s" % abs_file)

    cursor.execute("copy (select * from party where party_id = (select max(party_id) from party) order by score desc) to '%s/%s';" % (abs_dir, file_name))

def store_party_in_file(cursor, file_name, p_id):
    abs_dir = os.path.abspath("../algorithms")
    abs_file = "%s/%s" % (abs_dir, file_name)
    os.system("touch %s" % abs_file)
    os.system("chmod 666 %s" % abs_file)

    cursor.execute("copy (select * from party where party_id = %d order by party_id, score) to '%s/%s';" % (
    p_id, abs_dir, file_name))


class Hero:
    def __init__(self,name,health,attachPower,armor):
        #public
        self.name = name
        self.health = health
        self.attachPower = attachPower
        self.armor = armor

        #protected
        self._tinggi = 15

        #private
        self.__exp = 10

    #cara untuk mendapatkan variable private, dengan trik membuat methode baru seperti dibawah ini getExp
    def getExp(self):
        print(self.__exp)

natalia = Hero("natalia",500,50,10)

print(natalia.name)
print(natalia._tinggi)
print(natalia.getExp())


import numpy as np
import matplotlib.pyplot as plt
from pylab import *
from matplotlib.widgets import Slider
import networkx as nx
########## Here we do some introductory stuff with percolation ###########
#update function
def update(val):
	GW = G.copy()
	for i,j in GW.edges():
		if np.random.uniform(0,1) > val:
			GW.remove_edge(i,j)
	wcc = [GW.subgraph(c).copy() for c in nx.connected_components(GW)]
	ax.clear()
	for index, sg in enumerate(wcc):
		color = (np.random.uniform(0,1), np.random.uniform(0,1),np.random.uniform(0,1))
		nx.draw(sg, pos, ax, edge_color = color, node_color = color)
	fig.canvas.draw_idle()
N = 500 #size of the grid

# Creating the subplot

#Generating the grid
G = nx.grid_2d_graph(N,N)
#Generating the position to be a square.
pos = dict((N,N) for N in G.nodes())

#creating the initial graph
fig, ax = plt.subplots()

# initializing the graph
nx.draw(G, pos, ax)

plt.subplots_adjust(bottom = 0.25)
#creating the buttons
axAlpha = plt.axes([0.25, 0.10, 0.65, 0.03])
alphaSlider = Slider(axAlpha, 'Probability', 0, 1.0)
alphaSlider.on_changed(update)
plt.show()


import pytest
from yarl import URL

async def _get_username(web_client_session, app_url):
    """
    Visits /hub/home to get an _xsrf token set to cookies, that we can then
    pass as a X-XSRFToken header when accessing /hub/api, then the function
    visit /hub/api/user to get the username as recognized by JupyterHub
    based on cookies passed.
    """
    hub_url = str(app_url / "hub/")
    home_url = str(app_url / "hub/home")
    api_user_url = str(app_url / "hub/api/user")

    r = await web_client_session.get(home_url)
    assert r.status == 200

    hub_cookies = web_client_session.cookie_jar.filter_cookies(home_url)
    if "_xsrf" in hub_cookies:
        _xsrf = hub_cookies["_xsrf"].value
    else:
        _xsrf = ""
    headers = {
        "X-XSRFToken": _xsrf,  # required for jupyterhub>=4
        "Referer": hub_url,  # required for jupyterhub<4
        "Accept": "application/json",
    }

    r = await web_client_session.get(api_user_url, headers=headers)
    assert r.status == 200
    user_api_response = await r.json()
    return user_api_response["name"]

@pytest.mark.parametrize(
    "test_config, test_status, test_location, test_url",
    [
        ({}, 302, "hub/tmplogin", None),
        ({"TmpAuthenticator": {"auto_login": True}}, 302, "hub/tmplogin", None),
        ({"TmpAuthenticator": {"auto_login": False}}, 200, None, "hub/login"),
    ],
)
async def test_auto_login_config(
    hub_app,
    hub_config,
    web_client_session,
    test_config,
    test_status,
    test_location,
    test_url,
):
    """
    Tests TmpAuthenticator.auto_login's behavior when its default value is used,
    and when its explicitly set.
    """
    hub_config.merge(test_config)

    app = await hub_app(hub_config)
    app_port = URL(app.bind_url).port
    app_url = URL(f"http://localhost:{app_port}{app.base_url}")

    login_url = str(app_url / "hub/login")
    r = await web_client_session.get(login_url, allow_redirects=False)

    assert r.status == test_status
    if test_url:
        assert test_url in str(r.url)
    if test_location:
        assert test_location in r.headers["Location"]

async def test_login(
    hub_app,
    hub_config,
    web_client_session,
):
    """
    Tests that the user is redirected and finally authorized for /hub/home.
    """
    app = await hub_app(hub_config)
    app_port = URL(app.bind_url).port
    app_url = URL(f"http://localhost:{app_port}{app.base_url}")

    home_url = str(app_url / "hub/home")
    r = await web_client_session.get(home_url)

    assert r.status == 200

@pytest.mark.parametrize(
    "test_setting_admin_to, test_status",
    [
        (True, 200),
        (False, 403),
    ],
)
async def test_post_auth_hook_config(
    hub_app,
    hub_config,
    web_client_session,
    test_setting_admin_to,
    test_status,
):
    """
    Tests that the inherited Authenticator.post_auth_hook is respected by
    updating the authentication dictionary's admin key and accessing /hub/admin,
    which should result in a forbidden response if not configured as admin via
    the post_auth_hook.
    """

    def set_admin_post_auth_hook(authenticator, handler, authentication):
        authentication["admin"] = test_setting_admin_to
        return authentication

    hub_config.TmpAuthenticator.post_auth_hook = set_admin_post_auth_hook

    app = await hub_app(hub_config)
    app_port = URL(app.bind_url).port
    app_url = URL(f"http://localhost:{app_port}{app.base_url}")

    admin_url = str(app_url / "hub/admin")
    r = await web_client_session.get(admin_url)

    assert r.status == test_status

async def test_revisit_tmplogin(
    hub_app,
    hub_config,
    web_client_session,
):
    """
    Tests that we get a new user if visiting /hub/tmplogin, even if we already
    were authenticated as one as recognized by cookies.

    This is done by first visiting /hub/home which should get us logged in and
    inspecting the user via a cookie, and then /hub/tmplogin to again inspect
    the user via a cookie.
    """
    app = await hub_app(hub_config)
    app_port = URL(app.bind_url).port
    app_url = URL(f"http://localhost:{app_port}{app.base_url}")

    # first access, so we receive a new user
    first_username = await _get_username(web_client_session, app_url)
    assert first_username

    # when we visit /hub/home again, we are recognized and that doesn't make us
    # arrive at /hub/login -> /hub/tmplogin, and therefore we shouldn't get a
    # new user
    assert first_username == await _get_username(web_client_session, app_url)

    # we visit /hub/tmplogin and should get a _new_ user
    tmplogin_url = str(app_url / "hub/tmplogin")
    r = await web_client_session.get(tmplogin_url)
    assert r.status == 200
    second_username = await _get_username(web_client_session, app_url)
    assert first_username != second_username


import os
import shutil

def list_files(startpath):
    for root, dirs, files in os.walk(startpath):
        for name in files:
            yield os.path.join(root, name)

source_dir = 'C:\\Users\\User\\Portabolt\\Portabolt - Documents'
destination_dir = 'C:\\Users\\User\\Documents\\Facturenfolder'

for file in list_files(source_dir):
    if file.endswith('.pdf'):
        shutil.copy(file, destination_dir)


'''
SCRIPT: ReadStoichiometry
@Authors:
    Alberto Cuoci [1]
    [1]: CRECK Modeling Lab, Department of Chemistry, Materials, and Chemical Engineering, Politecnico di Milano
@Contacts:
    alberto.cuoci@polimi.it
@Additional notes:
    This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    Please report any bug to: alberto.cuoci@polimi.it
'''

# Import main libraries
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import xml.etree.ElementTree as ET
from scipy.sparse import csr_matrix, find
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show
plt.style.use('default')

# Define path to PyTools classes
import sys
sys.path.insert(0, '../src')

# Import PyTools classes
from KineticMechanism import KineticMechanism
from OpenSMOKEppXMLFile import OpenSMOKEppXMLFile
from PolimiSootModule import *
from Utilities import *

# --------------------------------------------------------------------------------------
# Utilities
# --------------------------------------------------------------------------------------

def ReadVectorInt(vector, pos):
    length = int(vector[pos])
    subvector = vector[pos+1:pos+length+1]
    return np.int32(subvector), pos+length+1

def ReadVectorFloat64(vector, pos):
    length = int(vector[pos])
    subvector = vector[pos+1:pos+length+1]
    return np.float64(subvector), pos+length+1

# --------------------------------------------------------------------------------------
# Read kinetic mechanism
# --------------------------------------------------------------------------------------

kin_xml_folder_name="C:\\Users\\acuoci\\Aachen-Data\\Kinetics\\GRI30\\kinetics-GRI30\\"
kinetics = KineticMechanism(kin_xml_folder_name + "kinetics.xml")
kinetics.ReadKinetics(kin_xml_folder_name + "reaction_names.xml")

# --------------------------------------------------------------------------------------
# Import stoichiometry
# --------------------------------------------------------------------------------------

tree = ET.parse(kin_xml_folder_name + "kinetics.xml")
root = tree.getroot()
kinetics_element = root.find('Kinetics')
stoichiometry_element = kinetics_element.find('Stoichiometry')
stoichiometry = (stoichiometry_element.text).split()

# --------------------------------------------------------------------------------------
# Process stoichiometric data
# --------------------------------------------------------------------------------------

pos = 0
numDir1, pos = ReadVectorInt(stoichiometry, pos)
numDir2, pos = ReadVectorInt(stoichiometry, pos)
numDir3, pos = ReadVectorInt(stoichiometry, pos)
numDir4, pos = ReadVectorInt(stoichiometry, pos)
numDir5, pos = ReadVectorInt(stoichiometry, pos)

numRevTot1, pos = ReadVectorInt(stoichiometry, pos)
numRevTot2, pos = ReadVectorInt(stoichiometry, pos)
numRevTot3, pos = ReadVectorInt(stoichiometry, pos)
numRevTot4, pos = ReadVectorInt(stoichiometry, pos)
numRevTot5, pos = ReadVectorInt(stoichiometry, pos)

numRevEq1, pos = ReadVectorInt(stoichiometry, pos)
numRevEq2, pos = ReadVectorInt(stoichiometry, pos)
numRevEq3, pos = ReadVectorInt(stoichiometry, pos)
numRevEq4, pos = ReadVectorInt(stoichiometry, pos)
numRevEq5, pos = ReadVectorInt(stoichiometry, pos)

jDir1, pos = ReadVectorInt(stoichiometry, pos)
jDir2, pos = ReadVectorInt(stoichiometry, pos)
jDir3, pos = ReadVectorInt(stoichiometry, pos)
jDir4, pos = ReadVectorInt(stoichiometry, pos)
jDir5, pos = ReadVectorInt(stoichiometry, pos)
valueDir5, pos = ReadVectorFloat64(stoichiometry, pos)

jDir1 = jDir1-1
jDir2 = jDir2-1
jDir3 = jDir3-1
jDir4 = jDir4-1
jDir5 = jDir5-1

jRevTot1, pos = ReadVectorInt(stoichiometry, pos)
jRevTot2, pos = ReadVectorInt(stoichiometry, pos)
jRevTot3, pos = ReadVectorInt(stoichiometry, pos)
jRevTot4, pos = ReadVectorInt(stoichiometry, pos)
jRevTot5, pos = ReadVectorInt(stoichiometry, pos)
valueRevTot5, pos = ReadVectorFloat64(stoichiometry, pos)

jRevTot1 = jRevTot1-1
jRevTot2 = jRevTot2-1
jRevTot3 = jRevTot3-1
jRevTot4 = jRevTot4-1
jRevTot5 = jRevTot5-1

jRevEq1, pos = ReadVectorInt(stoichiometry, pos)
jRevEq2, pos = ReadVectorInt(stoichiometry, pos)
jRevEq3, pos = ReadVectorInt(stoichiometry, pos)
jRevEq4, pos = ReadVectorInt(stoichiometry, pos)
jRevEq5, pos = ReadVectorInt(stoichiometry, pos)
valueRevEq5, pos = ReadVectorFloat64(stoichiometry, pos)

jRevEq1 = jRevEq1-1
jRevEq2 = jRevEq2-1
jRevEq3 = jRevEq3-1
jRevEq4 = jRevEq4-1
jRevEq5 = jRevEq5-1

changeOfMoles, pos = ReadVectorFloat64(stoichiometry, pos)
explicit_reaction_orders = int(stoichiometry[pos])

# Elementary reactions only
if (explicit_reaction_orders == 0):

    lambda_numDir1 = numDir1;
    lambda_numDir2 = numDir2;
    lambda_numDir3 = numDir3;
    lambda_numDir4 = numDir4;
    lambda_numDir5 = numDir5;

    lambda_numRevEq1 = numRevEq1;
    lambda_numRevEq2 = numRevEq2;
    lambda_numRevEq3 = numRevEq3;
    lambda_numRevEq4 = numRevEq4;
    lambda_numRevEq5 = numRevEq5;

    lambda_jDir1 = jDir1;
    lambda_jDir2 = jDir2;
    lambda_jDir3 = jDir3;
    lambda_jDir4 = jDir4;
    lambda_jDir5 = jDir5;
    lambda_valueDir5 = valueDir5;

    lambda_jRevEq1 = jRevEq1;
    lambda_jRevEq2 = jRevEq2;
    lambda_jRevEq3 = jRevEq3;
    lambda_jRevEq4 = jRevEq4;
    lambda_jRevEq5 = jRevEq5;
    lambda_valueRevEq5 = valueRevEq5;

else:

    sys.exit("Non-elementary reactions cannot be processed")

# --------------------------------------------------------------------------------------
# Reactant side analysis (stoichiometric coefficients)
# --------------------------------------------------------------------------------------

react_species = []
react_reaction = []
react_nu = []

count1=0
count2=0
count3=0
count4=0
count5=0
for i in range(kinetics.ns):

    for k in range(numDir1[i]):
        react_species.append(i)
        react_reaction.append(jDir1[count1])
        react_nu.append(1.)
        count1 = count1+1

    for k in range(numDir2[i]):
        react_species.append(i)
        react_reaction.append(jDir2[count2])
        react_nu.append(2.)
        count2 = count2+1

    for k in range(numDir3[i]):
        react_species.append(i)
        react_reaction.append(jDir3[count3])
        react_nu.append(3.)
        count3 = count3+1

    for k in range(numDir4[i]):
        react_species.append(i)
        react_reaction.append(jDir4[count4])
        react_nu.append(0.5)
        count4 = count4+1

    for k in range(numDir5[i]):
        react_species.append(i)
        react_reaction.append(jDir5[count5])
        react_nu.append(valueDir5[count5])
        count5 = count5+1

nur = sparse.coo_matrix((react_nu,(react_reaction,react_species)),shape=(kinetics.nr,kinetics.ns))

# --------------------------------------------------------------------------------------
# Product side analysis (stoichiometric coefficients)
# --------------------------------------------------------------------------------------

react_species = []
react_reaction = []
react_nu = []

count1=0
count2=0
count3=0
count4=0
count5=0
for i in range(kinetics.ns):

    for k in range(numRevTot1[i]):
        react_species.append(i)
        react_reaction.append(jRevTot1[count1])
        react_nu.append(1.)
        count1 = count1+1

    for k in range(numRevTot2[i]):
        react_species.append(i)
        react_reaction.append(jRevTot2[count2])
        react_nu.append(2.)
        count2 = count2+1

    for k in range(numRevTot3[i]):
        react_species.append(i)
        react_reaction.append(jRevTot3[count3])
        react_nu.append(3.)
        count3 = count3+1

    for k in range(numRevTot4[i]):
        react_species.append(i)
        react_reaction.append(jRevTot4[count4])
        react_nu.append(0.5)
        count4 = count4+1

    for k in range(numRevTot5[i]):
        react_species.append(i)
        react_reaction.append(jRevTot5[count5])
        react_nu.append(valueRevTot5[count5])
        count5 = count5+1

nup = sparse.coo_matrix((react_nu,(react_reaction,react_species)),shape=(kinetics.nr,kinetics.ns))

# --------------------------------------------------------------------------------------
# Reactant side analysis (reaction orders)
# --------------------------------------------------------------------------------------

react_species = []
react_reaction = []
react_lambda = []

count1=0
count2=0
count3=0
count4=0
count5=0
for i in range(kinetics.ns):

    for k in range(lambda_numDir1[i]):
        react_species.append(i)
        react_reaction.append(lambda_jDir1[count1])
        react_lambda.append(1.)
        count1 = count1+1

    for k in range(lambda_numDir2[i]):
        react_species.append(i)
        react_reaction.append(lambda_jDir2[count2])
        react_lambda.append(2.)
        count2 = count2+1

    for k in range(lambda_numDir3[i]):
        react_species.append(i)
        react_reaction.append(lambda_jDir3[count3])
        react_lambda.append(3.)
        count3 = count3+1

    for k in range(lambda_numDir4[i]):
        react_species.append(i)
        react_reaction.append(lambda_jDir4[count4])
        react_lambda.append(0.5)
        count4 = count4+1

    for k in range(lambda_numDir5[i]):
        react_species.append(i)
        react_reaction.append(lambda_jDir5[count5])
        react_lambda.append(lambda_valueDir5[count5])
        count5 = count5+1

lambdar = sparse.coo_matrix((react_lambda,(react_reaction,react_species)),shape=(kinetics.nr,kinetics.ns))

# --------------------------------------------------------------------------------------
# Product side analysis (reaction orders)
# --------------------------------------------------------------------------------------

react_species = []
react_reaction = []
react_lambda = []

count1=0
count2=0
count3=0
count4=0
count5=0
for i in range(kinetics.ns):

    for k in range(lambda_numRevEq1[i]):
        react_species.append(i)
        react_reaction.append(lambda_jRevEq1[count1])
        react_lambda.append(1.)
        count1 = count1+1

    for k in range(lambda_numRevEq2[i]):
        react_species.append(i)
        react_reaction.append(lambda_jRevEq2[count2])
        react_lambda.append(2.)
        count2 = count2+1

    for k in range(lambda_numRevEq3[i]):
        react_species.append(i)
        react_reaction.append(lambda_jRevEq3[count3])
        react_lambda.append(3.)
        count3 = count3+1

    for k in range(lambda_numRevEq4[i]):
        react_species.append(i)
        react_reaction.append(lambda_jRevEq4[count4])
        react_lambda.append(0.5)
        count4 = count4+1

    for k in range(lambda_numRevEq5[i]):
        react_species.append(i)
        react_reaction.append(lambda_jRevEq5[count5])
        react_lambda.append(lambda_valueRevEq5[count5])
        count5 = count5+1

lambdap = sparse.coo_matrix((react_lambda,(react_reaction,react_species)),shape=(kinetics.nr,kinetics.ns))

# Final checks (to be removed)
diff_nur = nur - kinetics.nur
print(diff_nur)
diff_nup = nup - kinetics.nup
print(diff_nup)
diff_lambdar = lambdar - kinetics.nur
print(diff_lambdar)
#diff_lambdap = lambdap - kinetics.nup
#print(diff_lambdap)

# Final output (to be removed)
#for j in range(kinetics.nr):
#    indices1 = sparse.find(kinetics.nup.getrow(j))[1]
#    indices2 = sparse.find(nup.getrow(j))[1]
#    print(indices1, indices2)


import os
import pkg_resources

###
# This was obtained from https://stackoverflow.com/questions/16294819/check-if-my-python-has-all-required-packages
###

def check():

    # dependencies can be any iterable with strings,
    # e.g. file line-by-line iterator
    dependencies = []
    dir_path = os.path.dirname(os.path.realpath(__file__))

    with open(dir_path + "/../../requirements.txt", 'rb') as f:
        for line in f:
            requirement = line.decode('ascii').rstrip()
            if requirement:
                dependencies += [requirement]

    # here, if a dependency is not met, a DistributionNotFound or VersionConflict
    # exception is thrown.
    for dependency in dependencies:
        try:
            pkg_resources.require(dependency)
        except pkg_resources.ContextualVersionConflict as e:
            continue
        except (pkg_resources.DistributionNotFound, pkg_resources.VersionConflict) as e:
            print(e)
            return 1
        except Exception as e:
            return 1
    return 0


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from .models import Tag, User, Image
from rest_framework import status
from rest_framework.request import Request
import json
from django.views.decorators.http import require_http_methods
import logging
from .utils import validate_token, get_download_url

@require_http_methods(['POST', 'GET'])
def create_image(request: Request) -> HttpResponse:
    try:
        logging.error(request)
        logging.error(request.body)
        try:
            token_id = request.META['HTTP_AUTHORIZATION']
            user_id = validate_token(token_id)
        except Exception as exp:
            logging.error(exp)
            return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
        if request.method == 'POST':
            logging.error(request)
            if user_id is not None:
                image_data = json.loads(request.body)
                url = get_download_url(image_data['uri'], image_data['name'])
                if url == "":
                    return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
                image_content = {}
                if 'timestamp' in image_data:
                    image_content['timestamp'] = image_data['timestamp']
                image_content['name'] = image_data['name']
                image_content['place'] = image_data['place']
                image_content['uri'] = url
                image_content['user_id'] = user_id
                tag_ids = []
                tags = []
                if 'tags' in image_data:
                    for tag_id in image_data['tags']:
                        tag_ids.append(tag_id)
                        tags.append(Tag.objects.get(id=int(tag_id)))
                image_instance = Image(name=image_content['name'], place=image_content['place'],
                                       uri=image_content['uri'],
                                       user_id=image_content['user_id'], identity_group=0)
                image_instance.save()
                image_instance.identity_group = image_instance.id
                image_instance.save()
                image_instance = Image.objects.get(id=image_instance.id)
                for tag in tags:
                    image_instance.tags.add(tag)
                image_instance.save()
                shared_with = [user_instance.id for user_instance in image_instance.shared_with.all()]
                image_instance_json = json.dumps({'id': image_instance.id, 'name': image_instance.name,
                                                  'place': image_instance.place, 'uri': image_instance.uri,
                                                  'timestamp': str(image_instance.timestamp),
                                                  'user_id': user_id, 'tags': tag_ids, 'shared_with': shared_with})
                return HttpResponse(image_instance_json, status=status.HTTP_201_CREATED)
            else:
                return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
        if request.method == 'GET':
            if user_id is not None:
                logging.error(request)
                own_by_me = Image.objects.filter(user_id=user_id)
                shared_with_me = Image.objects.filter(shared_with__id=user_id)
                all_images = []
                for image in own_by_me:
                    all_images.append(image.id)
                for image in shared_with_me:
                    all_images.append(image.id)
                return HttpResponse(json.dumps({"image_ids": all_images}), status=status.HTTP_200_OK)
            return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
    except Exception as ex:
        logging.error('Final Catch se ye error aayi hai ' + str(ex))
        return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
    return HttpResponse(status=status.HTTP_404_NOT_FOUND)

@require_http_methods(['GET', 'PUT', 'DELETE'])
def image_crud(request: Request, image_id: int):
    logging.error(request)
    try:
        token_id = request.META['HTTP_AUTHORIZATION']
        user_id = validate_token(token_id)
    except Exception as exp:
        return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
    if user_id is not None:
        if request.method == 'GET':
            try:
                image_instance = Image.objects.get(id=image_id)
            except Exception as exp:
                return HttpResponse(status=status.HTTP_404_NOT_FOUND)
            if image_instance.user_id == user_id:
                image_instance_json = json.dumps({
                    'id': image_instance.id,
                    'name': image_instance.name,
                    'place': image_instance.place,
                    'uri': image_instance.uri,
                    'timestamp': str(image_instance.timestamp),
                    'tags': [tag_instance.id for tag_instance in image_instance.tags.all()],
                    'shared_with': [user_instance.id for user_instance in image_instance.shared_with.all()]
                })
                return HttpResponse(image_instance_json, status=status.HTTP_200_OK)
            return HttpResponse('get call kiya!!', status=status.HTTP_401_UNAUTHORIZED)
        if request.method == 'PUT':
            image_instance = Image.objects.get(id=image_id)
            if image_instance.user_id == user_id:
                query = json.loads(request.body)
                if 'name' in query:
                    image_instance.name = query['name']
                if 'place' in query:
                    image_instance.place = query['place']
                if 'uri' in query:
                    image_instance.uri = query['uri']
                if 'timestamp' in query:
                    image_instance.timestamp = query['timestamp']
                image_instance.save()
                tag_ids = []
                if 'tags' in query:
                    image_instance.tags.clear()
                    for tag_id in query['tags']:
                        tag_ids.append(tag_id)
                        image_instance.tags.add(Tag.objects.get(id=int(tag_id)))
                image_instance.save()
                image_instance_json = json.dumps({
                    'id': image_instance.id,
                    'name': image_instance.name,
                    'place': image_instance.place,
                    'uri': image_instance.uri,
                    'timestamp': str(image_instance.timestamp),
                    'tags': [tag_instance.id for tag_instance in image_instance.tags.all()]
                })
                return HttpResponse(image_instance_json, status=status.HTTP_200_OK)
        if request.method == 'DELETE':
            image_instance = Image.objects.get(id=image_id)
            if image_instance.user_id == user_id:
                image_instance.delete()
                return HttpResponse(status=status.HTTP_204_NO_CONTENT)
    return HttpResponse(status=status.HTTP_404_NOT_FOUND)

@require_http_methods(['GET'])
def get_private_images(request: Request):
    try:
        logging.error(request)
        try:
            token_id = request.META['HTTP_AUTHORIZATION']
            user_id = validate_token(token_id)
        except Exception as exp:
            return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
        if user_id is not None:
            image_instances = Image.objects.all().filter(user_id=user_id)
            image_ids = []
            for image_instance in image_instances:
                image_ids.append(image_instance.id)
            return HttpResponse(json.dumps({'image_ids': image_ids}), status=status.HTTP_200_OK)
        else:
            return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
    except Exception as exp:
        logging.error(str(exp))
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)

@require_http_methods(['GET'])
def get_shared_images(request):
    try:
        logging.error(request)
        token_id = request.META['HTTP_AUTHORIZATION']
        user_id = validate_token(token_id)
        if user_id is not None:
            image_instances = Image.objects.filter(shared_with__id=user_id)
            image_ids = []
            for image_instance in image_instances:
                image_ids.append(image_instance.id)
            return HttpResponse(json.dumps({"image_ids": image_ids}), status=status.HTTP_200_OK)
        return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
    except Exception as exp:
        logging.error("exception has occured: " + str(exp))
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)

def image_share(request: Request, image_id: int):
    try:
        logging.error(request)
        try:
            token_id = request.META['HTTP_AUTHORIZATION']
            user_id = validate_token(token_id)
            user_ids = json.loads(request.body)['user_ids']
        except Exception as exp:
            return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
        if user_id is not None:
            image_instance = Image.objects.get(id=image_id)
            if image_instance.user_id == user_id:
                try:
                    if len(user_ids) != len(set(user_ids)) or len(user_ids) == 0:
                        return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
                    for user_id_ in user_ids:
                        if user_id == user_id_:
                            return HttpResponse(status=status.HTTP_403_FORBIDDEN)
                        image_instance.shared_with.add(User.objects.get(id=int(user_id_)))
                except Exception as exp:
                    return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
                image_instance.save()
                return HttpResponse(status=status.HTTP_204_NO_CONTENT)
            return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    except Exception as ex:
        return HttpResponse(status=status.HTTP_400_BAD_REQUEST)

    return HttpResponse(status=status.HTTP_404_NOT_FOUND)

@require_http_methods(['GET', 'POST'])
def identical_image(request, image_id: int):
    try:
        logging.error(request)
        token_id = request.META['HTTP_AUTHORIZATION']
        user_id = validate_token(token_id)
    except Exception as exp:
        return HttpResponse(status=status.HTTP_401_UNAUTHORIZED)
    if user_id is not None:
        try:
            image_instance = Image.objects.get(id=image_id)
        except Exception as exp:
            return HttpResponse(status=status.HTTP_404_NOT_FOUND)
        if user_id == image_instance.user_id:
            if request.method == 'GET':
                identity_group = image_instance.identity_group
                identical_images = Image.objects.filter(identity_group=identity_group)
                image_ids = []
                for image in identical_images:
                    if int(image.id) != int(image_id):
                        image_ids.append(image.id)
                return HttpResponse(json.dumps({"image_ids": image_ids}), status=status.HTTP_200_OK)

            if request.method == 'POST':
                identity_group = image_instance.identity_group
                logging.error(request.body)
                image_ids = json.loads(request.body)['image_ids']
                if len(image_ids) != len(set(image_ids)) or len(image_ids) == 0:
                    return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
                try:
                    identical_images = []
                    for image_id in image_ids:
                        image = Image.objects.get(id=int(image_id))
                        if image.user_id != user_id:
                            return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
                        if image.id == image_instance.id:
                            return HttpResponse(status=status.HTTP_403_FORBIDDEN)
                        current_identity_group = image.identity_group
                        for im in Image.objects.filter(identity_group=current_identity_group):
                            identical_images.append(im)
                    for im in identical_images:
                        im.identity_group = identity_group
                        im.save()
                    return HttpResponse(status=status.HTTP_204_NO_CONTENT)
                except Exception as exp:
                    logging.error(exp)
                    return HttpResponse(status=status.HTTP_400_BAD_REQUEST)
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    return HttpResponse(status=status.HTTP_400_BAD_REQUEST)


import pandas as pd
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
import pickle
from sklearn.metrics import roc_curve
from matplotlib import pyplot
import numpy as np

x_train = pd.read_csv('x_train')
x_valid = pd.read_csv('x_valid')
y_train = pd.read_csv('y_train')
y_valid = pd.read_csv('y_valid')

parameters = {
    'kernel':("linear","rbf"),
    'gamma':[1,0.1,0.01,0.001],
    'C':[1,10,100,1000]
}
#
# mySVM = SVC(max_iter=1500)
# Grid = GridSearchCV(mySVM,parameters,cv=3,verbose=1)
# #Dans le cas de SVM nous devons n'avoir qu'une colonne de label, représentant la classe
# print("GridSearch launched")
# Grid.fit(x_train,y_train.iloc[:,1].values) #Le label prédit sera : "Le salaire est-il supérieur à 50K ?"  oui -> 1 sinon 0
# mySVM = Grid.best_estimator_
# pickle.dump(mySVM, open("BestSVM","wb"))

mySVM = pickle.load(open("BestSVM","rb"))
print("Best SVM selected is : ")
print(mySVM.get_params)
print("Accuracy on training set : "  + str(mySVM.score(x_train,y_train.iloc[:,1].values)))
print("Accuracy on valid set : "  + str(mySVM.score(x_valid,y_valid.iloc[:,1].values)))
# plotRoc(mySVM,x_train,y_train.iloc[:,1],False)

y_pred = mySVM.predict(x_valid)
name = 'SVM'
fpr, tpr, thr = roc_curve(y_valid.iloc[:,1].values, y_pred,pos_label=1)
fig = pyplot.figure(figsize=(6, 6))
pyplot.plot(fpr, tpr, '-', lw=2, label=name)
pyplot.plot([0, 1], [0, 1], color='red', lw=1, linestyle='--',label='Courbe théorique')
pyplot.xlabel('False Positive Rate', fontsize=16)
pyplot.ylabel('True Positive Rate', fontsize=16)
pyplot.title('Courbe de ROC : '+name, fontsize=16)
pyplot.legend(loc="lower right", fontsize=14)
pyplot.show()


# Temperature calculator

def celicustempcalc():
    try:
        Current_F = float(input("What is the current temp in farenheit? "))

        Current_C = 5.0 * (float(Current_F) - 32.0) / 9.0

        print("The current temperature in celcius is: " + str(Current_C))
    except Exception as e:
        print("Error: " + e)
    return

def farenheittempcalc():
    try:
        Current_C_Temp = float(input("What is the current temp in celcius? "))

        Current_F_Temp = (float(Current_C_Temp) * 9.0/5.0) + 32

        print("The current temperature in farenheit is: " + str(Current_F_Temp))
    except Exception as e2:
        print("Error: " + e2)
    return

def chooser():
    tempvariable = str(input("What tempature do you need converted from? Farenheit or celcius? "))
    if tempvariable.lower() == "farenheit":
        farenheittempcalc()
    elif tempvariable.lower() == "celcius":
        celicustempcalc()
    else:
        print("Error: Expecting 'farenheit' or 'celcius'")

if __name__ == '__main__':
    chooser()


import requests
from datetime import date, datetime, timedelta
from json import dump
import sys

def datespan(start, end, delta=timedelta(days=1)):
    current = start
    while current < end:
        yield current
        current += delta

with open(sys.argv[1], 'w') as file:
    for dates in datespan(date(2021, 1, 1), date(2021,2,1),delta=timedelta(days=1)):
        day = str(dates).replace("-", "")
        res = requests.get("https://bank.gov.ua/NBUStatService/v1/statdirectory/exchange?valcode={0}&date={1}&json".format(sys.argv[2],day))
        dump(res.json(), file,
            sort_keys=False,
            ensure_ascii=False,
            separators=(',', ': '),
            indent=4)

with open(sys.argv[1],'r') as file:
    data = file.read()
    data = data.replace("][",",")

with open(sys.argv[1],'w') as file:
    file.write(data)