from datetime import date, datetime, timedelta

from django.test import TestCase
from django.utils.formats import localize
from model_bakery import baker

from evap.evaluation.models import Evaluation, Course, Contribution, Questionnaire
from evap.evaluation.models_logging import FieldAction

class TestLoggedModel(TestCase):
    def setUp(self):
        self.old_start_date = datetime.now()
        self.new_start_date = self.old_start_date + timedelta(days=10)

        self.course = baker.make(Course)

        self.evaluation = baker.make(
            Evaluation,
            course=self.course,
            state="prepared",
            vote_start_datetime=self.old_start_date,
            vote_end_date=date.today() + timedelta(days=20),
        )
        self.evaluation.save()  # first logentry

        self.evaluation.vote_start_datetime = self.new_start_date
        self.evaluation.save()  # second logentry

        self.logentry = self.evaluation.related_logentries()[1]

    def test_voters_not_in_evaluation_data(self):
        self.assertFalse(any("voters" in entry.data for entry in self.evaluation.related_logentries()))

    def test_datetime_change(self):
        self.assertEqual(
            self.logentry.data["vote_start_datetime"],
            {"change": [localize(self.old_start_date), localize(self.new_start_date)]},
        )

    def test_data_attribute_is_correctly_parsed_to_fieldactions(self):
        self.assertEqual(
            self.logentry.field_context_data["vote_start_datetime"],
            [
                FieldAction(
                    "Start of evaluation",
                    "change",
                    [localize(self.old_start_date), localize(self.new_start_date)],
                )
            ],
        )

    def test_deletion_data(self):
        self.assertEqual(self.evaluation._get_change_data(action_type="delete")['course']['delete'][0], self.course.id)
        self.evaluation.delete()
        self.assertEqual(self.evaluation.related_logentries().count(), 0)

    def test_creation(self):
        course = baker.make(Course)
        self.assertEqual(course.related_logentries().count(), 1)

    def test_related_logged_model_creation(self):
        self.assertEqual(self.evaluation.related_logentries().count(), 2)
        contribution = baker.make(Contribution, evaluation=self.evaluation)
        self.assertFalse(contribution.related_logentries().exists())
        self.assertEqual(self.evaluation.related_logentries().count(), 3)

    def test_m2m_creation(self):
        self.assertEqual(self.evaluation.related_logentries().count(), 2)
        questionnaire = baker.make(Questionnaire)
        contribution = self.evaluation.contributions.get(contributor__isnull=True)
        contribution.questionnaires.add(questionnaire)
        self.assertEqual(self.evaluation.related_logentries().count(), 3)
        self.assertEqual(self.evaluation.related_logentries().order_by("id").last().data['questionnaires']['add'], [questionnaire.id])

    def test_none_value_not_included(self):
        baker.make(Contribution, evaluation=self.evaluation, label="testlabel")
        self.assertIn("label", self.evaluation.related_logentries().order_by("id").last().data)

        baker.make(Contribution, evaluation=self.evaluation, label=None)
        self.assertNotIn("label", self.evaluation.related_logentries().order_by("id").last().data)


#!/usr/bin/env python
# coding: utf-8

# In[53]:

import pandas as pd
import numpy as np

# In[54]:

data = pd.read_excel('C:\\Users\\Gaddam Srinivas\\Load Data in kW.xlsx')

# In[55]:

data.isnull().sum()

# In[56]:

data.head()

# In[57]:

X=data[data.columns[:3]]
X.head()

# In[58]:

y=data[['Load (kW)']]
y.head()

# In[75]:

from sklearn.model_selection import train_test_split
#train test split
X_train, X_test, y_train,y_test = train_test_split(x, y, test_size=0.2,random_state=121)

# # ANN implementation using Adagrad Optimizer

# In[76]:

def define_structure(X, Y):
    input_unit = X.shape[0] # size of input layer
    hidden_unit = 4 #hidden layer of size 4
    output_unit = Y.shape[0] # size of output layer
    return (input_unit, hidden_unit, output_unit)
(input_unit, hidden_unit, output_unit) = define_structure(X_train, y_train)
print("The size of the input layer is:  = " + str(input_unit))
print("The size of the hidden layer is:  = " + str(hidden_unit))
print("The size of the output layer is:  = " + str(output_unit))

# In[77]:

def parameters_initialization(input_unit, hidden_unit, output_unit):
    np.random.seed(2)
    W1 = np.random.randn(hidden_unit, input_unit)*0.01
    b1 = np.zeros((hidden_unit, 1))
    W2 = np.random.randn(output_unit, hidden_unit)*0.01
    b2 = np.zeros((output_unit, 1))
    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

    return parameters

# In[78]:

def sigmoid(z):
    return 1/(1+np.exp(-z))

# In[79]:

def forward_propagation(X, parameters):
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    Z1 = np.dot(W1, X) + b1
    A1 = np.tanh(Z1)
    Z2 = np.dot(W2, A1) + b2
    A2 = sigmoid(Z2)
    cache = {"Z1": Z1,"A1": A1,"Z2": Z2,"A2": A2}

    return A2, cache

# In[80]:

def cross_entropy_cost(A2, Y, parameters):
    # number of training example
    m = Y.shape[1]
    # Compute the cross-entropy cost
    logprobs = np.multiply(np.log(A2), Y) + np.multiply((1-Y), np.log(1 - A2))
    cost = - np.sum(logprobs) / m
    cost = float(np.squeeze(cost))

    return cost

# In[81]:

def backward_propagation(parameters, cache, X, Y):
    #number of training example
    m = X.shape[1]

    W1 = parameters['W1']
    W2 = parameters['W2']
    A1 = cache['A1']
    A2 = cache['A2']

    dZ2 = A2-Y
    dW2 = (1/m) * np.dot(dZ2, A1.T)
    db2 = (1/m) * np.sum(dZ2, axis=1, keepdims=True)
    dZ1 = np.multiply(np.dot(W2.T, dZ2), 1 - np.power(A1, 2))
    dW1 = (1/m) * np.dot(dZ1, X.T)
    db1 = (1/m)*np.sum(dZ1, axis=1, keepdims=True)

    grads = {"dW1": dW1, "db1": db1, "dW2": dW2,"db2": db2}

    return grads

# In[89]:

def do_adagrad(parameters,grads,learning_rate = 0.01):
    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    dW1 = grads['dW1']
    db1 = grads['db1']
    dW2 = grads['dW2']
    db2 = grads['db2']
    v_w1, v_b1, v_w2, v_b2, eps = 0, 0, 0, 0, 1e-8
    for i in range(max_epochs):
        dw1, db1, dw2, db2 = 0, 0, 0, 0
        for x,y in zip(X,Y):
            dw1 += grad_w(W1, b1, x, y)
            db1 += grad_b(W1, b1, x, y)
            dw2 += grad_w(W2, b2, x, y)
            db2 += grad_b(W2, b2, x, y)

        v_w1 = v_w1 + dw1**2
        v_b1 = v_b1 + db1**2
        v_w2 = v_w2 + dw2**2
        v_b2 = v_b2 + db2**2

        W1 = W1 - (eta/np.sqrt(v_w1 + eps)) * dw1
        b1 = b1 - (eta/np.sqrt(v_b1 + eps)) * db1
        W2 = W2 - (eta/np.sqrt(v_w2 + eps)) * dw2
        b2 = b2 - (eta/np.sqrt(v_b2 + eps)) * db2

    parameters = {"W1": W1, "b1": b1,"W2": W2,"b2": b2}
    return parameters

# In[ ]:

def neural_network_model(X, Y, hidden_unit, num_iterations = 1000):
    np.random.seed(3)
    input_unit = define_structure(X, Y)[0]
    output_unit = define_structure(X, Y)[2]

    parameters = parameters_initialization(input_unit, hidden_unit, output_unit)

    W1 = parameters['W1']
    b1 = parameters['b1']
    W2 = parameters['W2']
    b2 = parameters['b2']

    for i in range(0, num_iterations):
        A2, cache = forward_propagation(X, parameters)
        cost = cross_entropy_cost(A2, Y, parameters)
        grads = backward_propagation(parameters, cache, X, Y)
        parameters = do_adagrad(parameters, grads)
        if i % 5 == 0:
            print ("Cost after iteration %i: %f" %(i, cost))
    return parameters
parameters = neural_network_model(X_train, y_train, 4, num_iterations=1000)

# In[ ]:

def prediction(parameters, X):
    A2, cache = forward_propagation(X, parameters)
    predictions = np.round(A2)

    return predictions

# In[ ]:

predictions = prediction(parameters, X_train)
print ('Accuracy Train: %d' % float((np.dot(y_train, predictions.T) + np.dot(1 - y_train, 1 - predictions.T))/float(y_train.size)*100) + '%')
predictions = prediction(parameters, X_test)
print ('Accuracy Test: %d' % float((np.dot(y_test, predictions.T) + np.dot(1 - y_test, 1 - predictions.T))/float(y_test.size)*100) + '%')


# write your code here

import argparse
import os

class FileHandler:
    def __init__(self):
        self.file_list = []
        self.size_dict = {}

    @staticmethod
    def size_sort_valid(size):
        while size not in [1, 2]:
            print('Wrong option')
            size = input()
        print()

        return size

    def file_list_append(self, file_format, name):
        for root, dirs, files in os.walk(name):
            if file_format == '':
                self.file_list.extend(
                    [
                        (os.path.join(root, v), os.path.getsize(os.path.join(root, v)))
                        for v in files
                    ]
                )
            else:
                self.file_list.extend(
                    [
                        (os.path.join(root, v), os.path.getsize(os.path.join(root, v)))
                        for v in files
                        if v.endswith(f'.{file_format}')
                    ]
                )

    def size_dict_add(self):
        for a, b in self.file_list:
            self.size_dict.setdefault(b, []).append(a)

    def find_root_chdir(self, name):
        if name != 'Directory is not specified':

            print('Enter file format:')
            file_format = input()

            print('Size sorting options:')
            print('1. Descending')
            print('2. Ascending')

            print('Enter a sorting option:')
            size_sort = int(input())
            size_sort_result = self.size_sort_valid(size_sort)

            self.file_list_append(file_format, name)
            self.size_dict_add()

            if size_sort_result == 1:
                size_keys = sorted(self.size_dict, reverse=True)
            else:
                size_keys = sorted(self.size_dict)

            for s in size_keys:
                print(f'{s} bytes')
                for v in self.size_dict[s]:
                    print(v)
                print()
        else:
            print(name)

def main():
    parse = argparse.ArgumentParser()
    parse.add_argument('root_folder', nargs='?', default='Directory is not specified')
    args_name = parse.parse_args()

    file_handler = FileHandler()
    file_handler.find_root_chdir(args_name.root_folder)

if __name__ == '__main__':
    main()


# Обробка кількох з'єднань одночасно, потоки та процеси
import socket
import threading
import multiprocessing
import os

def process_request(conn, addr):
    print("connected client:", addr)
    with conn:
        while True:
            data = conn.recv(1024)
            if not data:
                break
            print(data.decode("utf8"))

def worker(sock):
    while True:
        conn, addr = sock.accept()
        print("pid", os.getpid())
        th = threading.Thread(target=process_request,
                              args=(conn, addr))
        th.start()

with socket.socket() as sock:
    sock.bind(("", 10001))
    sock.listen()
    workers_count = 3
    workers_list = [multiprocessing.Process(target=worker,
                                            args=(sock,))
                    for _ in range(workers_count)]
    for w in workers_list:
        w.start()
    for w in workers_list:
        w.join()


from flask import render_template, abort, flash, redirect, url_for, request, current_app, make_response
from . import main
from ..models import User, Role, Permission, Post, Comment
from flask_login import login_required, current_user
from .forms import EditProfileForm, EditProfileAdminForm, PostForm, CommentForm
from ..models import db
from ..decorators import admin_required, permission_required

# 蓝图为该蓝图下的全部端点添加了一个命名空间，不同蓝图可以有相同的端点

# 文章分页显示，显示所有文章或者只显示所关注用户的文章
@main.route('/', methods=['GET', 'POST'])
def index():
    form = PostForm()
    if current_user.can(Permission.WRITE_ARTICLES) and \
            form.validate_on_submit():
        # _get_current_object()获取数据库中真正的用户对象
        post = Post(body=form.body.data, author=current_user._get_current_object())
        db.session.add(post)
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)  # 页数
    # 用 show_followed 表示是否显示所关注用户的文章
    show_followed = False
    if current_user.is_authenticated:
        show_followed = bool(request.cookies.get('show_followed', ''))
    if show_followed:
        query = current_user.followed_posts
    else:
        query = Post.query
    # Flask - SQLAlchemy 提供的 paginate()方法。页数是 paginate()方法的第一个参数，也是唯一必需的参数。
    # 可选参数 per_page 用来指定每页显示的记录数量； 如果没有指定，则默认显示20个记录
    # 可选参数 error_out，当其设为 True 时（默认值），如果请求的页数超出了范围，则会返回 404 错误；、
    # 如果设为 False，页数超出范围时会返回一个空列表
    # 文章按时间顺序排列
    # pagination对象用于产生分页链接，将其传给模板参数
    pagination = query.order_by(Post.timestamp.desc()).paginate(
        page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
        error_out=False)
    posts = pagination.items   # 当前页面中的记录
    return render_template('index.html', form=form, posts=posts,
                           show_followed=show_followed, pagination=pagination)

# 用户资料页
@main.route('/user/<username>')
def user(username):
    user = User.query.filter_by(username=username).first_or_404()
    page = request.args.get('page', 1, type=int)
    pagination = user.posts.order_by(Post.timestamp.desc()).paginate(
        page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'],
        error_out=False)
    posts = pagination.items
    return render_template('user.html', user=user, posts=posts,
                           pagination=pagination)

# 编辑个人资料
@main.route('/eit-profile', methods=['GET', 'POST'])
@login_required
def edit_profile():
    form = EditProfileForm()   # 这个表单中的内容是可选的
    if form.validate_on_submit():
        current_user.name = form.name.data
        current_user.location = form.location.data
        current_user.about_me = form.about_me.data
        db.session.add(current_user)
        flash('Your profile has been updated')
        return redirect(url_for('.user', username=current_user.username))
    # 初始化表单中的值
    form.name.data = current_user.name
    form.location.data = current_user.location
    form.about_me.data = current_user.about_me
    return render_template('edit_profile.html', form=form)

# 管理员编辑用户个人资料
@main.route('/edit-profile/<int:id>', methods=['GET', 'POST'])
@login_required
@admin_required
def edit_profile_admin(id):
    # get_or_404由Flask-SQLAlchemy提供，未查询到则返回404错误
    user = User.query.get_or_404(id)
    form = EditProfileAdminForm(user=user)
    if form.validate_on_submit():
        user.email = form.email.data
        user.username = form.username.data
        user.confirmed = form.confirmed.data
        user.role = Role.query.get(form.role.data)
        user.name = form.name.data
        user.location = form.location.data
        user.about_me = form.about_me.data
        db.session.add(user)
        flash('The profile has been updated.')
        return redirect(url_for('.user', username=user.username))
    form.email.data = user.email
    form.username.data = user.username
    form.confirmed.data = user.confirmed
    form.role.data = user.role_id
    form.name.data = user.name
    form.location.data = user.location
    form.about_me.data = user.about_me
    return render_template('edit_profile.html', form=form, user=user)

# 文章的固定链接，支持博客文章评论
@main.route('/post/<int:id>', methods=['GET', 'POST'])
def post(id):
    # 博客文章的URL使用插入数据库时分配的唯一id字段构建
    post = Post.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit() and post is not None:
        # print(form.body.data)
        comment = Comment(body=form.body.data,
                          post=post,
                          author=current_user._get_current_object())
        db.session.add(comment)
        flash('Your comment has been published.')
        return redirect(url_for('.post', id=post.id, page=-1))    # -1用来请求评论的最后一页，
    page = request.args.get('page', 1, type=int)
    if page == -1:
        page = (post.comments.count() - 1) // \
               current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1
    pagination = post.comments.order_by(Comment.timestamp.asc()).paginate(
        page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
        error_out=False)
    comments = pagination.items
    return render_template('post.html', posts=[post], form=form,
                           comments=comments, pagination=pagination)

# 编辑文章
@main.route('/edit/<int:id>', methods=['GET', 'POST'])
@login_required
def edit(id):
    post = Post.query.get_or_404(id)
    if current_user != post.author and \
            not current_user.can(Permission.ADMINISTER):
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.body = form.body.data
        db.session.add(post)
        flash('The post has been updated.')
        return redirect(url_for('.post', id=post.id))
    form.body.data = post.body
    return render_template('edit_post.html', form=form)

# 关注用户
@main.route('/follow/<username>')
@login_required
@permission_required(Permission.FOLLOW)
def follow(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user')
        return redirect(url_for('.index'))
    if current_user.is_following(user):
        flash('You are already following this user')
        return redirect(url_for('.user', username=username))
    current_user.follow(user)
    flash('You are now following %s.'%username)
    return redirect(url_for('.user', username=username))

@main.route('/unfollow/<username>')
@login_required
@permission_required(Permission.FOLLOW)
def unfollow(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user.')
        return redirect(url_for('.index'))
    if not current_user.is_following(user):
        flash('You are not following this user.')
        return redirect(url_for('.user', username=username))
    current_user.unfollow(user)
    flash('You are not following %s anymore.' % username)
    return redirect(url_for('.user', username=username))

# 关注者路由
@main.route('/followers/<username>')
def followers(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user.')
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    pagination = user.followers.paginate(
        page, per_page=current_app.config['FLASKY_FOLLOWERS_PER_PAGE'],
        error_out=False)
    follows = [{'user': item.follower, 'timestamp': item.timestamp}
               for item in pagination.items]
    return render_template('followers.html', user=user, title="Followers of",
                           endpoint='.followers', pagination=pagination,
                           follows=follows)

@main.route('/followed-by/<username>')
def followed_by(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user.')
        return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    pagination = user.followed.paginate(
        page, per_page=current_app.config['FLASKY_FOLLOWERS_PER_PAGE'],
        error_out=False)
    follows = [{'user': item.followed, 'timestamp': item.timestamp}
               for item in pagination.items]
    return render_template('followers.html', user=user, title="Followed by",
                           endpoint='.followed_by', pagination=pagination,
                           follows=follows)

# 查询所有文章还是所关注的用户的文章
@main.route('/all')
@login_required
def show_all():
    # cookie 只能在响应对象中设置，不能依赖 Flask，要使用 make_response（）方法创建响应对象
    resp = make_response(redirect(url_for('.index')))
    resp.set_cookie('show_followed', '', max_age=30*24*60*60)   # max_age 单位为秒，过期时间为30天
    return resp

@main.route('/followed')
@login_required
def show_followed():
    resp = make_response(redirect(url_for('.index')))
    resp.set_cookie('show_followed', '1', max_age=30*24*60*60)
    return resp

# 管理评论
@main.route('/moderate')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def moderate():
    page = request.args.get('page',1,type=int)
    pagination = Comment.query.order_by(Comment.timestamp.desc()).paginate(
        page,per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
        error_out=False
    )
    comments = pagination.items
    return render_template('moderate.html', comments=comments,
                           pagination=pagination, page=page)

@main.route('/moderate/enable/<int:id>')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def moderate_enable(id):
    comment = Comment.query.get_or_404(id)
    comment.disabled = False
    db.session.add(comment)
    return redirect(url_for('.moderate',
                            page=request.args.get('page', 1, type=int)))

@main.route('/moderate/disable/<int:id>')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def moderate_disable(id):
    comment = Comment.query.get_or_404(id)
    comment.disabled = True
    db.session.add(comment)
    return redirect(url_for('.moderate',
                            page=request.args.get('page', 1, type=int)))


import numpy as np
import torch
from torch_geometric.data import InMemoryDataset, Dataset, Data
from torch_geometric.loader import DataLoader
import os.path as osp

from sim import GraphSimulator

class ParticleRobotDataset(InMemoryDataset):
    def __init__(self, root, transform=None, pre_transform=None, start_idx=0):
        self.episodes = 100
        self.episodelength = 5000
        self.start_idx = start_idx
        super(ParticleRobotDataset, self).__init__(root, transform, pre_transform)
        self.data, self.slices = torch.load(self.processed_paths[0])

    @property
    def raw_file_names(self):
        return "not_implemented.pt"

    @property
    def processed_file_names(self):
        return 'data.pt'
        # return f'episode_{self.episodes-1+self.start_idx}.pt'

    def download(self):
        pass

    def process(self):
        numbots = 9
        simulator = GraphSimulator(num=numbots, start=(300, 300), goal=(0, 0), visualizer=None)
        data_list = []

        for epoch in range(self.episodes):
            simulator.setup()
            for step in range(self.episodelength):
                action = np.random.randint(2, size=numbots)
                data = simulator.step(action, append_action=True)
                data_list.append(data)

        data, slices = self.collate(data_list)
        torch.save((data, slices), osp.join(self.processed_dir, 'data.pt'))

dataset = ParticleRobotDataset(root="in_memory_data/")


import pytest

answer = [0, 0]

def compression(arr, x, y, check_length):
    sum = 0

    for i in range(x, x+check_length):
        for j in range(y, y+check_length):
            sum += arr[i][j]

    if sum == 0:
        answer[0] += 1
        return
    elif sum == check_length*check_length:
        answer[1] += 1
        return
    else:
        next_length = check_length // 2
        compression(arr, x, y, next_length)
        compression(arr, x, y + next_length, next_length)
        compression(arr, x + next_length, y, next_length)
        compression(arr, x + next_length, y + next_length, next_length)

def solution(arr):
    compression(arr, 0, 0, len(arr))
    return answer

@pytest.mark.parametrize("arr, expected", [
    ([[1,1,0,0],[1,0,0,0],[1,0,0,1],[1,1,1,1]], [4,9])
    # ([[1,1,1,1,1,1,1,1],[0,1,1,1,1,1,1,1],[0,0,0,0,1,1,1,1],[0,1,0,0,1,1,1,1],[0,0,0,0,0,0,1,1],[0,0,0,0,0,0,0,1],[0,0,0,0,1,0,0,1],[0,0,0,0,1,1,1,1]], [10, 15])
])
def test_simple(arr, expected):
    assert solution(arr) == expected

"""
[ 풀이 방법 ]
-
"""

2-쿼드압축 후 개수 세기.py


import matplotlib
matplotlib.use('TkAgg')
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
print('Finished imports')

admits = pd.read_csv('admit.csv')
gre_x = np.array(admits.gre.tolist())
admit_y = np.array(admits.admit.tolist())

# plt.grid(True)

# for (gre, admit) in zip(gre_x, admit_y):
# 	plt.plot(gre, admit, '.', color='orange')

# plt.ylim(-0.5, 1.5)
# plt.show()

x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)

W = tf.Variable(0.0, tf.float32)
b = tf.Variable(0.0, tf.float32)

a = 1/1 + tf.exp(-tf.add(tf.multiply(x, W), b))

# Logistic regression cost function
cross_entropy = tf.reduce_mean(-(y * tf.log(a) + (1 - y) * tf.log(1 - a)))
cross_entropy = tf.Print(cross_entropy, [a])

# Will take care of differentiation n all
train = tf.train.GradientDescentOptimizer(0.0001).minimize(cross_entropy)

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

# 1000 iterations
for epoch in range(1000):
	idx = np.random.choice(len(admit_y), 20, replace=False)
	_, l = sess.run([train, cross_entropy], feed_dict={x:gre_x[idx], y:admit_y[idx]})
	if epoch % 100 == 0:
		print(l)

##################################################
## THE PROBLEM WITH THIS IS IT WILL GIVE NaN.
## Also we shouldn't be writing cost function on our own
## Tf should provide sigmoid function.
## Hence in show1-fix.py I'm doing that.
##################################################


import discord
from datetime import datetime
from discord.ext import commands
from discord.utils import get
import json
from discord.ext import commands
import manageDB#DB 관리하기, 테트리오 API를 사용하여 사용자 정보를 받아오기
import GetLB
import requests

token=open("token.txt","r").readline()
game=discord.Game(' )도움 입력')
bot = commands.Bot(command_prefix=')',intents=discord.Intents.all(),activity=game)

def lbSend(username):#리더보드를 임베드로 반환
    _dict = GetLB.Get(username)
    if(_dict=='no user'):
        embed= discord.Embed(title='유저를 찾을 수 없습니다')
    else:
        embed = discord.Embed(title='**'+_dict['name'].upper()+'**님의 리더보드입니다',color=discord.Color.blue())
        img_url='https://tetr.io/user-content/avatars/'+_dict['_id']+'.jpg'
        if('<Response [404]>'==str(requests.get(img_url))):
            img_url='https://media.discordapp.net/attachments/1096103445024485488/1096103529506164867/logo.png'
        embed.set_thumbnail(url=img_url)
        embed.add_field(name='계정 생성일', value=_dict['ts'], inline=False)
        embed.add_field(name='40LINE 최고 기록', value=str(_dict['40L']//60)+'분 '+str(_dict['40L']%60)+'초', inline=True)
        embed.add_field(name='BLITZ 최고 기록', value=str(_dict['blitz']), inline=False)
        embed.add_field(name='LEAGUE 기록',value='', inline=False)
        embed.add_field(name='플레이 횟수', value=str(_dict['league']['playeds'])+'회', inline=True)
        embed.add_field(name='이긴 횟수', value=str(_dict['league']['wins'])+'회', inline=True)
        embed.add_field(name='RANK', value=str(_dict['league']['rank'])+' rank', inline=True)

    return(embed)

def helpSend():
    embed = discord.Embed(title='**SunrinTETRIO**에서 사용할 수 있는 명령어들 입니다',color=discord.Color.blue())
    embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/1096103445024485488/1096103529506164867/logo.png")
    embed.add_field(name='`)추가 [사용자 이름] [게임 닉네임]`', value='DB에 사용자 정보를 업로드 합니다', inline=False)
    embed.add_field(name='`)리더보드 [게임 닉네임]`', value='게임 리더보드를 간략화하여 출력합니다', inline=False)
    embed.add_field(name='`)순위 [페이지 번호(기본:1)]`', value='선린고의 학생들의 40L기록을 순위로 나열하여 출력 합니다', inline=False)

    return(embed)

def rankSend(pageNum):
    userDB=open(file='./DB/userData.json',mode='r',encoding="UTF-8").read()
    _dict = json.loads(userDB)
    rankLi=manageDB.updateRank()
    embed = discord.Embed(title='선린인고 학생들의 40L 순위입니다.',color=discord.Color.blue())

    for i in range(pageNum*5,pageNum*5+5):
        userCode=rankLi[i][0]
        userRecord=rankLi[i][1]
        if(userRecord==9999):
            break
        embed.add_field(name=str(i+1)+'위', value=_dict[userCode]['name']+'('+_dict[userCode]['nick']+') '+str(userRecord//60)+'분'+str(userRecord%60)+'초', inline=False)

    return(embed)

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')

@bot.command()#리더보드 leaderboard
async def 리더보드(ctx, arg):
    print(datetime.now().strftime,ctx.author,'가',ctx.guild,'에서 리더보드를 사용 ')
    await ctx.send(embed=lbSend(arg))

@bot.command()#DB 업로드 register
async def 추가(ctx,arg,arg2):
    print(datetime.now(),ctx.author,'가',ctx.guild,'에서 추가를 사용 ')
    check=manageDB.AddUser((arg,arg2.lower()))
    if(check=='no user'):
        await ctx.send('유저가 없습니다.')
    elif(check=='overlap user'):
        await ctx.send('이미 추가된 유저입니다')
    else:
        await ctx.send('사용자 정보가 DB에 업로드 되었습니다')

@bot.command()
async def 순위(ctx,arg=0):
    print(datetime.now(),ctx.author,'가',ctx.guild,'에서 순위를 사용 ')
    try:
        if(int(arg)<=0):
            pageNum=0
        else:
            pageNum=int(arg)-1
    except:
        pageNum=0

    await ctx.send(embed=rankSend(pageNum))

@bot.command()
async def 도움(ctx):
    print(datetime.now(),ctx.author,'가',ctx.guild,'에서 도움을 사용 ')
    await ctx.send(embed=helpSend())

bot.run(token)


"""This module contains utilities for interacting with the maya DAG.

Functionality should try to be limited to using om2 only, querying with cmds is allowed with
no performance issues.
"""

import itertools
import logging
import pprint

import maya.cmds as mc
from maya.api import OpenMaya as om2

from ..dg import dg_utils

logger = logging.getLogger(__name__)
_pf = pprint.PrettyPrinter(indent=4).pformat

try:
    from typing import Callable, Dict, Generator, Iterable, List, Union
except:
    pass

# iterators
def idag(root=None, getter='currentItem', filter_type=om2.MFn.kInvalid):
    # type: (om2.MObject, str, om2.MFn) -> Generator[Union[om2.MDagPath, om2.MObject]]
    """Iterate over the maya DAG.

    Examples:
        >>> # iterate on all meshes (MObjects) under one transform
        >>> # from AL.dept.assets.om2utils.dg import dg_utils
        >>> root = dg_utils.as_mObject('|my|transform|node')
        >>> for mobj in idag(root, filter_type=om2.MFn.kMesh):
        ...     print(mobj)

        >>> # iterate on all full paths (strings) for shapes on the scene (mesh, nurbsCurves, etc.)
        >>> for path in idag(getter='fullPathName', filter_type=om2.MFn.kShape):
        ...     print(path)

    Args:
        root (Optional[MObject]): Root of the DAG iteration, if None is given, the world will be used.
        getter (str): Name of the accessor to retrieve on each iteration ('currentItem', 'getPath', etc).
                      Defaults to 'currentItem'.
        filterType: Filters the iterator to a specific type node. Defaults to om2.MFn.kInvalid (access all DAG).

    Yields:
        object: the current requested object on the DAG iteration (defaults to an MObject).
    """
    traversal_type = om2.MItDag.kDepthFirst
    mit = om2.MItDag()
    mit.reset(root or mit.root(), traversal_type, filter_type)

    accessor = getattr(mit, getter)

    while not mit.isDone():
        yield accessor()
        mit.next()

def itransforms_under_root(root=None, **kwargs):
    # type: (om2.MObject, Dict) -> Generator[om2.MObject]
    """Convenience wrapper function to iterate over all transforms under `root` in the DAG.

    Args:
        root (Optional): Root node in the DAG to iterate transforms from. If None is given, all transforms in the
                         scene are yielded. Defaults to None.
        **kwargs: Extra variable keyword arguments to pass on to the iterDAG function object (see help(iterDAG)).

    Yields:
        object: Next transform in the DAG (defaults to an MObject)

    """
    return idag(root, filter_type=om2.MFn.kTransform, **kwargs)

def itransformed_local_pivots(transforms):
    # type: (Iterable[om2.MObject]) -> Generator[Union[om2.MDagPath, om2.MObject]]
    """Iterate over the transforms that have non-zeroed local pivots (user modified pivots, frozen transforms, etc).

    Args:
        transforms (iterable): Iterable containing the transforms to iterate in.

    Yields:
        MObject: Next transform with non zero values on its local pivots.

    Examples:
        >>> # On an empty scene, create three cubes. Move the second and the third. Freeze transforms on the third one.
        >>> transforms = itransforms()  # TODO: This is incorrect...
        >>> identityTtransforms = iter_identity_transforms(transforms)
        >>> frozenTransforms = itransformed_local_pivots(identityTtransforms)
        >>> from maya.api.OpenMaya om2 import MFnDagNode
        >>> [MFnDagNode(t).fullPathName() for t in frozenTransforms]
        [u'|pCube3']

    """
    mdag = om2.MFnDagNode()

    mobj_path = lambda mobj:  mdag.setObject(mobj).fullPathName()
    local_space = lambda mobj: any(mc.xform(mobj_path, piv=True, q=True))

    for transform in itertools.ifilter(local_space, transforms):
        yield transform

def _itransforms_of_identity(transforms, filter_func):
    # type: (Iterable[om2.MObject], Callable) -> Generator[om2.MObject]

    mtrans = om2.MFnTransform()
    identity = lambda mobj: mtrans.setObject(mobj).transformationMatrix().isEquivalent(om2.MMatrix.kIdentity)

    return (t for t in filter_func(identity, transforms))

def itransforms_of_non_identity(transforms):
    # type: (Iterable[om2.MObject]) -> Generator[om2.MObject]
    """Iterate over the transforms that have any transformation values (object matrix is not identity).

    Args:
        transforms (iterable): Iterable containing the transforms to iterate in.

    Yields:
        MObject: Next transform with transformation values.

    Example:
        >>> # On an empty scene, create three cubes. Move the second and the third. Freeze transforms on the third one.
        >>> transforms = itransforms()  # TODO: This is incorrect...
        >>> from maya.api.OpenMaya om2 import MFnDagNode
        >>> [MFnDagNode(t).fullPathName() for t in itransforms_of_non_identity(transforms)]
        [u'|persp', u'|top', u'|front', u'|side', u'|pCube2']
    """
    for transform in _itransforms_of_identity(transforms, itertools.ifilterfalse):
        yield transform

def itransforms_of_identity(transforms):
    # type: (Iterable[om2.MObject]) -> Generator[om2.MObject]
    """Iterate over the transforms with an identity matrix (no transformation values).

    Args:
        transforms (iterable): Iterable containing the transforms to iterate in.

    Yields:
        MObject: Next transform with no transformation values.

    Example:
        >>> # On an empty scene, create three cubes. Move the second and the third. Freeze transforms on the third one.
        >>> transforms = itransforms()
        >>> from maya.api.OpenMaya om2 import MFnDagNode
        >>> [MFnDagNode(t).fullPathName() for t in _itransforms_of_identity(transforms)]
        [u'|pCube1', u'|pCube3']
    """
    for transform in _itransforms_of_identity(transforms, itertools.ifilter):
        yield transform

class IMeshs(object):

    @staticmethod
    def iter(root=None, **kwargs):
        # type: (om2.MObject, Dict) -> Generator[om2.MObject]
        """Convenience wrapper function to iterate over all meshs under `root` in the DAG.

        Args:
            root (Optional): Root node in the DAG to iterate transforms from. If None is given, all transforms in the
                             scene are yielded. Defaults to None.
            **kwargs: Extra variable keyword arguments to pass on to the iterDAG function object (see help(iterDAG)).

        Yields:
            object: Next transform in the DAG (defaults to an MObject)

        """
        return idag(root, filter_type=om2.MFn.kMesh, **kwargs)

    @staticmethod
    def mesh_parents(node_name):
        # type: (str) -> Generator[om2.MObject]

        root = dg_utils.as_mObject(node_name)
        shape_paths = idag(root, getter='getPath', filter_type=om2.MFn.kShape)
        transform_paths = (path.pop().node() for path in shape_paths)

        return transform_paths

class IDag(object):

    _omTypeMap = {getattr(om2.MFn, n): n for n in dir(om2.MFn)}

    _omTypeInts = [i for i in _omTypeMap.iterkeys()]
    _omTypeStrs = [n for n in _omTypeMap.iteritems()]
    _omTypeIntToType = lambda i: IDag._omTypeMap[i]

    def __init__(self, root=None, *args, **kwargs):
        # type: (Union[str, om2.MObject , om2.MFn], List, Dict) -> None

        self._root = dg_utils.as_mObject(root)
        self._getter = 'currentItem'
        self._traversal_type = om2.MItDag.kDepthFirst
        self._traversal_depth_max = 0  # Traverse trees till exhausted.
        self._yield_only_at_depth = 0  # Yield nodes at specified depth
        self._filter_types = [om2.MFn.kInvalid]

    def doIt(self):

        mit = om2.MItDag()

        root = self._root or mit.root()
        traversal_type = self._traversal_type
        limit = self._traversal_depth_max

        if self._yield_only_at_depth:
            traversal_type = om2.MItDag.kBreadthFirst
            limit = self._yield_only_at_depth + 1

        if len(self._filter_types) == 1:
            mit.reset(root, traversal_type, self._filter_types[0])

        else:
            mItr = om2.MIteratorType()
            mItr.filterList = self._filter_types
            mit.reset(mItr, root, traversal_type)

        accessor = getattr(mit, self._getter)

        # logger.debug('root: "{}"'.format(dg_utils.get_fullpath(root)))
        # logger.debug('IDag.doIt() - locals()\n{}'.format(_pf(locals())))

        while not mit.isDone():
            logger.debug('@depth: {}, {}'.format(mit.depth(), mit.fullPathName()))

            # Handle if traversal of tree has depth limit.
            if limit and mit.depth() >= limit:
                logger.debug('Traversal depth limit ({}) reached for "{}"'
                             .format(limit - 1, dg_utils.get_fullpath(self._root)))
                break

            # Handle if yielding node at a specified depth.
            if self._yield_only_at_depth and mit.depth() < self._yield_only_at_depth:
                mit.next()
                continue

            # Finally yield the node.
            yield accessor()
            mit.next()

    @property
    def root(self):
        # type: () -> Union[str, om2.MObject , om2.MFn]
        return self._root

    @root.setter
    def root(self, value):
        # type: (Union[om2.MItDag.kDepthFirst, om2.MItDag.kBreadthFirst]) -> None
        self._root = dg_utils.as_mObject(value)

    @property
    def getter(self):
        # type: () -> Union['currentItem', 'getPath', 'fullPathName', 'getAllPaths',  'partialPathName']
        return self._getter

    @getter.setter
    def getter(self, value):
        # type: (Union['currentItem', 'getPath', 'fullPathName', 'getAllPaths', 'partialPathName']) -> None
        self._getter = value

    @property
    def filter_types(self):
        # type: () -> (Iterable[om2.MFn])
        return self._filter_types

    @filter_types.setter
    def filter_types(self, values):
        # type: (Iterable[om2.MFn]) -> None
        assert isinstance(values, list)
        msg = 'Provided "filter_types" are invalid, "{}"'.format(values)
        assert all(lambda v: v in IDag._omTypeInts for v in values), msg

        self._filter_types = values

    @property
    def traversal_type(self):
        # type: () -> Union[om2.MItDag.kDepthFirst, om2.MItDag.kBreadthFirst]
        return self._traversal_type

    @traversal_type.setter
    def traversal_type(self, value):
        # type: (Union[om2.MItDag.kDepthFirst, om2.MItDag.kBreadthFirst]) -> None
        self._traversal_type = value

    @property
    def traversal_depth_max(self):
        # type: () -> int
        return self._traversal_depth_max

    @traversal_depth_max.setter
    def traversal_depth_max(self, value):
        # type: (int) -> None
        self._traversal_depth_max = value

    @property
    def yield_only_at_depth(self):
        # type: () -> int
        return self._yield_only_at_depth

    @yield_only_at_depth.setter
    def yield_only_at_depth(self, value):
        # type: (int) -> None
        self._yield_only_at_depth = value


from __future__ import print_function
import ConfigParser
import os
import time
import numpy as np
import matplotlib.pylab as plt
from oct2py import octave
from splitRecording.recordingSplitter import findApsPeriod, findApsTypeFromFilename
from scoreMatrices.matricesPuntajes import calculateScoreMatrix
from alertSignalAndScenarios.scenarios import getScenery

def leakyIntegrator(x, lambdaVar=0.97):
    N = len(x)  # numero de entradas
    y = [0.0] * N  # la senial de salida
    yAnt = 0
    for n in range(0, N):
        y[n] = lambdaVar * yAnt + (1 - lambdaVar) * x[n]
        yAnt = y[n]
    return np.array(y)

def movingAverage(x, K=30):
    if 0 < K:
        N = len(x)
        y = [0.0] * N
        xp = np.append([0.0] * K, x)
        for n in range(0, N):
            mean = 0
            for k in range(0, K):
                mean += xp[n - k]
            mean /= K
            y[n] = mean
        return y
    else:
        return x

def getScoreSignal(filepath, fRange, df, kernelType, nArm, NFFT=256):
    [D, f0s, fsp, ss] = calculateScoreMatrix(filepath, NFFT, fRange, df, kernelType, nArm)
    dim = 1
    return [fsp, np.max(ss, dim)]

def applyTs2Means(x, dt, wMin, wMax):
    octave.addpath(os.path.dirname(os.path.realpath(__file__)))
    return octave.ts2meansHack(x, dt, [wMin, wMax])

def calculateScoreSignalAndPlot(filepath, df, fRange, kernelType, nArm, apsType, filtering, forgetFactor=None,
                                outpath=None, verbose=False):
    # get the rest of parameters
    filename = filepath.split('/')[-1]
    T = findApsPeriod(apsType)

    # calculate the score signal
    tInicio = time.time()
    [fsp, x] = getScoreSignal(filepath, fRange, df, kernelType, nArm)
    if verbose:
        print('Elapsed time: %f (s)' % (time.time() - tInicio))
    dt = 1.0 / fsp
    N = len(x)
    t = np.linspace(0, N * dt, N)
    plt.figure()
    plt.plot(t, x, label='Crudo')
    plt.title('%s, T=%.2fs' % (filename, T))
    plt.xlabel('Tiempo (s)')
    plt.ylabel('Puntaje')

    # ask for the type of filtering
    if filtering == 'ts2means':
        # apply the filtering using TS2Means
        wMin = T
        wMax = 2.0 * T
        xp = applyTs2Means(x, dt, wMin, wMax)
        plotLabel = filtering
    elif filtering == 'leakyInt':
        xp = leakyIntegrator(x, forgetFactor)
        plotLabel = '%s (%.3f)' % (filtering, forgetFactor)
    else:
        xp = x
        plotLabel = 'None'

    # plot the result
    plt.plot(t, xp, label=plotLabel)
    plt.xlabel('Tiempo (s)')
    plt.ylabel('Puntaje')
    plt.legend()

    # save plot or display in screen?
    if outpath is None:
        plt.show()
    else:
        plt.savefig(outpath)

def run(outdirPath=None):
    # save current working directory and change to script's directory
    cwd = os.getcwd()
    os.chdir(os.path.dirname(os.path.realpath(__file__)))

    configParser = ConfigParser.RawConfigParser()
    configParser.read(r'../configurations.properties')
    snrs = {
        'bajo': {
            'filepath': configParser.get('output', 'noisyContourRecordingPath'),
            'apsType': 'lowchirp'
        },
        'alto': {
            'filepath': configParser.get('output', 'clearContourRecordingPath'),
            'apsType': 'lowchirp'
        }
    }

    # create output folder if it does not exist
    if outdirPath is not None and not os.path.exists(outdirPath):
        os.makedirs(outdirPath)

    for sceneryID in ['scenery7', 'scenery8']:
        scenery = getScenery(sceneryID)
        for snr in snrs.keys():
            # get filepath and aps type
            filePath = snrs[snr]['filepath']
            apsType = snrs[snr]['apsType']

            # get scenery and its parameters
            df = scenery[apsType]['df']
            fRange = scenery[apsType]['fRange']
            kernelType = scenery[apsType]['tipoNucleo']
            numHarmonics = scenery[apsType]['nArmonicas']
            filtering = scenery[apsType]['thresholdMethod']
            forgetFactor = scenery[apsType]['forgetFactor'] if sceneryID == 'scenery8' else None

            if outdirPath is not None:
                outfilename = '%s/%s-%s-%s.pdf' % (outdirPath, apsType, filtering, snr)
            else:
                outfilename = None

            calculateScoreSignalAndPlot(filePath, df, fRange, kernelType, numHarmonics, apsType, filtering,
                                        forgetFactor, outfilename)

    os.chdir(cwd)  # restore directory

# testing
if __name__ == "__main__":
    run()


__author__ = 'florian-strub'
import os
import pickle
import collections
import tensorflow as tf

from GarnetNetwork import GarnetNetwork
from GarnetDataset import GarnetDataset
from Garnet import Garnet
from tools import *
from argparse import Namespace

import matplotlib.pyplot as plt

Dataset = collections.namedtuple('Dataset', ['train', 'test', 'eval'])

params = Namespace(

    Ns=100,
    Na=5,
    Nb=1,

    noPlayer=2,

    gamma=0.9,

    garnet="S_linear_T2_zero",
    sparsity=0.5,

    coefSample=5,

    Qlayers=[80],
    Pilayers=[80],

    Qregu=1e-7,
    Pregu=1e-7,
    dropout=1,

    Q_lrt=0.001,
    Pi_lrt=5e-05,

    epoch=5000,
    batchSize=20,

    gpu=1,
    outDir=".",
    seed=-1,
)

def start(params, garnet=None, dataset=None):
    print (params)

    no_samples = params.Ns * params.Na * params.coefSample

    if params.seed > -1:
        np.random.seed(params.seed)
        rd.seed(params.seed)
        tf.set_random_seed(params.seed)

    ##############################################
    # Build training dataset

    if garnet is None:
        garnet = Garnet(params.Ns, params.Na, params.Nb, params.noPlayer, params.sparsity, params.garnet)

        if dataset is None:
            dataset = Dataset(
                train=GarnetDataset(Ns=params.Ns, Na=params.Na, samples=garnet.uniform_batch_data(no_samples)),
                test=GarnetDataset(Ns=params.Ns, Na=params.Na, samples=garnet.uniform_batch_data(params.Ns * params.Na)),
                eval=GarnetDataset(Ns=params.Ns, Na=params.Na, samples=garnet.eval_batch_data()),
            )

        print ("Starting points")
        print (garnet.start)

    #############################################
    # Compute random policy

    policy_random = (1.0 * np.ones((garnet.Ns, garnet.Na))) / (1.0 * garnet.Na)
    err_random = garnet.l2errorDiffQstarQpi(policy_random, params.gamma)
    print ("Error random policy Bellman")
    print (err_random)
    print ("")

    #############################################
    # build network

    Qlayers = [params.Ns] + params.Qlayers + [params.Na]
    Pilayers = [params.Ns] + params.Pilayers + [params.Na]

    print (["Qlayers  : ", Qlayers])
    print (["Pilayers : ", Pilayers])

    fApp = GarnetNetwork(qLayerSize=Qlayers, policyLayerSize=Pilayers, params=params)
    plots = fApp.execute(dataset=dataset, nEpoch=params.epoch, nMiniBatch=params.batchSize, garnet=garnet, gamma=params.gamma)

    #############################################
    # plot results

    print("Final Error random policy Bellman")
    print (err_random)

    res = {}
    res["garnet"] = garnet
    res["params"] = params
    res["plots"] = plots

    with open(os.path.join(params.outDir, "data.p"), "wb") as file:
        pickle.dump(res, file=file)

    # plot with various axes scales

    x = range(0, len(plots["errResidual"]["train"]))

    # Two subplots, the axes array is 1-d
    fig, axarr = plt.subplots(3, sharex=True)

    axarr[0].set_title(str(params))

    axarr[0].plot(x, np.log10(plots["errResidual"]["train"]), color='b', linestyle='-')
    axarr[0].plot(x, np.log10(plots["errResidual"]["test"]), color='r', linestyle='-')
    axarr[0].legend(["Train residual Error", "Test residual Error"])

    colors = ["b", "k", "r", "g", "y", "m"]
    legends = []
    for player in range(params.noPlayer):
        axarr[1].plot(x, plots["errPolicy"][player], color=colors[player], linestyle='-')
        axarr[1].plot(x, [err_random[player]] * len(x), color=colors[player], linestyle='--')

        legends += ["NN policy " + str(player), "RND policy " + str(player)]

    axarr[1].legend(legends)
    axarr[1].set_ylim([0.0, 1.0])

    actions = np.array(plots["histoAction"])
    legend = []
    for i in range(params.Na):
        axarr[2].plot(x, actions[:, i])
        legend.append('action ' + str(i))
    axarr[2].legend(legend)
    axarr[2].set_ylim([0.0, 1.0])

    fig.savefig(params.outDir + "/training.eps")
    plt.close(fig)

    return plots

if __name__ == '__main__':
    start(params)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr  1 12:36:05 2018

@author: rita
"""

import requests
import os
import re
#import random
from bs4 import BeautifulSoup as bs

def wiki_down(url = 'https://en.wikipedia.org/wiki/Special:Random', wiki_files = paths.get('wiki_path')):

    """
    Function to download Wikipedia articles, clean them superficially
    and save them in a folder stored in my computer.

    If the url is not specified, it chooses a random article, thanks to Wikipedia's API
    """

    r = requests.get(url)
    soup = bs(r.text, 'html.parser')
    file_name = re.sub(r'\-', r'', re.sub(r'\s', r'_', str(soup.title.string).lower())) + '.txt'
    file_name = re.sub(r'\/', r'_', file_name)

    article = soup.get_text()

    # Finds where the actual article starts and removes anything being displayed before that:
    article = article.split('From Wikipedia, the free encyclopedia')[1]
    # Finds where the actual article ends and removes anything being displayed after that:
    article = article.split('References[edit]')[0]

    article = re.sub(r'\[\d*\]','', article)
    article = re.sub(r'\[(edit)\]','', article)
    article = re.sub(u'\n', ' ', article)
    article = re.sub(r'\s+', ' ', article)

    with open(os.path.join(wiki_files, file_name), 'w') as f:
        f.write(article)

num_articles = 10000
for i in tqdm(range(num_articles)):
    wiki_down()


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import os

# Set up the Selenium WebDriver with the Chrome browser
options = webdriver.ChromeOptions()
options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), chrome_options=options)

def vote():
    try:
        # Navigate to the top.gg voting page for your desired bot
        driver.get("https://top.gg/bot/<bot-id>/vote")

        # Find the vote button and click it
        vote_button = driver.find_element_by_xpath("//button[contains(text(), 'Vote')]")
        vote_button.click()

        # Wait for the page to load and verify that the vote was successful
        time.sleep(5)
        assert "Voted" in driver.page_source

        # Print success message
        print("Vote successful!")
    except Exception as e:
        print(e)

while True:
    # Call the vote function
    vote()

    # Wait for 12 hours before voting again
    time.sleep(12 * 60 * 60)


import os
import json
from pickle import TRUE
import urllib
import codecs
import webapp2
from google.appengine.ext.webapp import template

class MainPage(webapp2.RequestHandler):
    def get(self):
        template_values = {}
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

    def post(self):
        pincode = self.request.get('zipCode')
        url = "https://api.postalpincode.in/pincode/"+ pincode
        data = urllib.urlopen(url).read()
        data = json.loads(data)
        Status = data[0]['Status']

        if(Status=='Success'):

            post_office = data[0]['PostOffice'][0]['State']
            name = data[0]['PostOffice'][0]['Name']
            block = data[0]['PostOffice'][0]['Block']
            district = data[0]['PostOffice'][0]['District']
            template_values = {
                "post_office": post_office,
                "name": name,
                "block": block,
                "district": district
            }
            path = os.path.join(os.path.dirname(__file__), 'results.html')
            self.response.out.write(template.render(path, template_values))
        else:
            #url = os.path.join(os.path.dirname(__file__), 'error.html')
            INDEX_HTML = open('error.html').read()
            self.response.out.write(INDEX_HTML)

app = webapp2.WSGIApplication([('/', MainPage)], debug=True)


import numpy as np
import pandas as pd
pd.set_option('display.max_columns',34)
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go

from sklearn.model_selection import train_test_split
from sklearn import neighbors
from sklearn.neighbors import KNeighborsRegressor, KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.cluster import KMeans
from sklearn.metrics import accuracy_score, mean_absolute_error, mean_squared_error, r2_score, confusion_matrix, ConfusionMatrixDisplay
from sklearn.preprocessing import StandardScaler, MinMaxScaler, OneHotEncoder
from sklearn.ensemble import RandomForestRegressor, AdaBoostRegressor
from sklearn.linear_model import LogisticRegression, LinearRegression, Ridge, Lasso
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline, Pipeline

import streamlit as st
from streamlit_option_menu import option_menu

st.set_option('deprecation.showPyplotGlobalUse', False)
st.set_page_config(layout="wide")

data = pd.read_csv('data.csv')
data.pop('Unnamed: 0')
data = data.rename(columns = {'Type of ownership' : 'Type of Ownership',
                      'min_salary' : 'Min. Salary',
                      'max_salary' : 'Max. Salary',
                      'avg_salary' : 'Avg. Salary',
                      'job_state' : 'Job State',
                      'same_state' : 'Same State',
                      'age' : 'Age',
                      'python_yn' : 'Python Exp.',
                      'R_yn' : 'R Exp.',
                      'spark' : 'Spark Exp.',
                      'aws' : 'AWS Exp.',
                      'excel' : 'Excel Exp.',
                      'job_simp' : 'Title Simplified',
                     'Headquarters' : 'HQ',
                     'hourly' : 'Hourly',
                     'desc_len' : 'Description Length',
                     'num_comp' : '# of Competitors',
                     'employer_provided' : 'Employer Provided',
                     'seniority' : 'Seniority',
                     })
data.pop('R Exp.')
data['Title Simplified'] = data['Title Simplified'].str.title()
data['Title Simplified'] = data['Title Simplified'].str.replace('Mle', 'MLE')
data['Min. Salary'] = data['Min. Salary']*1000
data['Max. Salary'] = data['Max. Salary']*1000
data['Avg. Salary'] = data['Avg. Salary']*1000
data.replace({"Na": np.nan, "NaN": np.nan, "-1": np.nan, "Unknown / Non-Applicable": np.nan}, inplace=True)
data = data.drop(['Competitors', '# of Competitors'], axis=1)
data['Type of Ownership'] = data['Type of Ownership'].replace('Unknown', 'Na')
data_orig = data
columns_to_drop = ['Seniority' , 'Job Title', 'Salary Estimate', 'Job Description', 'Company Name', 'Location', 'HQ', 'Industry', 'Hourly', 'Same State', 'Seniority', 'Employer Provided', 'company_txt']

df = data.drop(columns=columns_to_drop)
df_orig = df

#removing data
df = df.loc[(df['Age'] >= 18) & (df['Age'] <= 120)]
df = df[df['Min. Salary'] > 30000]

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

from PIL import Image
image = Image.open('image2.png')

###############################################################################################################################################
###############################################################################################################################################
with st.sidebar:
    selected = option_menu(
        menu_title="Menu",
        options=['Home', 'EDA', 'Salary Analysis', 'Salary Prediction', 'Conclusion','About Me'],
        icons= ['house-door-fill' , 'rocket-takeoff-fill', 'graph-up', 'layers-fill', 'piggy-bank-fill' ]
    )

###############################################################################################################################################
###############################################################################################################################################
if selected == 'Home':
    st.image(image, width=800)
    st.write("""
    We long gone past the time when we once lived where every action required user input and the view on how math subjects can be applicable outside of what is required while in school.  Thanks to every growing field on Artificial Intelligence (AI) and Machine Learning (ML) and the well renowned [article published in Harvard Business Review](https://hbr.org/2012/10/data-scientist-the-sexiest-job-of-the-21st-century) back in 2012, which called Data Science (DS) the sexiest job of the 21st century. As businesses continue to seek ways effectively access trends (from present data, reviewing trends from past data and accurately predicting future trends), satisfy the need of customers while operating in the most efficient way possible.

    It is widely known that pursing a career in Data Science can be rewarding in terms of income, while that may be the case as the great saying goes 'More money, more problems'. This purpose of this app will explore through the job listings from a Glassdoor back in 2016 and observe certain trends in salaries, location, satisfaction of a position, the abundance of positions and see how they all relate to one another.""")

###############################################################################################################################################
###############################################################################################################################################
if selected == 'EDA':
    st.image(image, width=800)
    st.title('Exploratior Data Analysis')

    col1, col2 = st.columns([4, 2])
    with col1:
        with st.expander('**View Data**'):
            st.write(df)
            with col2:
                with st.expander('**View Data**'):
                    st.subheader('Summary Statistics of Job Postings')
                    st.write(df.describe())

    tab1, tab2, tab3, tab4, tab5 = st.tabs(['**Description**', '**Simple Data Review**', '**Map Visualization**', '**Correlations**', '**Conclusion**'])

    with tab2:
        st.title('Dataset Exploratation')
        col1, col2 = st.columns([4, 1])
        with col1:
            pursue_ds_career = st.radio('**How sure are you that you want to pursue a career in Data Science**', ['ehh maybe...', '**Very Certain❕**'], horizontal=True)
            if pursue_ds_career == 'ehh maybe...':
                st.write(df.head())
                st.write('#')
                st.subheader('Summary statistics of a ***few*** job postings')
                st.write(df.head().describe())
            else:
                st.write(df)
                st.write('#')
                st.subheader('Summary statistics of **all** job postings')
                st.write(df.describe())
        with col2:
            select = st.checkbox('Show Description of Features')
            if select:
                st.write('''
                Founded: Year company was founded Job

                State: The state where the job is located

                Same State: An indicator of whether the job is in the same state as the person looking at the job

                Age: The age of the person looking at the job

                Python Exp.: An Indicator of whether the person looking at the job knows Python

                R Exp.: An indicator of whether the person looking at the job knows R

                Spark Exp.: An indicator of whether the person looking at the job knows Spark

                AWS Exp.: An Indicator of whether the person looking at the job knows AWS

                Excel Exp.: An indicator of whether the person looking at the job knows Excel

                Title Simplified: A simplified job title

                HQ: Location of Headquarters

                Hourly: An indicator of whether the person will be paid hourly

                Description Length: A count of total number of characters in the job posting

                **MLE: Machine Learning Engineer''')

        small_vis = st.radio('**Quick visuals**', ['Distributions', 'Coding Skillset'], horizontal=True)
        if small_vis == 'Distributions':
            st.write('''
            The message mentions that there is a bias in job openings, specifically in the context of Data Scientist positions and the type of ownership field being a private company. This suggests that there might be more job openings for Data Scientists in private companies compared to other roles. It can be seen that there are roughly 66% more Data Science job openings than the next option. This indicates that Data Scientist roles are abundant in the dataset.''')
            col1, col2 = st.columns(2)
            with col1:
                lst = ['Rating', 'Size', 'Founded', 'Age', 'Type of Ownership', 'Sector', 'Revenue', 'Title Simplified', 'Job State']
                #color_sel = st.sidebar.selectbox('Sorting Options', df.columns)
                st.sidebar.title('''Fig. 1A: Distribution of Features''')
                sel = st.sidebar.selectbox('Features', sorted(lst), index=7)

                if sel:
                    color_mapping = {
                        value: color
                        for value, color in zip(df[sel].unique(), px.colors.qualitative.Set1)}
                    df['Color'] = df[sel].map(color_mapping)
                    fig = px.histogram(df, x=sel, color=sel)
                    fig.update_layout(xaxis=dict(showgrid=False), yaxis=dict(showgrid=False, title_text='Count'), title_text=f'Distribution for {sel}')
                    st.plotly_chart(fig, use_container_width=True)
                    st.title('Fig. 1A: Distribution of Features')
                    with col2:
                        fig_pie = px.pie(df, names=sel, title=f'Pie chart for {sel}')
                        fig_pie.update_traces(textposition='inside', textinfo='percent+label')
                        st.plotly_chart(fig_pie)
        else:
            code_exp = df.groupby('Title Simplified')[['Python Exp.', 'Spark Exp.', 'AWS Exp.', 'Excel Exp.']].sum().drop_duplicates()
            st.sidebar.title('''Fig. 1B: Distribution of Coding Skillset''')
            code_sel = st.sidebar.selectbox('How does your coding experience help', code_exp.index)
            selected_data = code_exp.loc[code_sel]
            sorted_data = selected_data.sort_values()
            colors = ['crimson' if x == sorted_data.min() else 'green' if x == sorted_data.max() else 'lightslategray' for x in sorted_data]
            st.write('''
            Now you can observe the distributions which coding languages were idenitifed in the job listing for each of the simplified job titles. The data shows the experience in older languages, such as python and excel, are used more. Also, there are certain positions that require a knowledge in multiple skillsets.
            The column which is green identifies the coding language that was identified most often, column which is red/no color identifies the coding language that was identified most often.
            ''')
            fig = go.Figure(data=[go.Bar(
                x=sorted_data.index,
                y=sorted_data,
                marker_color=colors)])
            fig.update_layout(xaxis=dict(showgrid=False, title_text=f'Coding Experience for {code_sel}'), yaxis=dict(showgrid=False, title_text='Count'), title_text=f'Distribution of Coding Skillset Required for {code_sel} Position')
            st.plotly_chart(fig)
            st.title('''Fig. 1B: Distribution of Coding Skillset''')

###############################################################################################################################################
###############################################################################################################################################
if selected == 'Salary Analysis':
    st.image(image, width=800)
    st.title('Salary Analysis')

    col1, col2 = st.columns([4,2])
    with col1:
        num_feats = data.select_dtypes(include=['number']).columns
        num_feats = num_feats.drop(['Hourly', 'Employer Provided', 'Same State', 'Avg. Salary','Python Exp.', 'Spark Exp.', 'AWS Exp.', 'Excel Exp.'])
        number_of_feats = st.slider('Select the # of Features to Reflect Prediction', 1,3,1)
        df_cols = df.columns.drop(['Python Exp.', 'Spark Exp.', 'AWS Exp.', 'Excel Exp.'])
        available_feats =  num_feats.tolist()
        selected_feats = []

        for i in range(number_of_feats):
            if available_feats:
                feature_name = st.selectbox(f'Select Feature #{i + 1}', options=available_feats, key=i)
                selected_feats.append(feature_name)
                available_feats = [feat for feat in available_feats if feat != feature_name]
            else:
                st.warning('No more features available to select.')
                break
    with col2:
        code_skills = st.multiselect(
        'Select all coding languages you are familiar with:',
        ['Python', 'Spark', 'AWS', 'Excel'],
        placeholder="Choose an option")

    y = df['Avg. Salary']
    X = df[selected_feats]
    test_fraction = 0.2
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_fraction)

    model_options = ["Linear Regression", "Ridge", "Lasso"]
    selected_model = st.selectbox("Select a regression model:", model_options)

    model_dict = {
        "Linear Regression": LinearRegression(),
        "Ridge": Ridge(),
        "Lasso": Lasso()
    }

    model = model_dict[selected_model]
    model.fit(X_train, y_train)
    y_pred = model.predict(X_test)

    def evaluate_model (true, predicted):
        mae = mean_absolute_error(true, predicted)
        mse = mean_squared_error(true, predicted)
        rmse = np.sqrt (mean_squared_error (true, predicted))
        r2_square = r2_score (true, predicted)
        return mae, mse, rmse, r2_square
    mae, mse, rmse, r2 = evaluate_model(y_test, y_pred)

    fig = go.Figure()
    fig.add_trace(go.Scatter(x=np.arange(len(y_test)), y=y_test, mode='markers', name='Actual'))
    fig.add_trace(go.Scatter(x=np.arange(len(y_pred)), y=y_pred, mode='markers', name='Prediction'))
    fig.update_layout(title="Actual vs Predicted Salary Values", xaxis_title="Index", yaxis_title="Values")
    fig.update_traces(marker=dict(size=8,  symbol="diamond",
                                line=dict(width=2,
                                            color='DarkSlateGrey')),
                    selector=dict(mode='markers'))
    st.plotly_chart(fig)
    st.metric(label="Mean Absolute Error (MAE)", value=f"{mae:.2f}")
    #st.metric(label="Mean Squared Error (MSE)", value=f"{mse:.2f}")
    st.metric(label="Root Mean Squared Error (RMSE)", value=f"{rmse:.2f}")
    st.metric(label="R-Squared (R2)", value=f"{r2:.2f}")

###############################################################################################################################################
###############################################################################################################################################
if selected == 'Salary Prediction':
    st.title('')
    st.header("Let's Predict Your Salary")

    #jt = data['Title Simplified'].tolsit()
    st.subheader('What is simplified job tilie you are seeking?')

    job_title = st.selectbox(
        "What job title best reflects your daily work?",
        ['Data Scientist', 'Analyst', 'Data Engineer', 'Director',
        'Manager', 'Machine Learning Engineer'])

    comp_size = st.selectbox(
        "How many people are you hoping to have within your new company?",
        ['501 to 1000 employees', '10000+ employees',
        '1001 to 5000 employees', '201 to 500 employees',
        '5001 to 10000 employees', '51 to 200 employees',
        '1 to 50 employees'])

    top = st.selectbox(
        "What type of ownership are you interested in?",
    ['Company - Private', 'Other Organization', 'Government',
        'Company - Public', 'Nonprofit Organization',
        'Subsidiary or Business Segment'])

    pick_sector = st.selectbox(
        "Which sector are you interested in being apart of?",
    ['Aerospace & Defense', 'Health Care',
        'Oil, Gas, Energy & Utilities', 'Real Estate', 'Business Services',
        'Retail', 'Insurance', 'Transportation & Logistics', 'Finance',
        'Biotech & Pharmaceuticals', 'Telecommunications',
        'Information Technology', 'Manufacturing', 'Government',
        'Agriculture & Forestry', 'Education',
        'Arts, Entertainment & Recreation', 'Travel & Tourism', 'Media',
        'Non-Profit'])

    work_loc   = st.selectbox(
        "Ideally what state would you work in?",
    ['NM', 'MD', 'WA', 'TX', 'VA', 'CO', 'KY', 'OR', 'MI', 'MA', 'CA',
        'NY', 'IL', 'AL', 'PA', 'GA', 'WI', 'NC', 'IN', 'MN', 'DC', 'OH',
        'TN', 'NJ', 'MO', 'ID', 'IA', 'FL', 'KS', 'UT', 'AZ'])

    predictors = ['Title Simplified', 'Size', 'Type of Ownership', 'Sector', 'Job State']
    target = 'Avg. Salary'
    y = df[target]
    X = df[predictors]
    test_fraction = 0.2
    X_train, X_test, y_train, y_test = train_test_split(df[predictors], df[target], test_size=0.2, random_state=42)

    categorical_features = ['Title Simplified', 'Size', 'Type of Ownership', 'Sector', 'Job State']
    categorical_transformer = OneHotEncoder(handle_unknown='ignore')

    preprocessor = ColumnTransformer(
        transformers=[
            ('cat', categorical_transformer, categorical_features)
        ])

    model = Pipeline(steps=[('preprocessor', preprocessor),
                            ('regressor', LinearRegression())])

    model.fit(X_train, y_train)

    y_pred = model.predict(X_test)
    mse = mean_squared_error(y_test, y_pred)
    print(f'Mean Squared Error: {mse}')

    def predict_salary(job_title, comp_size, ownership_type, sector, job_state):
        input_data = pd.DataFrame({
            'Title Simplified': [job_title],
            'Size': [comp_size],
            'Type of Ownership': [ownership_type],
            'Sector': [sector],
            'Job State': [job_state]})
        predicted_salary = model.predict(input_data)
        return predicted_salary[0]

    image2 = Image.open('IMG_0370.png')
    if st.button("Predict Salary"):
        predicted_salary = predict_salary(job_title, comp_size, top, pick_sector, work_loc)
        st.write(f"Predicted Salary: ${predicted_salary:,.2f}")
        st.image(image2, width=100)

###############################################################################################################################################
###############################################################################################################################################
if selected == 'Conclusion':
    st.title('Conclusion')

###############################################################################################################################################
###############################################################################################################################################
if selected == 'About Me':
    st.title('Sharod Williams')

    col1, col2 = st.columns([3,2])

    with col1:
        image = Image.open('image2.png')
        st.image(image, width=600)
        st.title('Conclusion')
        st.write('''Throughout the exploration of this dataset certain factors have been analyzed to support your journey as you try to find the data science position that is right for you. It was also shown that the more money you make in a position equaites to having a higher level of happiness. This process included reviewal of underlying commonalities in the listings, an evaluation of different visualizations was made to help make this easier, including the comparing and contrasting of the relationship agamous Salary, Employee Satisfaction and their Location. I plan to further investigate this dataset by conducting testing and training of the data, this will allow for better accurate imputation of the missing data.''')
    with col2:
        image2 = Image.open('IMG_0370.png')
        st.image(image2, width=300)
        st.write('Committed to using computational techniques to solve community problems, furthering my professional development through enrolling in courses and completing projects, while working independently and collaboratively as a data scientist/bioinformatician.')

    #

    #st.set_page_config(page_title = 'Analysis of Data Scientist Openings', page_icon = '💰', layout='wide')


# -*- coding: utf-8 -*-
import functools
import typing
from urllib.parse import quote

from django.http.request import HttpRequest
from django.urls import reverse, NoReverseMatch
from django.utils.translation import gettext as _

from ..models import Event, EventPermission, UserAdapter

class MenuItem(typing.NamedTuple):
    name: typing.Optional[str]
    url: typing.Optional[str]
    active: typing.Optional[bool]
    sub_items: typing.Optional[typing.List["MenuItem"]]

    @staticmethod
    def separator():
        return MenuItem(None, None, None, None)

def _fill(event: Event, active: str, name, func, sub=None, query=None, is_global=False):
    if not is_global:
        kwargs = {"event_slug": event.slug}
    else:
        kwargs = {}
    link = reverse(func, kwargs=kwargs) if func else None
    if query:
        link += "?" + "&".join(
            quote(k, safe="") + (("=" + quote(v, safe="")) if v else "")
            for k, v in query.items())
    return MenuItem(name, link, func == active, sub)

def management_menu(
        request: HttpRequest,
        event: Event,
        permissions: typing.Optional[EventPermission] = None
) -> typing.List[MenuItem]:
    fill = functools.partial(_fill, event, request.resolver_match.view_name)

    manage_sub: typing.List[MenuItem] = []
    permissions = permissions or EventPermission.get(event, request.user)

    if not event.source_db and (request.user.is_staff or UserAdapter.is_clerk(request.user, event)):
        manage_sub.append(fill(_(u"Checkout commands"), "kirppu:commands"))
        if event.checkout_active:
            manage_sub.append(fill(_(u"Checkout"), "kirppu:checkout_view"))
            if event.use_boxes:
                manage_sub.append(fill(_("Box codes"), "kirppu:box_codes"))

    if not event.source_db and (request.user.is_staff or permissions.can_see_clerk_codes):
        manage_sub.append(fill(_(u"Clerk codes"), "kirppu:clerks"))

    if not event.source_db and (request.user.is_staff or permissions.can_see_accounting):
        manage_sub.append(fill(_(u"Lost and Found"), "kirppu:lost_and_found"))

    if request.user.is_staff \
            or UserAdapter.is_clerk(request.user, event) \
            or permissions.can_see_statistics:
        manage_sub.append(fill(_(u"Statistics"), "kirppu:stats_view"))

    if permissions.can_manage_event or request.user.is_superuser:
        manage_sub.append(fill(_(u"Manage event"), "kirppu:manage_event"))

    if request.user.is_staff:
        try:
            manage_sub.append(fill(_(u"Site administration"), "admin:index", is_global=True))
        except NoReverseMatch as e:
            pass

    return manage_sub

def accounting_menu(
        request: HttpRequest,
        event: Event,
        permissions: typing.Optional[EventPermission] = None
) -> typing.Optional[typing.List[MenuItem]]:
    fill = functools.partial(_fill, event, request.resolver_match.view_name)

    permissions = permissions or EventPermission.get(event, request.user)

    if permissions.can_see_accounting:
        return [
            fill(_("View"), "kirppu:accounting"),
            fill(_("Download"), "kirppu:accounting", query={"download": ""}),
            MenuItem.separator(),
            fill(_("View items"), "kirppu:item_dump", query={"txt": ""}),
            fill(_("View items (CSV)"), "kirppu:item_dump"),
            MenuItem.separator(),
            fill(_("Accounts"), "kirppu:live_accounts"),
        ]

def vendor_menu(request: HttpRequest, event: Event) -> typing.List[MenuItem]:
    """
    Generate menu for Vendor views.
    Returned tuple contains entries for the menu, each entry containing a
    name, url, and flag indicating whether the entry is currently active
    or not.

    :param event: The event.
    :param request: Current request being processed.
    :return: List of menu items containing name, url and active fields.
    """
    fill = functools.partial(_fill, event, request.resolver_match.view_name)

    items = [
        fill(_(u"Home"), "kirppu:vendor_view"),
    ]

    if not event.source_db:
        items.append(fill(_(u"Item list"), "kirppu:page"))
        if event.use_boxes:
            items.append(fill(_(u"Box list"), "kirppu:vendor_boxes"))

    if event.mobile_view_visible:
        items.append(fill(_("Mobile"), "kirppu:mobile"))

    permissions = EventPermission.get(event, request.user)

    manage_sub = management_menu(request, event, permissions=permissions)
    if manage_sub:
        items.append(fill(_(u"Management"), "", manage_sub))

    accounting_sub = accounting_menu(request, event, permissions=permissions)
    if accounting_sub:
        items.append(fill(_("Accounting"), "", accounting_sub))

    return items

def event_management_menu(
        request: HttpRequest,
        event: Event,
) -> typing.List[MenuItem]:
    fill = functools.partial(_fill, event, request.resolver_match.view_name)

    items: typing.List[MenuItem] = [
        fill(_("People"), "kirppu:people_manage"),
    ]
    return items


from typing import List

from tracardi.domain.event import Event
from tracardi.domain.profile import Profile
from tracardi.domain.session import Session
from tracardi.service.plugin.domain.register import Plugin, Spec, MetaData, Documentation, PortDoc, Form, FormGroup, \
    FormField, FormComponent
from tracardi.service.plugin.domain.result import Result
from tracardi.service.plugin.runner import ActionRunner
from tracardi.service.plugin.domain.config import PluginConfig

class Configuration(PluginConfig):
    traits: List[str]

def validate(config: dict) -> Configuration:
    return Configuration(**config)

class MaskTraitsAction(ActionRunner):

    config: Configuration

    async def set_up(self, init):
        self.config = validate(init)

    async def run(self, payload: dict, in_edge=None) -> Result:

        dot = self._get_dot_accessor(payload)

        for trait in self.config.traits:

            if dot.source(trait) == 'flow':
                self.console.warning("Flow values can not be hashed.")
                continue

            elif not dot.validate(trait) or trait not in dot:
                self.console.warning(f"Given trait {trait} is invalid or does not exist.")
                continue

            dot[trait] = "###"

        if dot.profile:
            profile = Profile(**dot.profile)
            self.profile.replace(profile)

        if dot.session:
            session = Session(**dot.session)
            self.session.replace(session)

        event = Event(**dot.event)
        self.event.replace(event)

        return Result(port='payload', value=payload)

def register() -> Plugin:
    return Plugin(
        start=False,
        spec=Spec(
            module=__name__,
            className='MaskTraitsAction',
            inputs=["payload"],
            outputs=['payload'],
            version='0.7.0',
            license="MIT",
            author="Risto Kowaczewski",
            init={"traits": []},
            form=Form(groups=[
                FormGroup(
                    name="Traits to hash",
                    fields=[
                        FormField(
                            id="traits",
                            name="Reference value to be masked",
                            description="Values with be masked with ###.",
                            component=FormComponent(type="listOfDotPaths",
                                                    props={"label": "traits", "defaultSourceValue": "profile"})
                        )
                    ]
                ),
            ]),

        ),
        metadata=MetaData(
            name='Mask data',
            desc='Mask defined data, e.g. profile traits.',
            icon='hash',
            group=["Operations"],
            tags=['profile', 'trait', 'data'],
            documentation=Documentation(
                inputs={
                    "payload": PortDoc(desc="This port takes payload object.")
                },
                outputs={
                    "payload": PortDoc(desc="This port returns input payload.")
                }
            )
        )
    )


import os
import subprocess
import time
from datetime import datetime

def check_host_status(ip):
    command = ['ping', '-c', '1', ip]
    response = subprocess.call(command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
    return response == 0

def main():
    ip = '8.8.8.8'  # Replace with the IP address you want to test

    while True:
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
        status = 'Network Active' if check_host_status(ip) else 'Network Down'
        print(f'{timestamp} {status} to {ip}')
        time.sleep(2)

if __name__ == '__main__':
    main()


import time
import threading
import logging
try:
    import queue
except ImportError:
    import Queue as queue

log = logging.getLogger(__name__)

class Task(object):

    """
    A scheduled task that will be executed by the scheduler
    after a given time interval has passed.

    Attributes:
        name     -- The name of the task.
        seconds  -- The number of seconds to wait before executing.
        callback -- The function to execute.
        args     -- The arguments to pass to the callback.
        kwargs   -- The keyword arguments to pass to the callback.
        repeat   -- Indicates if the task should repeat.
                    Defaults to False.
        qpointer -- A pointer to an event queue for queuing callback
                    execution instead of executing immediately.

    Methods:
        run   -- Either queue or execute the callback.
        reset -- Reset the task's timer.
    """

    def __init__(self, name, seconds, callback, args=None,
                 kwargs=None, repeat=False, qpointer=None):
        """
        Create a new task.

        Arguments:
            name     -- The name of the task.
            seconds  -- The number of seconds to wait before executing.
            callback -- The function to execute.
            args     -- The arguments to pass to the callback.
            kwargs   -- The keyword arguments to pass to the callback.
            repeat   -- Indicates if the task should repeat.
                        Defaults to False.
            qpointer -- A pointer to an event queue for queuing callback
                        execution instead of executing immediately.
        """
        self.name = name
        self.seconds = seconds
        self.callback = callback
        self.args = args or tuple()
        self.kwargs = kwargs or {}
        self.repeat = repeat
        self.next = time.time() + self.seconds
        self.qpointer = qpointer

    def run(self):
        """
        Execute the task's callback.

        If an event queue was supplied, place the callback in the queue;
        otherwise, execute the callback immediately.
        """
        if self.qpointer is not None:
            self.qpointer.put(('schedule', self.callback, self.args))
        else:
            self.callback(*self.args, **self.kwargs)
        self.reset()
        return self.repeat

    def reset(self):
        """
        Reset the task's timer so that it will repeat.
        """
        self.next = time.time() + self.seconds

class Scheduler(object):

    """
    A threaded scheduler that allows for updates mid-execution unlike the
    scheduler in the standard library.

    http://docs.python.org/library/sched.html#module-sched

    Attributes:
        addq        -- A queue storing added tasks.
        schedule    -- A list of tasks in order of execution times.
        thread      -- If threaded, the thread processing the schedule.
        run         -- Indicates if the scheduler is running.
        parentqueue -- A parent event queue in control of this scheduler.

    Methods:
        add     -- Add a new task to the schedule.
        process -- Process and schedule tasks.
        quit    -- Stop the scheduler.
    """

    def __init__(self, parentqueue=None, parentstop=None):
        """
        Create a new scheduler.

        Arguments:
            parentqueue -- A separate event queue controlling this scheduler.
        """
        self.addq = queue.Queue()
        self.schedule = []
        self.thread = None
        self.run = False
        self.parentqueue = parentqueue
        self.parentstop = parentstop

    def process(self, threaded=True):
        """
        Begin accepting and processing scheduled tasks.

        Arguments:
            threaded -- Indicates if the scheduler should execute in its own
                        thread. Defaults to True.
        """
        if threaded:
            self.thread = threading.Thread(name='sheduler_process',
                                           target=self._process)
            self.thread.daemon = True
            self.thread.start()
        else:
            self._process()

    def _process(self):
        """Process scheduled tasks."""
        self.run = True
        try:
            while self.run and (self.parentstop is None or \
                                not self.parentstop.isSet()):
                    wait = 1
                    updated = False
                    if self.schedule:
                        wait = self.schedule[0].next - time.time()
                    try:
                        if wait <= 0.0:
                            newtask = self.addq.get(False)
                        else:
                            if wait >= 3.0:
                                wait = 3.0
                            newtask = self.addq.get(True, wait)
                    except queue.Empty:
                        cleanup = []
                        for task in self.schedule:
                            if time.time() >= task.next:
                                updated = True
                                if not task.run():
                                    cleanup.append(task)
                            else:
                                break
                        for task in cleanup:
                            x = self.schedule.pop(self.schedule.index(task))
                    else:
                        updated = True
                        self.schedule.append(newtask)
                    finally:
                        if updated:
                            self.schedule = sorted(self.schedule,
                                                   key=lambda task: task.next)
        except KeyboardInterrupt:
            self.run = False
            if self.parentstop is not None:
                log.debug("stopping parent")
                self.parentstop.set()
        except SystemExit:
            self.run = False
            if self.parentstop is not None:
                self.parentstop.set()
        log.debug("Quitting Scheduler thread")
        if self.parentqueue is not None:
            self.parentqueue.put(('quit', None, None))

    def add(self, name, seconds, callback, args=None,
            kwargs=None, repeat=False, qpointer=None):
        """
        Schedule a new task.

        Arguments:
            name     -- The name of the task.
            seconds  -- The number of seconds to wait before executing.
            callback -- The function to execute.
            args     -- The arguments to pass to the callback.
            kwargs   -- The keyword arguments to pass to the callback.
            repeat   -- Indicates if the task should repeat.
                        Defaults to False.
            qpointer -- A pointer to an event queue for queuing callback
                        execution instead of executing immediately.
        """
        self.addq.put(Task(name, seconds, callback, args,
                           kwargs, repeat, qpointer))

    def quit(self):
        """Shutdown the scheduler."""
        self.run = False


from __future__ import annotations

import os
from pathlib import Path
from typing import *

import pytest

from googleapiutils2.drive import Drive
from googleapiutils2.sheets import Sheets
from googleapiutils2.utils import Credentials, GoogleMimeTypes, get_oauth2_creds

@pytest.fixture(scope="session", autouse=True)
def creds():
    config_path = Path(
        os.environ.get("GOOGLE_API_CREDENTIALS", "auth/credentials.json")
    )
    return get_oauth2_creds(client_config=config_path)

@pytest.fixture(scope="session", autouse=True)
def drive(creds: Credentials):
    return Drive(creds=creds)

@pytest.fixture(scope="session", autouse=True)
def sheets(creds: Credentials):
    return Sheets(creds=creds)

@pytest.fixture(scope="session", autouse=True)
def google_folders(drive: Drive):
    test_folder_path = Path(os.environ.get("GOOGLE_API_TEST_PATH", "googleapiutils2"))

    sheets_folder = test_folder_path / "sheets"
    drive_folder = test_folder_path / "drive"

    folder_paths = [sheets_folder, drive_folder]

    google_folders = {
        folder_path.name: drive.create(
            name=folder_path,
            mime_type=GoogleMimeTypes.folder,
            get_extant=True,
            recursive=True,
        )
        for folder_path in folder_paths
    }

    yield google_folders

    for folder in google_folders.values():
        drive.delete(folder["id"])


# HTTP Verbs
# GET, POST, PUT, PATCH, DELETE

import requests

url = "https://icanhazdadjoke.com/"
response = requests.get(url, headers= { "Accept": 'application/json' })
# print(response.text)
# print(response.json())
data = response.json()
print(data['joke'])


import pandas as pd
import numpy as np
from sklearn.linear_model import SGDClassifier, LogisticRegression
from sklearn.preprocessing import normalize
from sklearn.metrics import precision_score, recall_score, f1_score
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from scipy.stats import pearsonr

def test(n, data):
  X = np.zeros((n, 1))
  Y = []
  i = 0
  prices = []
  for index, hit in data.iterrows():
    price = int(hit['price'])
    prices.append(price)
    X[i, :] = np.array(price)
    points = int(hit['points'])
    Y.append(points)
    i += 1
  return X, Y, prices

if __name__ == '__main__':
	data = pd.read_csv('train.csv')

	X_train, Y_train, p = test(109789, data)
	print('Retrieved data')
	print('Correlation: ' + str(pearsonr(p, Y_train)[0]))
	model = LogisticRegression().fit(X_train, Y_train)
	print('Training complete')

	datat = pd.read_csv('test.csv')
	X_test, Y_test, p = test(13707, datat)
	Y_pred = model.predict(X_test)

	print('F1: {}'.format(f1_score(Y_test, Y_pred, average='micro')))
	print(Y_pred)

	with open('test_output_price.txt' ,'w') as f:
	  for i in Y_pred:
	    f.write(str(i) + '\n')


# https://leetcode.com/problems/longest-turbulent-subarray/
# tags: #array, #dp, #sliding_window
#
# Solution 1: Sliding window
# Keep track of the last flip and current flip, if they are the same then reset start_index to i - 1
# Edge case when arr[i] == arr[i - 1], in this case reset start_index to i as last_flip is invalid
# Time Complexity: O(n), Space complexity: O(1)
#
# Solution 2: dp
# We use the counter to track the sub array size.
# Our counter is positive  if we expect '>', and '<' otherwise. For each turn we flip the sign
# Time complexity: O(n), Space complexity: O(1)
from typing import List

class Solution:
    def maxTurbulenceSize(self, arr: List[int]) -> int:
        max_length, start_index, last_flip = 1, 0, None
        for i in range(1, len(arr)):
            if arr[i] == arr[i - 1]:
                start_index = i
                last_flip = None
                continue

            current_flip = 1 if arr[i] > arr[i - 1] else -1
            if last_flip is None or last_flip == current_flip * -1:
                max_length = max(max_length, i - start_index + 1)
            else:
                start_index = i - 1

            last_flip = current_flip

        return max_length

    def maxTurbulenceSize_dp(self, arr: List[int]) -> int:
        max_length, counter = 0, 0
        for i in range(1, len(arr)):
            if arr[i] > arr[i - 1]:
                counter = counter + 1 if counter > 0 else 1
            elif arr[i] < arr[i - 1]:
                counter = counter - 1 if counter < 0 else -1
            else:
                counter = 0

            max_length = max(max_length, abs(counter))
            counter *= -1

        return max_length + 1

if __name__ == "__main__":
    sol = Solution()
    print(sol.maxTurbulenceSize_dp(arr=[9, 4, 2, 10, 7, 8, 8, 1, 9]))  # 5
    print(sol.maxTurbulenceSize_dp(arr=[4, 8, 12, 16]))  # 2
    print(sol.maxTurbulenceSize_dp(arr=[100]))  # 1