# encoding: utf-8
"""
Core
"""
import json
import pytz

from typing import Any, Dict
from base64 import b64decode
from datetime import datetime
from google.cloud import datastore
from jsonschema import validate
from jsonschema.exceptions import ValidationError, SchemaError

import config
import log

def current_datetime():
    return datetime.now(pytz.timezone("America/Sao_Paulo")).isoformat()

def validate_schema(doc_value: json, schema_properties: json) -> bool:
    """
        Parameters
        ----------
        doc_value : json
            The document json to validate
        schema_properties : json
            Json schema with properties to validate doc_value
    import datetime
        Returns
        -------
        Boolean
            Return with the boolean value

    """
    try:
        validate(instance=doc_value, schema=schema_properties)
    except (ValidationError, SchemaError):
        return False
    return True

def validate_manifest(pubsub_message: Any) -> Dict[str, Any]:
    """
    Parameters
    ----------
    pubsub_message : str
        Pub/Sub Message to Validate
    Returns
    -------
    JSON Manifest, Dict
        Return JSON dict with Manifest Config

    """
    json_manifest = json.loads(b64decode(pubsub_message).decode("utf-8"))
    print(json_manifest)
    try:
        result = validate_schema(json_manifest, config.MANIFEST_SCHEMA)
        if result:
            return json_manifest
        return None
    except ValueError:  # Invalid JSON
        return None

def datastore_publish(json_manifest: json, id: str):
    """
    Parameters
    ----------
    json_manifest : json
        JSON with Manifest contract
    id : str
        Id of Manifest
    Returns
    -------
    None
    """
    # Instantiates a client
    datastore_client = datastore.Client()

    # The kind for the new entity
    kind = "schema-repository"
    # The Cloud Datastore key for the new entity
    task_key = datastore_client.key(kind, id)

    # Verify if Document Alredy Exists and
    # update `version`` and `last_update`` values
    #
    task_result = datastore_client.get(task_key)
    version = (
        int(task_result["manifest"]["meta"]["version"]) + 1
        if task_result is not None
        else 1
    )

    json_manifest["meta"]["version"] = version
    json_manifest["meta"]["last_update"] = current_datetime()

    # Prepares the new entity
    task = datastore.Entity(key=task_key)
    task["manifest"] = json_manifest

    # Saves the entity
    datastore_client.put(task)

    log.info(f"Saved {task.key.name}: {task['manifest']}")


import os, sys, statistics as stats
tags = []

#Outliner v1.0
#Copyright Richard Gustafsson
#Release: Oct 15 2019

def spc(i=1337):
	if i != 1337:
		print(i)
	print("")

if os.path.isfile("tags.txt"):
	with open("tags.txt") as file:
		tags = [line.strip() for line in file]

	print("Tags loaded:")
	for index in range(0, len(tags)):
		if index == len(tags)-1:
			print(tags[index])
		else:
			print(tags[index], end=", ")
	spc()

else:
	print("ERROR: Could not find file tags.txt in this directory")
	print("Make a file called tags.txt and write one tag per line")
	input("Press any key to continue...")
	sys.exit()

def clear():
	if os.name == "nt":
		os.system("cls")
	else:
		os.system("clear")

def separate(file):
	with open(file) as f:
		tempFirstLine = f.readline()
		tempFirstLine = tempFirstLine.split()

		#reset the file pointer, otherwise the first row would
		#be ignored since it already has been read with f.readline()
		f.seek(0)

		#create a list containing each line, with any whitespace and
		#other weird chars like \n removed
		tempList = []
		for line in f.readlines():
			tempList.append(line.rstrip())

		return tempList, len(tempFirstLine)

def makeNumber(i):
	try:
		return int(i)
	except:
		return 0

def tryPrint(n):
	try:
		print(f"---{tags[n]}---")
	except:
		print("ERROR: UNKNOWN TAG")

def flattenData(fp):
	spc()
	spc()
	data, dataPerRow = separate(fp)

	lineCount = 0
	for line in data:
		lineCount += 1

	for i in range(1, dataPerRow+1):

		totalSum = 0
		medianList = []

		for line in data:

			tempLine = line.split()
			#if the list is still in range, continue
			try:
				totalSum += makeNumber(tempLine[i-1])
				medianList.append(makeNumber(tempLine[i-1]))
			except:
				pass

		#Calculate the average and the median
		avg = totalSum / lineCount

		med = stats.median(medianList)

		#print the content
		tryPrint(i-1)
		print(f"Sum: {totalSum}, Average: {round(avg, 1)}, Median {med}")
		spc()
	spc()
	print("Ready for another round")

while True:
	filePath = input("Name of data file (with extension)? ")
	if len(filePath) > 0:
		if os.path.isfile(filePath):
			flattenData(filePath)
		else:
			spc("ERROR: Not a file")
	else:
		spc("ERROR: Files must contain at least one character")


import weibull

# the current run time of the test or the
# time that the test was suspended completely
current_run_time = 4200.0

fail_times = [current_run_time] * 10
fail_times[7] = 1034.5
fail_times[8] = 2550.9
fail_times[6] = 3043.4

suspended = [True, True, True, True, True,
             False, False, False, True, True]

analysis = weibull.Analysis(fail_times, suspended=suspended, unit='hour')
analysis.fit()

analysis.probplot(file_name='weibull-fit.png')  # option to save as an image
analysis.pdf()
analysis.sf()
analysis.hazard()
analysis.cdf()

print(f'beta: {analysis.beta}\teta: {analysis.eta}')
print(f'{analysis.fit_test}')
print(f'B2 life: {analysis.b(2):.02f}\nB10 life: {analysis.b(10):.02f}\nB50 life: {analysis.b(50):.02f}')
print(f'median: {analysis.median}')
print(f'mean: {analysis.mean}')
print(f'mttf: {analysis.mttf}')


"""Deep Q learning graph with action masks

The functions in this file can are used to create the following functions:

======= act ========

    Function to chose an action given an observation

    Parameters
    ----------
    observation: object
        Observation that can be feed into the output of make_obs_ph
    stochastic: bool
        if set to False all the actions are always deterministic (default False)
    update_eps_ph: float
        update epsilon a new value, if negative no update happens
        (default: no update)

    Returns
    -------
    Tensor of dtype tf.int64 and shape (BATCH_SIZE,) with an action to be performed for
    every element of the batch.

======= act (in case of parameter noise) ========

    Function to chose an action given an observation

    Parameters
    ----------
    observation: object
        Observation that can be feed into the output of make_obs_ph
    stochastic: bool
        if set to False all the actions are always deterministic (default False)
    update_eps_ph: float
        update epsilon to a new value, if negative no update happens
        (default: no update)
    reset_ph: bool
        reset the perturbed policy by sampling a new perturbation
    update_param_noise_threshold_ph: float
        the desired threshold for the difference between non-perturbed and perturbed policy
    update_param_noise_scale_ph: bool
        whether or not to update the scale of the noise for the next time it is re-perturbed

    Returns
    -------
    Tensor of dtype tf.int64 and shape (BATCH_SIZE,) with an action to be performed for
    every element of the batch.

======= train =======

    Function that takes a transition (s,a,r,s') and optimizes Bellman equation's error:

        td_error = Q(s,a) - (r + gamma * max_a' Q(s', a'))
        loss = huber_loss[td_error]

    Parameters
    ----------
    obs_t: object
        a batch of observations
    action: np.array
        actions that were selected upon seeing obs_t.
        dtype must be int32 and shape must be (batch_size,)
    reward: np.array
        immediate reward attained after executing those actions
        dtype must be float32 and shape must be (batch_size,)
    obs_tp1: object
        observations that followed obs_t
    done: np.array
        1 if obs_t was the last observation in the episode and 0 otherwise
        obs_tp1 gets ignored, but must be of the valid shape.
        dtype must be float32 and shape must be (batch_size,)
    weight: np.array
        imporance weights for every element of the batch (gradient is multiplied
        by the importance weight) dtype must be float32 and shape must be (batch_size,)

    Returns
    -------
    td_error: np.array
        a list of differences between Q(s,a) and the target in Bellman's equation.
        dtype is float32 and shape is (batch_size,)

======= update_target ========

    copy the parameters from optimized Q function to the target Q function.
    In Q learning we actually optimize the following error:

        Q(s,a) - (r + gamma * max_a' Q'(s', a'))

    Where Q' is lagging behind Q to stablize the learning. For example for Atari

    Q' is set to Q once every 10000 updates training steps.

"""
import tensorflow as tf
import baselines.common.tf_util as U

def build_act(make_obs_ph, q_func, num_actions, scope="deepq", reuse=None):
    """Creates the act function:

    Parameters
    ----------
    make_obs_ph: str -> tf.placeholder or TfInput
        a function that take a name and creates a placeholder of input with that name
    q_func: (tf.Variable, int, str, bool) -> tf.Variable
        the model that takes the following inputs:
            observation_in: object
                the output of observation placeholder
            num_actions: int
                number of actions
            scope: str
            reuse: bool
                should be passed to outer variable scope
        and returns a tensor of shape (batch_size, num_actions) with values of every action.
    num_actions: int
        number of actions.
    scope: str or VariableScope
        optional scope for variable_scope.
    reuse: bool or None
        whether or not the variables should be reused. To be able to reuse the scope must be given.

    Returns
    -------
    act: (tf.Variable, bool, float) -> tf.Variable
        function to select and action given observation.
`       See the top of the file for details.
    """
    with tf.variable_scope(scope, reuse=reuse):
        observations_ph = make_obs_ph("observation")
        stochastic_ph = tf.placeholder(tf.bool, (), name="stochastic")
        update_eps_ph = tf.placeholder(tf.float32, (), name="update_eps")
        action_mask_ph = tf.placeholder(tf.float32, [None,num_actions], name="action_mask")

        eps = tf.get_variable("eps", (), initializer=tf.constant_initializer(0))

        q_values = q_func(observations_ph.get(), num_actions, scope="q_func")
        q_values = q_values*action_mask_ph + (action_mask_ph-1)*1000000 # remove invalid actions by adding forcing their q-values to be -1M
        deterministic_actions = tf.argmax(q_values, axis=1)

        batch_size = tf.shape(observations_ph.get())[0]
        random_actions = tf.random_uniform(tf.stack([batch_size]), minval=0, maxval=num_actions, dtype=tf.int64)
        chose_random = tf.random_uniform(tf.stack([batch_size]), minval=0, maxval=1, dtype=tf.float32) < eps
        stochastic_actions = tf.where(chose_random, random_actions, deterministic_actions)

        output_actions = tf.cond(stochastic_ph, lambda: stochastic_actions, lambda: deterministic_actions)
        update_eps_expr = eps.assign(tf.cond(update_eps_ph >= 0, lambda: update_eps_ph, lambda: eps))
        _act = U.function(inputs=[observations_ph, stochastic_ph, update_eps_ph, action_mask_ph],
                         outputs=output_actions,
                         givens={update_eps_ph: -1.0, stochastic_ph: True},
                         updates=[update_eps_expr])
        def act(ob, stochastic=True, update_eps=-1, action_mask=1):
            return _act(ob, stochastic, update_eps, action_mask)
        return act

def build_train(make_obs_ph, q_func, num_actions, optimizer, grad_norm_clipping=None,
    double_q=True, scope="controller", reuse=None):
    """Creates the train function:

    Parameters
    ----------
    make_obs_ph: str -> tf.placeholder or TfInput
        a function that takes a name and creates a placeholder of input with that name
    q_func: (tf.Variable, int, str, bool) -> tf.Variable
        the model that takes the following inputs:
            observation_in: object
                the output of observation placeholder
            num_actions: int
                number of actions
            scope: str
            reuse: bool
                should be passed to outer variable scope
        and returns a tensor of shape (batch_size, num_actions) with values of every action.
    num_actions: int
        number of actions
    reuse: bool
        whether or not to reuse the graph variables
    optimizer: tf.train.Optimizer
        optimizer to use for the Q-learning objective.
    grad_norm_clipping: float or None
        clip gradient norms to this value. If None no clipping is performed.
    double_q: bool
        if true will use Double Q Learning (https://arxiv.org/abs/1509.06461).
        In general it is a good idea to keep it enabled.
    scope: str or VariableScope
        optional scope for variable_scope.
    reuse: bool or None
        whether or not the variables should be reused. To be able to reuse the scope must be given.

    Returns
    -------
    act: (tf.Variable, bool, float) -> tf.Variable
        function to select and action given observation.
`       See the top of the file for details.
    train: (object, np.array, np.array, object, np.array, np.array) -> np.array
        optimize the error in Bellman's equation.
`       See the top of the file for details.
    update_target: () -> ()
        copy the parameters from optimized Q function to the target Q function.
`       See the top of the file for details.
    debug: {str: function}
        a bunch of functions to print debug data like q_values.
    """
    act_f = build_act(make_obs_ph, q_func, num_actions, scope=scope, reuse=reuse)

    with tf.variable_scope(scope, reuse=reuse):
        # set up placeholders
        obs_t_input = make_obs_ph("obs_t")
        act_t_ph = tf.placeholder(tf.int32, [None], name="action")
        rew_t_ph = tf.placeholder(tf.float32, [None], name="reward")
        obs_tp1_input = make_obs_ph("obs_tp1")
        done_mask_ph = tf.placeholder(tf.float32, [None], name="done")
        act_t_mask_ph = tf.placeholder(tf.float32, [None,num_actions], name="act_mask")
        gammas_ph = tf.placeholder(tf.float32, [None], name="gammas")
        importance_weights_ph = tf.placeholder(tf.float32, [None], name="weight")

        # q network evaluation
        q_t = q_func(obs_t_input.get(), num_actions, scope="q_func", reuse=True)  # reuse parameters from act
        q_func_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=tf.get_variable_scope().name + "/q_func")

        # target q network evalution
        q_tp1 = q_func(obs_tp1_input.get(), num_actions, scope="target_q_func")
        target_q_func_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=tf.get_variable_scope().name + "/target_q_func")

        # q scores for actions which we know were selected in the given state.
        q_t_selected = tf.reduce_sum(q_t * tf.one_hot(act_t_ph, num_actions), 1)

        # compute estimate of best possible value starting from state at t + 1
        if double_q:
            q_tp1_using_online_net = q_func(obs_tp1_input.get(), num_actions, scope="q_func", reuse=True)
            q_tp1_using_online_net = q_tp1_using_online_net*act_t_mask_ph + (act_t_mask_ph-1.0)*1000000.0 # remove invalid actions by adding forcing their q-values to be -1M
            q_tp1_best_using_online_net = tf.argmax(q_tp1_using_online_net, 1)
            q_tp1_best = tf.reduce_sum(q_tp1 * tf.one_hot(q_tp1_best_using_online_net, num_actions), 1)
        else:
            q_tp1 = q_tp1*act_t_mask_ph + (act_t_mask_ph-1)*1000000 # remove invalid actions by adding forcing their q-values to be -1M
            q_tp1_best = tf.reduce_max(q_tp1, 1)
        q_tp1_best_masked = (1.0 - done_mask_ph) * q_tp1_best

        # compute RHS of bellman equation
        q_t_selected_target = rew_t_ph + gammas_ph * q_tp1_best_masked

        # compute the error (potentially clipped)
        td_error = q_t_selected - tf.stop_gradient(q_t_selected_target)
        errors = U.huber_loss(td_error)
        weighted_error = tf.reduce_mean(importance_weights_ph * errors)

        # compute optimization op (potentially with gradient clipping)
        if grad_norm_clipping is not None:
            gradients = optimizer.compute_gradients(weighted_error, var_list=q_func_vars)
            for i, (grad, var) in enumerate(gradients):
                if grad is not None:
                    gradients[i] = (tf.clip_by_norm(grad, grad_norm_clipping), var)
            optimize_expr = optimizer.apply_gradients(gradients)
        else:
            optimize_expr = optimizer.minimize(weighted_error, var_list=q_func_vars)

        # update_target_fn will be called periodically to copy Q network to target Q network
        update_target_expr = []
        for var, var_target in zip(sorted(q_func_vars, key=lambda v: v.name),
                                   sorted(target_q_func_vars, key=lambda v: v.name)):
            update_target_expr.append(var_target.assign(var))
        update_target_expr = tf.group(*update_target_expr)

        # Create callable functions
        train = U.function(
            inputs=[
                obs_t_input,
                act_t_ph,
                rew_t_ph,
                obs_tp1_input,
                done_mask_ph,
                act_t_mask_ph,
                gammas_ph,
                importance_weights_ph
            ],
            outputs=td_error,
            updates=[optimize_expr]
        )
        update_target = U.function([], [], updates=[update_target_expr])

        q_values = U.function([obs_t_input], q_t)

        return act_f, train, update_target, {'q_values': q_values}


import unittest

from tree import TreeNode, build_tree
from checkIdenticalTree import checkIdenticalTree, checkIdenticalTree_iter

class CheckIdenticalTreeTestCase(unittest.TestCase):
    def test_identical_tree(self):
        test_in = [1,2,3,4,5]
        t1 = build_tree(test_in)
        t2 = build_tree(test_in)
        self.assertTrue(checkIdenticalTree(t1, t2))
        self.assertTrue(checkIdenticalTree_iter(t1, t2))

    def test_not_same_tree(self):
        test_in = [1,2,3,4,5]
        t1 = build_tree(test_in)
        t2 = build_tree([1])
        self.assertFalse(checkIdenticalTree(t1, t2))
        self.assertFalse(checkIdenticalTree_iter(t1, t2))

        # Flip input trees
        test_in = [1,2,3,4,5]
        t2 = build_tree(test_in)
        t1 = build_tree([1])
        self.assertFalse(checkIdenticalTree(t1, t2))
        self.assertFalse(checkIdenticalTree_iter(t1, t2))

    def test_empty_trees(self):
        t1 = build_tree([])
        t2 = build_tree([])
        self.assertTrue(checkIdenticalTree(t1, t2))
        self.assertTrue(checkIdenticalTree_iter(t1, t2))

    def test_check_iter(self):
        t1 = build_tree([1,2,3,4])
        t2 = build_tree([1,2,3,4])
        self.assertTrue(checkIdenticalTree(t1, t2))

        t1 = build_tree([1,2,3,1])
        t2 = build_tree([1,2,3,4])
        self.assertFalse(checkIdenticalTree(t1, t2))

if __name__ == '__main__':
    unittest.main()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('qsource_user', '0005_auto_20151118_2237'),
    ]

    operations = [
        migrations.CreateModel(
            name='QuestionsAnswered',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('questionID', models.IntegerField()),
                ('user', models.ForeignKey(to='qsource_user.UserData')),
            ],
        ),
        migrations.CreateModel(
            name='QuestionsAsked',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('questionID', models.IntegerField()),
                ('user', models.ForeignKey(to='qsource_user.UserData')),
            ],
        ),
    ]


import os
import pandas as pd
import numpy as np
import os
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from sklearn.pipeline import make_pipeline, Pipeline
import platform
import sys
import sklearn
import tensorflow as tf

file_path = 'H:/Study/Hackarthon/dacon/shopping/dataset/dataset'

train = pd.read_csv('H:/Study/Hackarthon/dacon/shopping/dataset/dataset/train.csv')
test = pd.read_csv('H:/Study/Hackarthon/dacon/shopping/dataset/dataset/test.csv')
sample_submission = pd.read_csv('H:/Study/Hackarthon/dacon/shopping/dataset/dataset/sample_submission.csv')

print(f"-os:{platform.platform()}")
print(f"-python:{sys.version}")
print(f"-pandas:{pd.__version__}")
print(f"-numpy:{np.__version__}")
print(f"-sklearn: {sklearn.__version__}")
print(f'-tensorflow: {tf.__version__}')

# print(train.shape) (6255, 13)
# print(test.shape) (180, 12)
# print(sample_submission.shape) (180, 2)

# print(train.info())

# #   Column        Non-Null Count  Dtype
# ---  ------        --------------  -----
#  0   id            6255 non-null   int64
#  1   Store         6255 non-null   int64
#  2   Date          6255 non-null   object
#  3   Temperature   6255 non-null   float64
#  4   Fuel_Price    6255 non-null   float64
#  5   Promotion1    2102 non-null   float64
#  6   Promotion2    1592 non-null   float64
#  7   Promotion3    1885 non-null   float64
#  8   Promotion4    1819 non-null   float64
#  9   Promotion5    2115 non-null   float64
#  10  Unemployment  6255 non-null   float64
#  11  IsHoliday     6255 non-null   bool
#  12  Weekly_Sales  6255 non-null   float64
# dtypes: bool(1), float64(9), int64(2), object(1)
# memory usage: 592.6+ KB
# None

train = train.fillna(0)

# print(train.shape) (6255, 13)

def date_encoder(date):
    day, month, year = map(int, date.split('/'))
    return day, month, year

train['Day'] = train['Date'].apply(lambda x: date_encoder(x)[0])
train['Month'] = train['Date'].apply(lambda x: date_encoder(x)[1])
train['Year'] = train['Date'].apply(lambda x: date_encoder(x)[2])

train = train.drop(columns=['Day', 'Date'])

scaler = StandardScaler()

scaler.fit(train[['Promotion1', 'Promotion2','Promotion3', 'Promotion4', 'Promotion5']])

scaled = scaler.transform(train[['Promotion1','Promotion2','Promotion3','Promotion4','Promotion5']])

train[['Scaled_Promotion1','Scaled_Promotion2',
        'Scaled_Promotion3','Scaled_promotion4',
        'Scaled_Promotion5']] = scaled

train = train.drop(columns=['Promotion1','Promotion2','Promotion3','Promotion4','Promotion5'])

test = test.fillna(0)

test['Month']=test['Date'].apply(lambda x: date_encoder(x)[1])
test['Year'] = test['Date'].apply(lambda x:date_encoder(x)[2])
test = test.drop(columns=['Date'])

scaled = scaler.transform(test[['Promotion1','Promotion2','Promotion3','Promotion4','Promotion5']])

test[['Scaled_Promotion1','Scaled_Promotion2','Scaled_Promotion3','Scaled_promotion4','Scaled_Promotion5']] = scaled

test = test.drop(columns=['Promotion1','Promotion2','Promotion3','Promotion4','Promotion5'])

from sklearn.ensemble import RandomForestRegressor
from xgboost import XGBRegressor
from catboost import CatBoostRegressor
from sklearn.model_selection import train_test_split, KFold, cross_val_score, StratifiedKFold

model = make_pipeline(StandardScaler(), RandomForestRegressor())

# model = RandomForestRegressor()

train = train.drop(columns=['id'])
test = test.drop(columns=['id'])

x_train = train.drop(columns=['Weekly_Sales'])
y_train = train['Weekly_Sales']

model.fit(x_train, y_train)

prediction = model.predict(test)
print('---------------------------예측된 데이터의 상위 10개의 값 확인 --------------------------------- \n')
print(prediction[:10])

sample_submission['Weekly_Sales'] = prediction

# sample_submission.head()

sample_submission.to_csv('H:/Study/Hackarthon/dacon/shopping/dataset/dataset/sookook7.csv', index = False)


import torch
import json

speakers = torch.load('../models/vits_ca/speakers.pth')
print(type(speakers))
conv = [line.strip().split(',') for line in open('speakers_conversion.csv').readlines()]
new_speakers = {}
for source, target in conv:
    id = speakers.get(source)
    if id:
        new_speakers[target] = source
with open('speaker_ids.json', 'w') as out:
    json.dump(new_speakers, out)
print(new_speakers)


#!/usr/bin/python3
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-10,10,100) # 创建一个包含-10到10之间100个等距点的数组作为x坐标
y= x ** 2 # 计算y坐标

plt.plot(x, y)  # 绘制曲线
plt.xlabel('x') # 设置x轴标签
plt.xlabel('y') # 设置y轴标签
plt.title('y = x^2') # 设置图标题
plt.grid(True) # 显示网格线

plt .show() #显示图形


#!/usr/bin/env python3

import multiprocessing
import time
import os

from src.model.load_json import load_json_to_dict
from src.model.load_script import load_script_file
from src.model.shunt import Shunt
from src.tools.engine.code.github.clone import CodeClone
from src.tools.log4py.log4py import print_log
from src.tools.log4py.log4py import LogLevel
from src.tools.engine.soft.install_soft import SoftTool
from src.config.config import configration

def main_process():

    print_log(msg=f"Main Process Started: PID {os.getpid()}", level=LogLevel.INFO)
    while True:
        print_log("Main Process is running...")
        folder_path = configration.get("script_path")
        script_type=[".json", ".py", ".shell", "sh"]
        scripts = load_script_file(folder_path=folder_path, script_type=script_type)

        print_log(msg="Begin execute scripts.", level=LogLevel.DEBUG)
        for item in scripts:

            print_log(msg=f"Begin run script: {item}.", level=LogLevel.DEBUG)

            script_shunt = Shunt(file_path=item, folder_path=folder_path)
            script_shunt.operation()
            print("|")
            print("|")
            print("|")

            print_log(msg=f"Run script: {item} over.", level=LogLevel.DEBUG)
            break

        time.sleep(configration.get("mian_process_sleep_time", 300))  # 模拟主进程工作

def monitor_process():
    while True:
        # 使用进程ID来检查主进程是否存活
        main_process_alive = False
        for process in multiprocessing.active_children():
            if process.name == 'MainProcess':
                main_process_alive = True
                break

        if not main_process_alive:
            print_log(msg="Main Process down! Restarting...", level=LogLevel.ERROR)
            # 重启主进程
            new_main_process = multiprocessing.Process(target=main_process, name='MainProcess')
            new_main_process.start()
        else:
            print_log(mgs=f"Main Process is alive.", level=LogLevel.INFO)

        time.sleep(30)  # 每隔3秒检查一次

if __name__ == "__main__":
    # 创建主进程
    main_proc = multiprocessing.Process(target=main_process, name='MainProcess')
    main_proc.start()

    # 创建监控进程
    monitor_proc = multiprocessing.Process(target=monitor_process)
    monitor_proc.start()

    main_proc.join()
    monitor_proc.join()


''' Has two pointers for the rear and front'''

class CircularQueue:
    def __init__(self, max_size):
        self.items = max_size * [None]
        self.max_size = max_size
        self.rear = -1
        self.front = -1

    def __str__(self) -> str:
        return ' '.join([ str(item) for item in self.items ])

    def is_full(self):
        if self.rear + 1 == self.front:
            return True
        if self.front == 0 and self.rear + 1 == self.max_size:
            return True
        return False

c_queue = CircularQueue(7)
print(c_queue)
print(c_queue.is_full())


#Run old_indexer first!

import sys

import whoosh.index as index
import whoosh.qparser as qparser
from whoosh.searching import Searcher

correct = 0
queries = 0

#Opens index
ix = index.open_dir("oldIndex")

#Opens test file
with open(sys.argv[1], 'r') as f:
    while True:
        #Reads next query/url pair
        line = f.readline()
        if not line:
            break

        #Loads query and expeced answer
        query = line.split(';')[0]
        expected = line.split(';')[1].rstrip('\n')
        retrieved = []
        found = False
        queries += 1

        #Handles query
        qp = qparser.QueryParser("content", schema=ix.schema)
        q = qp.parse(query)

        #Searches index for query, checks top 3 URLs for expected URL
        with ix.searcher() as searcher:
            results = searcher.search(q)
            #Correctness metric: Is our expected URL in the top 3?
            for i in range(3):
                retrieved.append(results[i]['title'])
                if expected == retrieved[i]:
                    correct += 1
                    found = True
            print("Query {}:".format(queries), query)
            print("URL:", expected)
            if found:
                print("PASSED")
            else:
                print("FAILED")
            #Uncomment to print top 3 URLs from IR
            #print("{}\n{}\n{}\n".format(retrieved[0], retrieved[1], retrieved[2]))

print("{} / {} queries matched.".format(correct, queries))
print("Accuracy: ", float(correct/queries))


#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.
import unittest

import openstack.connection

from ospurge.resources import cinder
from ospurge.tests import mock

class TestBackups(unittest.TestCase):
    def setUp(self):
        self.cloud = mock.Mock(spec_set=openstack.connection.Connection)
        self.creds_manager = mock.Mock(cloud=self.cloud)

    def test_list(self):
        self.assertIs(self.cloud.list_volume_backups.return_value,
                      cinder.Backups(self.creds_manager).list())
        self.cloud.list_volume_backups.assert_called_once_with()

    def test_delete(self):
        backup = mock.MagicMock()
        self.assertIsNone(cinder.Backups(self.creds_manager).delete(backup))
        self.cloud.delete_volume_backup.assert_called_once_with(backup['id'])

    def test_disable(self):
        backup = mock.MagicMock()
        with self.assertLogs(level='WARNING'):
            cinder.Backups(self.creds_manager).disable(backup)

    def test_to_string(self):
        backup = mock.MagicMock()
        self.assertIn("Volume Backup",
                      cinder.Backups(self.creds_manager).to_str(backup))

class TestSnapshots(unittest.TestCase):
    def setUp(self):
        self.cloud = mock.Mock(spec_set=openstack.connection.Connection)
        self.creds_manager = mock.Mock(cloud=self.cloud)

    def test_list(self):
        self.assertIs(self.cloud.list_volume_snapshots.return_value,
                      cinder.Snapshots(self.creds_manager).list())
        self.cloud.list_volume_snapshots.assert_called_once_with()

    def test_delete(self):
        snapshot = mock.MagicMock()
        self.assertIsNone(
            cinder.Snapshots(self.creds_manager).delete(snapshot))
        self.cloud.delete_volume_snapshot.assert_called_once_with(
            snapshot['id'])

    def test_disable(self):
        snapshot = mock.MagicMock()
        with self.assertLogs(level='WARNING'):
            cinder.Snapshots(self.creds_manager).disable(snapshot)

    def test_to_string(self):
        snapshot = mock.MagicMock()
        self.assertIn("Volume Snapshot ",
                      cinder.Snapshots(self.creds_manager).to_str(snapshot))

class TestVolumes(unittest.TestCase):
    def setUp(self):
        self.cloud = mock.Mock(spec_set=openstack.connection.Connection)
        self.creds_manager = mock.Mock(cloud=self.cloud, project_id=42)

    def test_check_prerequisite(self):
        self.cloud.list_volume_snapshots.return_value = []
        self.assertEqual(
            False,
            cinder.Volumes(self.creds_manager).check_prerequisite()
        )
        self.cloud.list_volume_snapshots.assert_called_once_with()
        self.cloud.list_servers.assert_called_once_with()

    def test_list(self):
        self.assertIs(self.cloud.list_volumes.return_value,
                      cinder.Volumes(self.creds_manager).list())
        self.cloud.list_volumes.assert_called_once_with()

    def test_should_delete(self):
        self.assertEqual(
            False,
            cinder.Volumes(self.creds_manager).should_delete(
                {'os-vol-tenant-attr:tenant_id': 84})
        )
        self.assertEqual(
            True,
            cinder.Volumes(self.creds_manager).should_delete(
                {'os-vol-tenant-attr:tenant_id': 42})
        )

    def test_delete(self):
        volume = mock.MagicMock()
        self.assertIsNone(cinder.Volumes(self.creds_manager).delete(volume))
        self.cloud.delete_volume.assert_called_once_with(volume['id'])

    def test_disable(self):
        volume = mock.MagicMock()
        cinder.Volumes(self.creds_manager).disable(volume)
        self.cloud.update_volume.assert_called_once_with(
            volume['id'],
            metadata={'readonly': 'true'}
        )

    def test_to_string(self):
        volume = mock.MagicMock()
        self.assertIn("Volume ",
                      cinder.Volumes(self.creds_manager).to_str(volume))


import morse, keras
import numpy as np
from scipy import signal

channels = 1
samples_per_sec = 100
max_seconds = 5
max_samples = max_seconds * samples_per_sec
trans_seconds = 5
trans_samples = trans_seconds * samples_per_sec
latent_dim = 100
TOKENS = "$^0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ "
num_decoder_tokens = len(TOKENS)
target_token_index = dict([(char, i) for i, char in enumerate(TOKENS)])
max_translation_length = 62
use_lstm = True

# detection model
def make_model(input_shape = (max_samples,channels)):
    input_layer = keras.layers.Input(input_shape)
    nf = 64
    ks = 7

    conv1 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(input_layer)
    conv1 = keras.layers.BatchNormalization()(conv1)
    conv1 = keras.layers.MaxPooling1D()(conv1)
    conv1 = keras.layers.ReLU()(conv1)
    conv1 = keras.layers.Dropout(0.3)(conv1)

    conv2 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv1)
    conv2 = keras.layers.BatchNormalization()(conv2)
    conv2 = keras.layers.MaxPooling1D()(conv2)
    conv2 = keras.layers.ReLU()(conv2)
    conv2 = keras.layers.Dropout(0.2)(conv2)

    conv3 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv2)
    conv3 = keras.layers.BatchNormalization()(conv3)
    conv3 = keras.layers.MaxPooling1D()(conv3)
    conv3 = keras.layers.ReLU()(conv3)
    conv3 = keras.layers.Dropout(0.1)(conv3)

    conv4 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv3)
    conv4 = keras.layers.BatchNormalization()(conv4)
    conv4 = keras.layers.MaxPooling1D()(conv4)
    conv4 = keras.layers.ReLU()(conv4)
    conv4 = keras.layers.Dropout(0.1)(conv4)

    conv5 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv4)
    conv5 = keras.layers.BatchNormalization()(conv5)
    conv5 = keras.layers.MaxPooling1D()(conv5)
    conv5 = keras.layers.ReLU()(conv5)

    gap = keras.layers.GlobalAveragePooling1D()(conv5)

    output_layer = keras.layers.Dense(1, activation="sigmoid")(gap)

    return keras.models.Model(inputs=input_layer, outputs=output_layer)

# translation model
# https://keras.io/examples/nlp/lstm_seq2seq/
def make_trans_model(input_shape = (trans_samples,channels)):
    input_layer = keras.layers.Input(input_shape)
    nf = 96
    ks = 7

    conv1 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(input_layer)
    conv1 = keras.layers.BatchNormalization()(conv1)
    conv1 = keras.layers.AveragePooling1D()(conv1)
    conv1 = keras.layers.ReLU()(conv1)
    conv1 = keras.layers.Dropout(0.6)(conv1)

    conv2 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv1)
    conv2 = keras.layers.BatchNormalization()(conv2)
    conv2 = keras.layers.AveragePooling1D()(conv2)
    conv2 = keras.layers.ReLU()(conv2)
    conv2 = keras.layers.Dropout(0.4)(conv2)

    conv3 = keras.layers.Conv1D(filters=nf, kernel_size=ks, padding="same")(conv2)
    conv3 = keras.layers.BatchNormalization()(conv3)
    conv3 = keras.layers.AveragePooling1D()(conv3)
    conv3 = keras.layers.ReLU()(conv3)
    conv3 = keras.layers.Dropout(0.2)(conv3)

    if use_lstm:
        conv8 = keras.layers.LSTM(nf*2, return_sequences=True)(conv3)
    else:
        conv7 = keras.layers.Conv1D(filters=nf, kernel_size=ks, activation="relu", padding="same")(conv3)
        conv8 = keras.layers.TimeDistributed(keras.layers.Dense(nf*2, activation="relu"))(conv7)

    concat = keras.layers.Concatenate(axis=2)([conv3, conv8])
    dense = keras.layers.TimeDistributed(keras.layers.Dense(num_decoder_tokens, activation="softmax"))(concat)
    return keras.models.Model(inputs=input_layer, outputs=dense)

    #encoder_lstm = keras.layers.LSTM(latent_dim, return_sequences=True, dropout=0.1)(conv5)
    #decoder_lstm = keras.layers.LSTM(num_decoder_tokens, dropout=0.1, return_sequences=True)(encoder_lstm)
    #return keras.models.Model(inputs=input_layer, outputs=decoder_lstm)

    #encoder_inputs = conv5
    #encoder = keras.layers.LSTM(latent_dim, return_state=True, dropout=0.2)
    #encoder_outputs, state_h, state_c = encoder(encoder_inputs)
    # We discard `encoder_outputs` and only keep the states.
    #encoder_states = [state_h, state_c]
    # Set up the decoder, using `encoder_states` as initial state.
    #decoder_inputs = keras.Input(shape=(None, num_decoder_tokens))
    # We set up our decoder to return full output sequences,
    # and to return internal states as well. We don't use the
    # return states in the training model, but we will use them in inference.
    #decoder_lstm = keras.layers.LSTM(latent_dim, return_sequences=True, return_state=True)
    #decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
    #decoder_dense = keras.layers.Dense(num_decoder_tokens, activation="softmax")
    #decoder_outputs = decoder_dense(decoder_outputs)
    #return keras.models.Model(inputs=[input_layer, decoder_inputs], outputs=decoder_outputs)

class DataGenerator(keras.utils.Sequence):
    'Generates detection data for Keras'
    def __init__(self, batch_size=256):
        'Initialization'
        self.dim = (batch_size,max_samples,channels)
        self.batch_size = batch_size
        self.on_epoch_end()

    def __len__(self):
        'Denotes the number of batches per epoch'
        return 1000

    def __getitem__(self, index):
        'Generate one batch of data'
        x_train = []
        y_train = []
        for i in range(0,self.batch_size):
            msg, x, posns = morse.generate_detection_training_sample(max_samples)
            x = np.reshape(x, (-1,1))
            y = len(msg) > 0 and msg[0] != '~'
            x_train.append(x)
            y_train.append(y)
        return np.array(x_train), np.array(y_train)

    def on_epoch_end(self):
        'Updates indexes after each epoch'

class TranslationGenerator(keras.utils.Sequence):
    'Generates detection data for Keras'
    def __init__(self, batch_size=256):
        'Initialization'
        self.dim = (batch_size,trans_samples,channels)
        self.batch_size = batch_size
        self.on_epoch_end()

    def __len__(self):
        'Denotes the number of batches per epoch'
        return 1000

    def __getitem__(self, index):
        'Generate one batch of data'
        x_train = []
        y_train = []
        for i in range(0, self.batch_size):
            msg, x, posns = morse.generate_translation_training_sample(trans_samples)
            assert(len(posns) == len(msg)+1)
            x = np.reshape(x, (-1,1))
            y = np.zeros((max_translation_length, num_decoder_tokens))
            str = ['.'] * max_translation_length
            # iterate over all bins
            for i, char in enumerate(msg):
                if use_lstm:
                    # lstm, bin goes at end of symbol
                    pos = posns[i+1] / trans_samples * max_translation_length
                    ofs = int(round(pos))+1
                else:
                    # put bin smack dab in middle of the feature
                    pos = ((posns[i] + posns[i+1]) / trans_samples / 2) * max_translation_length
                    ofs = int(round(pos))
                # is this symbol in the window?
                if ofs > 0 and ofs < max_translation_length-1 and posns[i] > 0 and posns[i+1] < trans_samples:
                    tti = target_token_index[msg[i]]
                    #y[ofs-1, tti] = 1/3
                    y[ofs+0, tti] = 1/1
                    #y[ofs+1, tti] = 1/3
                    str[ofs] = char
            # set "no symbol" probability for bins
            for ofs in range(0,max_translation_length):
                y[ofs, 0] = max(0.0, 1.0 - np.sum(y[ofs]))
            x_train.append(x)
            y_train.append(y)
            #print(''.join(str))
        return np.array(x_train), np.array(y_train)

    def on_epoch_end(self):
        'Updates indexes after each epoch'

class CWDetectorTranslator:
    def __init__(self, sample_rate, overlap=0.5, wndsizes=[512,256,128]):
        self.sr = sample_rate
        self.overlap = overlap
        self.wndsizes = wndsizes
        self.nsamples = int((128 * (1-overlap) + 1) * max_samples / 2)
        self.wnd = np.zeros((self.nsamples * 8,))
        self.detections = []

        detect_checkpoint_fn = "weights_detect.h5"
        self.detect_model = make_model()
        self.detect_model.load_weights(detect_checkpoint_fn)

        trans_checkpoint_fn = "weights_translate.h5"
        self.trans_model = make_trans_model()
        self.trans_model.load_weights(trans_checkpoint_fn)

    def clear(self):
        self.wnd[:] = 0

    def add_samples(self, samples):
        # shift window by 1/2
        n = len(samples)
        self.wnd[0:-n] = self.wnd[n:]
        # add new samples
        self.wnd[-n:] = samples
        # convert to spectrogram at three different scales
        specs = []
        for nps in self.wndsizes:
            nov = int(nps * self.overlap)
            wndsamp = max_samples * (nps-nov+1)
            w = self.wnd[-wndsamp:]
            frequencies, times, spectrogram = signal.spectrogram(w, fs=self.sr, nperseg=nps, noverlap=nov)
            specs.append(spectrogram[:, 0:max_samples])
        self.spec = np.concatenate(specs, axis=0)

    def detect(self):
        xy = self.spec #[:, 0:max_samples]
        # normalize spectrogram
        ymin = np.min(xy, axis=1)
        ymax = np.max(xy, axis=1)
        xy = (xy - ymin[:,None]) / (ymax - ymin + 1e-6)[:,None]
        #xy = (xy - np.min(xy)) / (np.max(xy) - np.min(xy) + 1e-6)
        self.xy = xy
        xy = np.reshape(xy, (xy.shape[0], xy.shape[1], 1))
        p = self.detect_model.predict(xy[:, 0:max_samples])
        self.detections = np.argwhere(p > 0.5)
        # combine adjacent bins
        for i in range(0, len(self.detections)-1):
            y = self.detections[i][0]
            if y == self.detections[i+1][0] - 1:
                #self.xy[y+1] = np.maximum(self.xy[y], self.xy[y+1])
                self.xy[y+1] = (self.xy[y] + self.xy[y+1]) / 2
                self.detections[i] = (-1,-1)

    def translate(self):
        results = []
        for y,i in self.detections:
            if y >= 0:
                row = self.xy[y]
                row = np.reshape(row, (1, row.shape[0], 1))
                t = self.trans_model.predict(row)[0]
                results.append((y, bins2msg(t)))
        return results

def bins2msg(t):
    nbins = t.shape[0]
    # pick the best choice for each bin
    msg = ['.'] * nbins
    for j in range(0, nbins):
        k = np.argmax(t[j])
        if k>0:
            msg[j] = TOKENS[k]
    return ''.join(msg)


import itertools
import operator
import re

import dataclasses
from dataclasses import dataclass, field
from typing import List, Tuple, Iterable, Dict, Optional, Set

from robotoff import settings
from robotoff.ml.langid import DEFAULT_LANGUAGE_IDENTIFIER, LanguageIdentifier
from robotoff.products import ProductDataset
from robotoff.taxonomy import TaxonomyType, get_taxonomy
from robotoff.utils import get_logger, text_file_iter
from robotoff.utils.cache import CachedStore
from robotoff.utils.es import generate_msearch_body
from robotoff.utils.text import FR_NLP_CACHE
from robotoff.utils.types import JSONType

from spacy.util import get_lang_class

logger = get_logger(__name__)

SPLITTER_CHAR = {"(", ")", ",", ";", "[", "]", "-", "{", "}"}

# Food additives (EXXX) may be mistaken from one another, because of their edit distance proximity
BLACKLIST_RE = re.compile(r"(?:\d+(?:[,.]\d+)?\s*%)|(?:[0-9])(?![\w-])")
PUNCTUATION_BLACKLIST_RE = re.compile(r"[_•:]")
E_BLACKLIST_RE = re.compile(r"(?<!\w)(?:E ?\d{3,5}[a-z]*)")
OffsetType = Tuple[int, int]

class TokenLengthMismatchException(Exception):
    pass

def get_fr_known_tokens() -> Set[str]:
    tokens = set(text_file_iter(settings.INGREDIENT_TOKENS_PATH, comment=False))
    tokens = tokens.union(set(text_file_iter(settings.FR_TOKENS_PATH, comment=False)))
    return tokens

FR_KNOWN_TOKENS = CachedStore(get_fr_known_tokens)

def extract_ingredients_from_taxonomy(lang: str):
    taxonomy = get_taxonomy(TaxonomyType.ingredient.name)
    ingredients = set()
    for key, node in taxonomy.nodes.items():
        synonyms: List[str] = node.get_synonyms(lang)

        for synonym in synonyms:
            ingredients.add(synonym.lower())

    return ingredients

def extract_tokens_ingredients_from_taxonomy(lang: str):
    ingredients = extract_ingredients_from_taxonomy(lang)
    nlp_class = get_lang_class(lang)
    nlp = nlp_class()
    tokens = set()

    for doc in nlp.pipe(texts=ingredients):
        for token in doc:
            tokens.add(token.orth_)

    return tokens

def dump_token_ingredients_from_taxonomy(lang: str):
    tokens = sorted(extract_tokens_ingredients_from_taxonomy(lang))

    with settings.INGREDIENT_TOKENS_PATH.open("w") as f:
        for token in tokens:
            f.write(token + "\n")

@dataclass
class Ingredients:
    text: str
    normalized: str
    offsets: List[OffsetType] = field(default_factory=list)

    def iter_normalized_ingredients(self) -> Iterable[str]:
        for start, end in self.offsets:
            yield self.normalized[start:end]

    def get_ingredient(self, index) -> str:
        start, end = self.offsets[index]
        return self.text[start:end]

    def get_normalized_ingredient(self, index) -> str:
        start, end = self.offsets[index]
        return self.normalized[start:end]

    def ingredient_count(self) -> int:
        return len(self.offsets)

@dataclass
class TermCorrection:
    original: str
    correction: str
    start_offset: int
    end_offset: int
    is_valid: bool = True

@dataclass
class Correction:
    term_corrections: List[TermCorrection]
    score: int

def normalize_ingredients(ingredient_text: str):
    normalized = ingredient_text

    for regex in (E_BLACKLIST_RE, BLACKLIST_RE, PUNCTUATION_BLACKLIST_RE):
        while True:
            try:
                match = next(regex.finditer(normalized))
            except StopIteration:
                break

            if match:
                start = match.start()
                end = match.end()
                normalized = normalized[:start] + " " * (end - start) + normalized[end:]
            else:
                break

    return normalized

def process_ingredients(ingredient_text: str) -> Ingredients:
    offsets = []
    chars = []

    normalized = normalize_ingredients(ingredient_text)
    start_idx = 0

    for idx, char in enumerate(normalized):
        if char in SPLITTER_CHAR:
            offsets.append((start_idx, idx))
            start_idx = idx + 1
            chars.append(" ")
        else:
            chars.append(char)

    if start_idx != len(normalized):
        offsets.append((start_idx, len(normalized)))

    normalized = "".join(chars)
    return Ingredients(ingredient_text, normalized, offsets)

def generate_corrections(
    client, ingredients_text: str, lang: Optional[str] = None, **kwargs
) -> List[Correction]:
    if lang is None:
        language_identifier: LanguageIdentifier = DEFAULT_LANGUAGE_IDENTIFIER.get()
        predicted_languages = language_identifier.predict(
            ingredients_text.lower(), threshold=0.5
        )

        if predicted_languages and predicted_languages[0].lang != "fr":
            predicted_language = predicted_languages[0]
            logger.info(
                "Predicted language is not 'fr': {} "
                "(confidence: {})\n{}".format(
                    predicted_language.lang,
                    predicted_language.confidence,
                    ingredients_text,
                )
            )
            return []

    corrections = []
    ingredients: Ingredients = process_ingredients(ingredients_text)
    normalized_ingredients: Iterable[str] = ingredients.iter_normalized_ingredients()

    for idx, suggestions in enumerate(
        _suggest_batch(client, normalized_ingredients, **kwargs)
    ):
        offsets = ingredients.offsets[idx]
        normalized_ingredient = ingredients.get_normalized_ingredient(idx)
        options = suggestions["options"]

        if not options:
            continue

        option = options[0]
        original_tokens = analyze(client, normalized_ingredient)
        suggestion_tokens = analyze(client, option["text"])
        try:
            term_corrections = format_corrections(
                original_tokens, suggestion_tokens, offsets[0]
            )

            for term_correction in term_corrections:
                term_correction.is_valid = is_valid_correction(term_correction)

            corrections.append(Correction(term_corrections, option["score"]))

        except TokenLengthMismatchException:
            # logger.warning("The original text and the suggestions must have the same number "
            #               "of tokens: {} / {}".format(original_tokens, suggestion_tokens))
            continue

    return corrections

def is_valid_correction(
    correction: TermCorrection, plural: bool = True, original_known: bool = True
) -> bool:
    if plural and is_plural_correction(correction):
        return False

    if original_known and is_original_ingredient_known(correction.original):
        return False

    # Tokens with numbers are tricky to correct
    if any(x.isdigit() for x in correction.correction):
        return False

    return True

def is_plural_correction(correction: TermCorrection) -> bool:
    original_str = correction.original.lower()
    correction_str = correction.correction.lower()

    return (
        len(original_str) > len(correction_str)
        and original_str.endswith("s")
        and correction_str == original_str[:-1]
    ) or (
        len(correction_str) > len(original_str)
        and correction_str.endswith("s")
        and original_str == correction_str[:-1]
    )

def is_original_ingredient_known(text: str):
    nlp = FR_NLP_CACHE.get()
    known_tokens = FR_KNOWN_TOKENS.get()

    for token in nlp(text):
        if token.lower_ not in known_tokens:
            return False

    return True

def generate_corrected_text(corrections: List[TermCorrection], text: str):
    valid_corrections = [c for c in corrections if c.is_valid]
    sorted_corrections = sorted(
        valid_corrections, key=operator.attrgetter("start_offset")
    )
    corrected_fragments = []

    last_correction = None
    for correction in sorted_corrections:
        if last_correction is None:
            corrected_fragments.append(text[: correction.start_offset])
        else:
            corrected_fragments.append(
                text[last_correction.end_offset : correction.start_offset]
            )

        corrected_fragments.append(correction.correction)
        last_correction = correction

    if last_correction is not None:
        corrected_fragments.append(text[last_correction.end_offset :])

    return "".join(corrected_fragments)

def format_corrections(
    original_tokens: List[Dict], suggestion_tokens: List[Dict], offset: int = 0
) -> List[TermCorrection]:
    corrections: List[TermCorrection] = []

    if len(original_tokens) != len(suggestion_tokens):
        raise TokenLengthMismatchException()

    for original_token, suggestion_token in zip(original_tokens, suggestion_tokens):
        original_token_str = original_token["token"]
        suggestion_token_str = suggestion_token["token"]

        if original_token_str.lower() != suggestion_token_str:
            if original_token_str.isupper():
                token_str = suggestion_token_str.upper()
            elif original_token_str.istitle():
                token_str = suggestion_token_str.capitalize()
            else:
                token_str = suggestion_token_str

            token_start = original_token["start_offset"]
            token_end = original_token["end_offset"]
            corrections.append(
                TermCorrection(
                    original=original_token_str,
                    correction=token_str,
                    start_offset=offset + token_start,
                    end_offset=offset + token_end,
                )
            )

    return corrections

def suggest(text: str, client, **kwargs) -> Dict:
    corrections = generate_corrections(client, text, **kwargs)
    term_corrections = list(
        itertools.chain.from_iterable((c.term_corrections for c in corrections))
    )

    return {
        "corrections": [dataclasses.asdict(c) for c in term_corrections],
        "text": text,
        "corrected": generate_corrected_text(term_corrections, text),
    }

def analyze(client, ingredient_text: str):
    r = client.indices.analyze(
        index=settings.ELASTICSEARCH_PRODUCT_INDEX,
        body={"tokenizer": "standard", "text": ingredient_text},
    )
    return r["tokens"]

def _suggest_batch(client, texts: Iterable[str], **kwargs) -> List[Dict]:
    suggester_name = "autocorrect"
    index_name = kwargs.pop("index_name", settings.ELASTICSEARCH_PRODUCT_INDEX)
    queries = (
        generate_suggest_query(text, name=suggester_name, **kwargs) for text in texts
    )
    body = generate_msearch_body(index_name, queries)
    response = client.msearch(body=body, doc_type=settings.ELASTICSEARCH_TYPE)

    suggestions = []

    for r in response["responses"]:
        if r["status"] != 200:
            root_cause = response["error"]["root_cause"][0]
            error_type = root_cause["type"]
            error_reason = root_cause["reason"]
            print("Elasticsearch error: {} [{}]" "".format(error_reason, error_type))
            continue

        suggestions.append(r["suggest"][suggester_name][0])

    return suggestions

def generate_suggest_query(
    text,
    confidence=1,
    size=1,
    min_word_length=4,
    suggest_mode="missing",
    name="autocorrect",
    reverse: bool = True,
):
    direct_generators = [
        {
            "field": "ingredients_text_fr.trigram",
            "suggest_mode": suggest_mode,
            "min_word_length": min_word_length,
        }
    ]

    if reverse:
        direct_generators.append(
            {
                "field": "ingredients_text_fr.reverse",
                "suggest_mode": suggest_mode,
                "min_word_length": min_word_length,
                "pre_filter": "reverse",
                "post_filter": "reverse",
            },
        )
    return {
        "suggest": {
            "text": text,
            name: {
                "phrase": {
                    "confidence": confidence,
                    "field": "ingredients_text_fr.trigram",
                    "size": size,
                    "gram_size": 3,
                    "direct_generator": direct_generators,
                    "smoothing": {"laplace": {"alpha": 0.5}},
                }
            },
        }
    }

def predict_insight(client, text: str, barcode: str, **kwargs) -> Optional[JSONType]:
    corrections = generate_corrections(client, text, **kwargs)

    if not corrections:
        return None

    term_corrections = list(
        itertools.chain.from_iterable((c.term_corrections for c in corrections))
    )

    return {
        "corrections": [dataclasses.asdict(c) for c in term_corrections],
        "text": text,
        "corrected": generate_corrected_text(term_corrections, text),
        "barcode": barcode,
        "lang": "fr",
        "index_name": kwargs.get("index_name", "product"),
    }

def generate_insights(
    client, max_errors: Optional[int] = None, **kwargs
) -> Iterable[JSONType]:
    dataset = ProductDataset(settings.JSONL_DATASET_PATH)

    product_iter = (
        dataset.stream()
        .filter_by_country_tag("en:france")
        .filter_text_field("lang", "fr")
        .filter_nonempty_text_field("ingredients_text_fr")
        .iter()
    )

    if max_errors is not None:
        product_iter = (
            p
            for p in product_iter
            if int(p.get("unknown_ingredients_n", 0)) <= max_errors
        )

    for product in product_iter:
        text = product["ingredients_text_fr"]
        barcode = product["code"]
        insight = predict_insight(client, text, barcode=barcode, **kwargs)

        if insight:
            yield insight


from src import source_kitten
from helpers import path_helper
from helpers import file_contents_helper
import unittest
import time

class TestSourceKitten(unittest.TestCase):
    # Here we test (within the "Monkey" example) that
    #
    # print("Eating the \(banana.
    #                           ^
    # comes up with 2 suggestions:
    # - color (String)
    # - flavor (Int)
    #
    # Formatted as a sublime autocomplete
    def test_complete_simple(self):
        offset = 121
        project_directory = path_helper.monkey_example_directory()
        file = project_directory + "/Monkey.swift"
        text = file_contents_helper.read(file)
        output = source_kitten.complete(offset, file, project_directory, text)

        expectation = [
            ["color\tString", "color"],
            ["flavor\tInt", "flavor"],
        ]

        self.assertEqual(len(output), 2)
        self.assertEqual(output[0]["name"], "color")
        self.assertEqual(output[0]["typeName"], "String")
        self.assertEqual(output[1]["name"], "flavor")
        self.assertEqual(output[1]["typeName"], "Int")

    # When a variable is changed from what is persisted on the file system
    #
    # (i.e. test to ensure unsaved autocomplete is working ok)
    def test_complete_changed_code_on_unsaved(self):
        offset = 162
        project_directory = path_helper.monkey_example_directory()
        file = project_directory + "/Monkey.swift"
        text = file_contents_helper.read(file)
        output = source_kitten.complete(offset, file, project_directory, text)
        self.assertTrue(len(output) > 0)

        text = text.replace("consumedBananas", "consumedBananaz", 1)
        output = source_kitten.complete(offset, file, project_directory, text)
        self.assertTrue(len(output) == 0)

        text = text.replace("consumedBananas", "consumedBananaz", 1)
        output = source_kitten.complete(offset, file, project_directory, text)
        self.assertTrue(len(output) > 0)

    # Midword completions. e.g:
    #
    # print("Eating the \(banana.co
    #                             ^
    def test_complete_midword(self):
        offset = 123

        project_directory = path_helper.monkey_example_directory()
        file = project_directory + "/Monkey.swift"
        text = file_contents_helper.read(file)
        output = source_kitten.complete(offset, file, project_directory, text)

        self.assertTrue(len(output) > 0)
        self.assertEqual(output[0]["name"], "color")
        self.assertEqual(output[0]["typeName"], "String")

    # There's not always a `.` preceeding a possible autocomplete
    #
    # struct Banana {
    #     let color: Strin
    #                    ^
    def test_complete_midword_type(self):
        offset = 80

        project_directory = path_helper.monkey_example_directory()
        file = project_directory + "/Banana.swift"
        text = file_contents_helper.read(file)
        output = source_kitten.complete(offset, file, project_directory, text)

        self.assertTrue(len(output) > 0)

        found_string_as_autocomplete = False
        for entry in output:
            if entry["name"] == "String" and entry["typeName"] == "String":
                found_string_as_autocomplete = True

        self.assertTrue(found_string_as_autocomplete)

    # Test project with spaces in subdirectory name
    def test_complete_spaced_project(self):
        project_directory = path_helper.spaced_example_directory()
        file = ""
        text = "\"\"."
        offset = 3
        output = source_kitten.complete(offset, file, project_directory, text)

        self.assertTrue(len(output) > 0)

    # Tests with UIKit import to check that it works
    #
    # Because it can be slow, also includes printout of how long it takes to autocomplete
    # twice (so as to also test caching)
    def test_complete_performance(self):
        text = "import UIKit; class Testing() { func aFunction() {  } }"
        offset = 51
        file = ""
        project_directory = "/dev/null"

        start_time = int(round(time.time() * 1000))
        output = source_kitten.complete(offset, file, project_directory, text)
        end_time = int(round(time.time() * 1000))

        print("source_kitten.complete - Performance test (1st): ", end_time - start_time, 'ms')

        start_time = int(round(time.time() * 1000))
        source_kitten.complete(offset, file, project_directory, text)
        end_time = int(round(time.time() * 1000))

        print("source_kitten.complete - Performance test (2nd): ", end_time - start_time, 'ms')

        self.assertTrue(len(output) > 0)
        ui_view_controller_entries = filter(lambda n: n["name"] == "UIViewController", output)
        self.assertTrue(len(list(ui_view_controller_entries)) > 0)

    # Cursor popover
    # eat(_ banana: Banana) {
    #                 ^
    def test_cursor_info(self):
        offset = 78
        project_directory = path_helper.monkey_example_directory()
        file = project_directory + "/Monkey.swift"
        text = file_contents_helper.read(file)

        output = source_kitten.cursor_info(offset, file, project_directory, text)

        self.assertEqual(output["key.name"], "Banana")
        self.assertEqual(output["key.typename"], "Banana.Type")
        self.assertEqual(output["key.filepath"], project_directory + "/Banana.swift")
        self.assertEqual(output["key.offset"], 7)
        self.assertEqual(output["key.length"], 6)

    def test_cursor_info_unsaved(self):
        offset = 31
        project_directory = path_helper.monkey_example_directory()
        file = project_directory + "/Monkey.swift"
        text = file_contents_helper.read(file)
        text = text.replace("consumedBananas", "consumedBananaz", 1)

        output = source_kitten.cursor_info(offset, file, project_directory, text)

        self.assertEqual(output["key.name"], "consumedBananaz")

    # Issue raised by GitHub user @ccampbell
    # https://github.com/Dan2552/SourceKittenSubl/issues/19
    def test_copyright_symbol(self):
        file = ""
        text = "// ©©\nvar test = 10\ntest."
        offset = len(text)
        project_directory = "/dev/null"
        output = source_kitten.complete(offset, file, project_directory, text)

        self.assertTrue(len(list(output)) > 0)


import json
import logging
import os
import requests
import uuid

from typing import Dict

LOGGER = logging.getLogger(__name__)

LOGGER.setLevel(logging.INFO)

class TestGCWebhook:

    def setup(self):
        self.tenant = os.getenv("TENANT", "rapha")
        self.stage = os.getenv("STAGE", "x")
        self.base_url = f'https://{self.tenant}.ps.{self.stage}.newstore.net'

        LOGGER.info(f'Setting up test with base url: {self.base_url}')

    def issue_gc(self, balance: Dict):
        ns_request = {
            "amount": {
                "value": balance.get("amount"),
                "currency": balance.get("currency")
            },
            "customer": {
                "id": "34683bfjbfjbdfkbfk",
                "name": "Jay DoubleYou",
                "email": "john_doe@ns.com"
            },
            "idempotence_key": "NewStore_Quajoo-" + uuid.uuid4().hex
        }
        r = requests.post(url=f'{self.base_url}/adyen_gift_card/gift_card/issue',
                          json=ns_request
                          )
        assert r.status_code == 200

        response = r.json()
        LOGGER.info(f"Issued gc: {json.dumps(response, indent=4)}")
        return r.json()

    def test_check_balance(self):
        balance = {"amount": 22.22, "currency": "USD"}
        card_issued = self.issue_gc(balance)
        r = requests.post(url=f'{self.base_url}/adyen_gift_card/gift_card/balance',
                          json=card_issued.get("identifier"))
        assert r.status_code == 200
        response = r.json()

        LOGGER.info(f"got gc balance: {json.dumps(response, indent=4)}")

        assert response.get("value") == balance.get("amount")


from itertools import permutations

def is_Prime(number):
    if number == 1 or number == 0:
        return False
    for i in range(2, int(number ** 0.5) + 1):
        if number % i == 0:
            return False
    return True

def solution(numbers):
    answer = set()
    number = list(map(str, numbers))
    comb_number = []

    for i in range(1, len(number) + 1):
        tmp = list(permutations(number, i))
        for x in tmp:
            comb_number.append(int(''.join(x)))

    for x in comb_number:
        if is_Prime(x):
            answer.add(x)

    return len(answer)


#!/usr/bin/env python3
"""Convert font to images of letters."""
import sys
import os
from PIL import Image, ImageFont, ImageDraw

LETTER_SIZE = 60

try:
    font_file = sys.argv[1]
    output_folder = sys.argv[2]
except IndexError:
    sys.stderr.write("Usage: {} [ttf file] [output folder]\n".format(sys.argv[0]))
    sys.exit(0)

# use a truetype font
font_name = os.path.basename(font_file).split(".")[0]
font = ImageFont.truetype(font_file, LETTER_SIZE - 2)
im = Image.new("RGB", (LETTER_SIZE, LETTER_SIZE))
draw = ImageDraw.Draw(im)
symbols = [ord(c) for c in "!?.,'[]()"]
up_letters_cyr = list(range(ord("А"), ord("Я") + 1))
lo_letters_cyr = list(range(ord("а"), ord("я") + 1))
up_letters_lat = list(range(ord("A"), ord("Z") + 1))
lo_letters_lat = list(range(ord("a"), ord("z") + 1))
numbers = list(range(ord("0"), ord("9") + 1))
characters = up_letters_lat + lo_letters_lat \
             + up_letters_cyr + lo_letters_cyr \
             + numbers

for code in characters:
    w, h = draw.textsize(chr(code), font=font)
    im = Image.new("RGB", (w, h), color="#FFFFFF")
    draw = ImageDraw.Draw(im)
    char = chr(code)
    draw.text((0, 0), char, font=font, fill="#000000")
    if char.islower():
        char += "_lo"
    im.save(f"letters/{font_name}_{char}.png")


def Euclide(a, b):
	# return (x1, x2, x3)
	# With x1 PGCD(a,b)
	# x2 first bezout coef
	# x3 second bezout coef

	(a, u0, v0, b, u1, v1) = (a, 1, 0, b, 0, 1)

	while b != 0:
		(a_pred, u0_pred, v0_pred, b_pred, u1_pred, v1_pred) = (a, u0, v0, b, u1, v1)
		q = int(a // b)
		a = b_pred
		u0 = u1_pred
		v0 = v1_pred
		b = a_pred - q*b_pred
		u1 = u0_pred - q*u1_pred
		v1 = v0_pred - q*v1_pred

	return (a, u0, v0)

def GloutonOptimal(liste,s):
	i = len(liste)-1
	liste2 = []
	for elem in liste :
		liste2.append(0)
	while(i >= 0):
		if(liste[i]<=s):
			s = s-liste[i]
			liste2[i] = 1
		else:
			liste2[i] = 0
		i = i-1
	return liste2

def DechiffrementMerkle(b,c,v):
	return GloutonOptimal(b,c/v)

liste = [1,2,4,8,16,32,64,128,256,512]
s = 3

#print(GloutonOptimal(liste,s))
v = 1
u = 600
for i in range(u):
	if Euclide(u,i) == 1:
		v = i
		break

a = []

for elem in liste :
	a.append(elem * v)

m = [0,1,0,1,0,1,0,1,0,1]

c = 0

for i in range(len(m)):
	c += m[i]*a[i]

print(DechiffrementMerkle(liste, c, v))


import tkinter as tk
from tkinter import ttk

class ToolbarWidget:
    def _bind_hovers(self):
        self.default_background = self['background']
        self.config(relief=tk.FLAT, bd=0, activebackground="#d8e6f2")
        self.bind("<Enter>", self.on_enter)
        self.bind("<Leave>", self.on_leave)

    def on_enter(self, e):
        if self['state'] == tk.NORMAL:
            self['background'] = self['activebackground']

    def on_leave(self, e):
        self['background'] = self.default_background

    def pack(self, side=tk.LEFT, anchor=tk.NW, padx=2, **kw):
        super().pack(side=side, anchor=anchor, padx=padx, **kw)

class ToolbarButton(ToolbarWidget, tk.Button):
    def __init__(self, master, **kw):
        tk.Button.__init__(self, master=master, relief=tk.FLAT, bd=0, activebackground="#d8e6f2", **kw)
        self.default_background = self['background']
        self._bind_hovers()

class ToolbarMenubutton(ToolbarWidget, tk.Menubutton):
    def __init__(self, master, **kw):
        tk.Menubutton.__init__(self, master, **kw)
        self._bind_hovers()

#https://stackoverflow.com/questions/59763822/show-combobox-drop-down-while-editing-text-using-tkinter

class DropdownCombobox(ttk.Combobox):
    def _tk(self, cls, parent):
        obj = cls(parent)
        obj.destroy()
        if cls is tk.Toplevel:
            obj._w = self.tk.call('ttk::combobox::PopdownWindow', self)
        else:
            obj._w = '{}.{}'.format(parent._w, 'f.l')
        return obj

    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)
        self.popdown = self._tk(tk.Toplevel, parent)
        self.listbox = self._tk(tk.Listbox, self.popdown)

        self.bind("<KeyPress>", self.on_keypress, '+')
        self.listbox.bind("<Up>", self.on_keypress)

    def on_keypress(self, event):
        if event.widget == self:
            state = self.popdown.state()

            if state == 'withdrawn' \
                    and event.keysym not in ['BackSpace', 'Up']:
                self.event_generate('<Button-1>')
                self.after(0, self.focus_set)

            if event.keysym == 'Down':
                self.after(0, self.listbox.focus_set)

        else:  # self.listbox
            curselection = self.listbox.curselection()

            if event.keysym == 'Up' and (len(curselection) == 0 or curselection[0] == 0):
                self.popdown.withdraw()


import constants
from game.casting.actor import Actor
from game.scripting.action import Action
from game.shared.point import Point

class HandleCollisionsAction(Action):
    """
    An update action that handles interactions between the actors.

    The responsibility of HandleCollisionsAction is to handle the situation when the cycle collides
    with the their trail, or the game is over.

    Attributes:
        _is_game_over (boolean): Whether or not the game is over.
    """

    def __init__(self):
        """Constructs a new HandleCollisionsAction."""
        self._is_game_over = False

    def execute(self, cast, script):
        """Executes the handle collisions action.

        Args:
            cast (Cast): The cast of Actors in the game.
            script (Script): The script of Actions in the game.
        """
        if not self._is_game_over:
            self._handle_segment_collision(cast)
            self._handle_game_over(cast)

    def _handle_segment_collision(self, cast):
        """Identifies when two actors come into contact with each other and handle it appropriately.
           If it is a hazzard, the frog dies, if it is a helper, the frog rides safely

        Args:
            cast (Cast): The cast of Actors in the game.
        """
        cars = cast.get_actors("cars")
        trucks = cast.get_actors("trucks")
        frog = cast.get_first_actor("frog")
        frog.set_off_log_or_turtle()
        logs = cast.get_actors("logs")
        turtles = cast.get_actors("turtles")

        # Test for car collision
        for car in cars:
            carsegs = car.get_segments()
            first_x = carsegs[0].get_position().get_x()
            last_x = carsegs[1].get_position().get_x()
            if (frog.get_position().get_x()+20 >= first_x and
                frog.get_position().get_x()-20 <= last_x and
                frog.get_position().get_y() + 1 == carsegs[0].get_position().get_y()):
                    self._is_game_over = True
                    frog.set_text("*")

        # Test for truck collision
        for truck in trucks:
            trucksegs = truck.get_segments()
            first_x = trucksegs[0].get_position().get_x()
            last_x = trucksegs[3].get_position().get_x()
            if (frog.get_position().get_x()+20 >= first_x and
                frog.get_position().get_x()-20 <= last_x and
                frog.get_position().get_y() + 1 == trucksegs[0].get_position().get_y()):
                    self._is_game_over = True
                    frog.set_text("*")

        # If the frog is in the top half of the screen, the frog dies if not in contact (collision) with a Helper
        if (frog.get_position().get_y() + 1 < 7 * constants.CELL_SIZE and
            frog.get_position().get_y() + 1 > 1 * constants.CELL_SIZE):
            self._is_game_over = True

            # Frog in contact with a Log
            for log in logs:
                logsegs = log.get_segments()
                first_x = logsegs[0].get_position().get_x()
                last_x = logsegs[len(logsegs)-1].get_position().get_x()
                if (frog.get_position().get_x()+20 >= first_x and
                    frog.get_position().get_x()-20 <= last_x and
                    frog.get_position().get_y() + 1 == logsegs[0].get_position().get_y()):
                        self._is_game_over = False
                        frog.set_velocity(logsegs[0].get_velocity())
                        frog.set_on_log_or_turtle()

            # Frog in contact with a turtle
            for turtle in turtles:
                turtlesegs = turtle.get_segments()
                first_x = turtlesegs[0].get_position().get_x()
                last_x = turtlesegs[len(turtlesegs)-1].get_position().get_x()
                if (frog.get_position().get_x()+20 >= first_x and
                    frog.get_position().get_x()-20 <= last_x and
                    frog.get_position().get_y() + 1 == turtlesegs[0].get_position().get_y()):
                        self._is_game_over = False
                        frog.set_velocity(turtlesegs[0].get_velocity())
                        frog.set_on_log_or_turtle()

            if (self._is_game_over == True):
                frog.set_text("*")

    def _handle_game_over(self, cast):
        """Shows the 'game over' message if the frog dies.

        Args:
            cast (Cast): The cast of Actors in the game.
        """
        if self._is_game_over:
            x = int(constants.MAX_X / 2)
            y = 7 * constants.CELL_SIZE
            position = Point(x, y)

            message = Actor()
            message.set_text("Game Over!")
            message.set_position(position)
            cast.add_actor("messages", message)
            lines = cast.get_actors("lines")
            for line in lines:
                cast.remove_actor("lines", line)
            score = cast.get_first_actor("score")
            score.game_over()
            frog = cast.get_first_actor("frog")
            frog.set_game_over()


import requests

## HTTP GET Request
req = requests.get('https://beomi.github.io/beomi.github.io_old/')

## HTML 소스 가져오기
html = req.text
## HTTP Header 가져오기
header = req.headers
## HTTP Status 가져오기 (200: 정상)
status = req.status_code
## HTTP가 정상적으로 되었는지 (True/False)
is_ok = req.ok

print(html)
print(header)
print(status)
print(is_ok)


import matplotlib.pyplot as plt

import networkx as nx
from queue import PriorityQueue
import time

start_time = time.time()

G = nx.Graph()

file = open("data.csv",'r')
lines = file.readlines()

edges = []

for row in range(0,len(lines[0:]),2):
    header = lines[row]
    header = header[:len(header)-1].split(',')
    header[2] = int(header[2])
    edges.append(tuple(header))

#edges = [('a', 'b', 9), ('a', 'd', 2), ('a', 'c', 5), ('d', 'c', 4),
#         ('d', 'b', 6), ('d', 'e', 4), ('e', 'c', 5), ('e', 'b', 5)]

G.add_weighted_edges_from(edges[:1000])

plt.figure(1)

nx.draw_networkx(G, with_labels = True)
plt.savefig("OriginalGraphPrim.png")

print("!---Original Graph---!")
print("Total number of edges: ", int(G.number_of_edges()))

class Prims:
    def __init__(self):
        self.tree = []
        self.table = {}
        self.queTable = {}
        self.que = PriorityQueue()

    def pickRoot(self, node):
        self.rootNode = node

    def initializePriorityQueue(self, listOfNodes):
        self.que.put((0,self.rootNode))
        for node in listOfNodes:
            if node != self.rootNode:
                self.que.put((10000,node))
                self.queTable[node] = 10000

    def initializeTable(self, listOfNodes):
        for node in listOfNodes:
            self.table[node] = '-'

    def primsAlgo(self, listOfEdges, NoOfNodes):
        while not self.que.empty():
            nextVertex = self.que.get()
            self.tree.append(nextVertex[1])
            for edge in listOfEdges:
                if nextVertex[1] in edge:
                    edge = list(edge)
                    edge.remove(nextVertex[1])
                    edge = tuple(edge)
                    if edge[0] not in self.tree:
                        if edge[1] < self.queTable[edge[0]]:
                            self.queTable[edge[0]] = edge[1]
                            self.table[edge[0]] = nextVertex[1]
                            self.que.put((edge[1],edge[0]))
            if(len(self.tree) == NoOfNodes):
                while not self.que.empty():
                    nextVertex = self.que.get()

    def ShowMST(self):
        MST = nx.Graph()

        edges = []

        for vertex in self.table.keys():
            if self.table[vertex] != '-':
                edges.append((vertex,self.table[vertex],self.queTable[vertex]))

        MST.add_weighted_edges_from(edges)
        plt.figure(2)
        nx.draw_spring(MST, with_labels = True)
        plt.savefig("MSTPrim.png")

        print("!---Minimum Spanning Tree---!")
        print("Total number of edges: ", int(MST.number_of_edges()))

MSTPrims = Prims()
MSTPrims.pickRoot('5')
MSTPrims.initializePriorityQueue(list(G.nodes()))
MSTPrims.initializeTable(list(G.nodes()))
MSTPrims.primsAlgo(edges[:1000], int(G.number_of_nodes()))
MSTPrims.ShowMST()
plt.show()
print("\n\nTotal time taken for all processes: ")
print("--- %s seconds ---" % (time.time() - start_time))
file = open("time.csv",'a')
file.write(str(time.time() - start_time))
file.write('\n')
file.close()


# /usr/bin/python3.6
# -*- coding:utf-8 -*-

class Solution(object):
    def compress(self, chars):
        """
        :type chars: List[str]
        :rtype: int
        """
        length = len(chars)
        if length <= 1:
            return length
        last = 0
        old = chars[0]
        i = 1
        while i < len(chars):
            if old == chars[i]:
                last += 1
                chars[i:i+1] = []
            else:
                old = chars[i]
                if last != 0:
                    s = str(last+1)
                    length_insert = len(s)
                    for index in range(length_insert):
                        chars.insert(i, s[length_insert-index-1])
                    i += length_insert+1
                else:
                    i += 1
                last = 0
        if last != 0:
            s = str(last+1)
            length_insert = len(s)
            for index in range(length_insert):
                chars.insert(i, s[length_insert-index-1])
        return len(chars)

def main():
    s = Solution()
    l = ["a","a","b","b","c","c","c"]
    print(s.compress(l), l)
    l = ["a","b","b","c","c","c"]
    print(s.compress(l), l)
    l = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
    print(s.compress(l), l)
    l = ["a","b","b","b","b","b","b","b","b","b","b","b","b","a","a","a","a","a"]
    print(s.compress(l), l)

if __name__ == "__main__":
    main()


def bubbleSort(arr):
    n = len(arr)

    # Traverse through all array elements
    for i in range(n):

        # Last i elements are already in place
        for j in range(0, n-i-1):

            # traverse the array from 0 to n-i-1
            # Swap if the element found is greater
            # than the next element
            if arr[j] > arr[j+1] :
                arr[j], arr[j+1] = arr[j+1], arr[j]

def insertionSort(arr):

    # Traverse through 1 to len(arr)
    for i in range(1, len(arr)):

        key = arr[i]

        # Move elements of arr[0..i-1], that are
        # greater than key, to one position ahead
        # of their current position
        j = i-1
        while j >=0 and key < arr[j] :
                arr[j+1] = arr[j]
                j -= 1
        arr[j+1] = key

def selectionSort(arr):
    # Traverse through all array elements
    for i in range(len(arr)):

        # Find the minimum element in remaining
        # unsorted array
        min_idx = i
        for j in range(i+1, len(arr)):
            if arr[min_idx] > arr[j]:
                min_idx = j

        # Swap the found minimum element with
        # the first element
        arr[i], arr[min_idx] = arr[min_idx], arr[i]
        print(arr)

def main():
    arr = [9, 12, 4, 19, 10, 3]
    print('Original List: ', arr)
    print('List sorted by Bubble Sort: ', bubbleSort(arr))
    print('List sorted by Insertion Sort: ', insertionSort(arr))
    print('List sorted by Selection Sort: ', selectionSort(arr))

main()


#!/usr/bin/env python
#
# Author: Greg Hellings - <ghelling@redhat.com> or <greg.hellings@gmail.com>
#
# Module to configure users in Jenkins authorized to use CLI
import xml.etree.ElementTree as ET
import os
from ansible.module_utils.basic import AnsibleModule

DOCUMENTATION = """
---
version_added: "2.1"
module: jenkins_cli_user
short_description: configure Jenkins CLI users with pub key
description:
  - This module configures admin users in Jenkins to utilize the specified
    SSH pubkey. Requires that role-based authentication be enabled and that
    a user be configured as an admin

options:
  jenkins_home:
    description:
     The root directory for the Jenkins install
    required: true
  jenkins_user:
    description:
      The name of the user to configure the SSH key for
  key_file:
    description:
      Path to the SSH keyfile to be listed as authorized
    required: true
  state:
    description:
      Currently limited to "present" - will create the user
    required: false

author: Gregory Hellings
"""

class UserList(object):
    def __init__(self, user_path):
        config = os.path.join(user_path, "users.xml")
        self.root = ET.parse(config).getroot()

    @property
    def users(self):
        if not hasattr(self, "_users"):
            mapping = self.root.find("idToDirectoryNameMap")
            if not mapping:
                return {}
            entries = mapping.getiterator("entry")
            if not entries:
                return {}
            users = {}
            for entry in entries:
                users[entry[0].text] = entry[1].text
            self._users = users
        return self._users

class User(object):
    def __init__(self, user_path, dirname):
        self.config = os.path.join(user_path, dirname, "config.xml")
        self.document = ET.parse(self.config)
        self.root = self.document.getroot()

    @property
    def keys(self):
        properties = self.root.find("properties")
        keys = properties.getiterator("authorizedKeys")
        return keys

    def add_key(self, pub_key):
        # If keys exist
        if self.keys:
            for key in self.keys:
                if pub_key not in str(key.text):
                    # No actual keys there, so create it, otherwise prepend
                    if key.text is None:
                        key.text = pub_key
                    else:
                        key.text = str(key.text) + "\n" + pub_key
                    return True
        # No keys exist
        else:
            properties = self.root.find("properties")
            xml = "org.jenkinsci.main.modules.cli.auth.ssh.UserPropertyImpl"
            ssh_auth = ET.SubElement(properties, xml)
            auth_key = ET.SubElement(ssh_auth, "authorizedKeys")
            auth_key.text = pub_key
            return True
        return False

    def save(self):
        self.document.write(self.config, encoding="UTF-8")

def main():
    module = AnsibleModule(
        argument_spec={
            "jenkins_home": {"required": True},
            "jenkins_user": {"required": True},
            "key": {"required": True},
            "state": {"choices": ["present"], "default": "present"},
        },
        supports_check_mode=False,
    )
    params = type("Params", (object,), module.params)
    user_path = os.path.join(params.jenkins_home, "users")
    changed = False

    # Check if user exists and find directory mapping
    users = UserList(user_path)
    if params.jenkins_user not in users.users.keys():
        module.fail_json(msg="No such user found." + users.users.keys())

    # Check user for configured key and add it
    user = User(user_path, users.users[params.jenkins_user])
    changed = user.add_key(params.key)
    if changed:
        user.save()
    module.exit_json(changed=changed)

main()


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

fname = sys.argv[1] # get filename from argument
samples = []
for s in sys.argv[2:len(sys.argv)]:
    samples.append(s)

data = open(fname, "r") # Open file from BMG (export as table) and store in a list
file_stored = []
for i in data:
    file_stored.append(i.split(","))
data.close()

Sample_names = []
y = []
check = False
for i in file_stored:
    if check:
        Sample_names.append(i[1] + i[2])
        y.append(i[3:len(i)-1] + [i[len(i)-1].split("\n")[0]])
    elif i[1] == " Time":
        check = True
        continue
for i in Sample_names:
    print(i)

times = file_stored[1]
times = times[3:len(times)]

x = []

for i in times:
    st = i.split(" ")
    if st[3] == "":
        x.append(int(st[1])*60)
    else:
        x.append(int(st[1])*60 + int(st[3]))

x_int = []
for i in x:
    x_int.append(int(i))

y_int = []
for i in y:
    ySub_int = []
    for j in i:
        ySub_int.append(int(j))
    y_int.append(ySub_int)

Sample_Ids = []
Sample_rep_n = []
for i in Sample_names:
    if not i in Sample_Ids:
        Sample_Ids.append(i)
        Sample_rep_n.append(Sample_names.count(i))

y_Av = []
y_Std = []

for i in Sample_Ids:
    reps = []
    reps_Av = []
    reps_Std = []
    for j in range(0,len(Sample_names)):
        if Sample_names[j] == i:
            reps.append(y_int[j])
    for r in range(0,len(reps[0])):
        avs  = []
        for z in range(0,len(reps)):
            avs.append(float(reps[z][r]))
        reps_Av.append(np.average(avs))
        reps_Std.append(np.std(avs))
    y_Av.append(reps_Av)
    y_Std.append(reps_Std)

#### Average

sample_show = []

n = 0
leg = []
for i in Sample_Ids:
    for j in samples:
        if j in i:
            sample_show.append(n)
            leg.append(i)
    n += 1

colors = []

for i in sample_show:
    a = plt.errorbar(x,y_Av[i],yerr=y_Std[i],capsize=2)
    for c in range(0,Sample_rep_n[i]):
        colors.append(a.lines[0].get_color())
plt.legend(leg)
sample_show_scatter = []

n = 0
for i in Sample_names:
    for j in samples:
        if j in i:
            sample_show_scatter.append(n)
    n += 1
m = 0
for i in sample_show_scatter:
    plt.scatter(x,y_int[i], facecolor = colors[m])
    m += 1

plt.show()

#### Raw Data

# sample_show = []
#
# n = 0
# for i in Sample_names:
#     for j in samples:
#         if j in i:
#             sample_show.append(n)
#     n += 1
#
# for i in sample_show:
#     plt.plot(x,y_int[i])
# plt.show()


#!/usr/bin/env python
import npyscreen
import curses

class ActionControllerSearch(npyscreen.ActionControllerSimple):
    def create(self):
        self.add_action('^/.*', self.set_search, True)

    def set_search(self, command_line, widget_proxy, live):
        self.parent.value.set_filter(command_line[1:])
        self.parent.wMain.values = self.parent.value.get()
        self.parent.wMain.display()

class FmSearchActive(npyscreen.FormMuttActiveTraditional):
    ACTION_CONTROLLER = ActionControllerSearch

class TestApp(npyscreen.NPSApp):
    def main(self):
        F = FmSearchActive()
        F.wStatus1.value = "Status Line "
        F.wStatus2.value = "Second Status Line "
        F.value.set_values([str(x) for x in range(500)])
        F.wMain.values = F.value.get()

        F.edit()

if __name__ == "__main__":
    App = TestApp()
    App.run()


import sys
c_del = 2
c_ins = 2

def lire_mots(filename):
    """ Prend en entrée le nom d'un fichier de type adn
        et renvoie les 2 mots associés à ce fichier sous
        forme de tableau numpy """

    with open(filename, 'r') as f:
        #Supression des deux premières lignes
        f.readline()
        f.readline()

        #Lecture des deux mots sous forme de chaine de caractères
        mot1 = f.readline()
        mot2 = f.readline()

    #Conversion des mots en liste
    mot1 = mot1.split(' ')
    mot2 = mot2.split(' ')

    #Suppression du \n terminal
    del mot1[-1]
    del mot2[-1]

    return (mot1, mot2)

def sub(a,b):
    """ Prend en entrée deux lettres et
        renvoie le coût de substition """

    if a == b:
        # Même lettre : coût nul
        return 0

    if a in ['A', 'T']:
        if b in ['A', 'T']:
            # Paire concordante
            return 3
        return 4

    if b in ['G', 'C']:
        # Paire concordante
        return 3

    return 4

def dist_naif(x,y):
    """ Prend en entrée deux mots sous forme de tableau
        et lance le premier appel de dist_naif_rec.
        Renvoie la distance d'édition minimale entre x et y """
        # Il faut faire le premier appel avec les indices -1, car à l'itération on remplit la ligne i+1
    return dist_naif_rec(x,y,-1,-1,0,-1)

def dist_naif_rec(x,y,i,j,c,dist):
    """ Prend en entée deux mots sous forme de tableau,
        deux indices entiers i et j,
        c coût de l'alignement de x[:i],y[:j]
        dist le coût du meilleur alignement de (x,y) avant appel
        Renvoie le coût du meilleur alignement après appel """

    # Traduction du pseudo-code donné dans l'énoncé en python
    if i == len(x)-1 and j == len(y)-1 :
        if dist == -1 or c < dist :
            return c

    if i < len(x)-1 and j < len(y)-1:
        dist = dist_naif_rec(x,y,i+1,j+1,c+sub(x[i+1], y[j+1]), dist)

    if i < len(x)-1:
        dist = dist_naif_rec(x,y,i+1,j,c+c_del, dist)

    if j < len(y)-1:
        dist = dist_naif_rec(x,y,i,j+1,c+c_ins, dist)

    return dist

def main():
    (x,y) = lire_mots(sys.argv[1])
    dist = dist_naif(x,y)
    print("Distance : {}".format(dist))

if __name__ == "__main__":
    main()


import tweepy
import configparser
from datetime import datetime
from dateutil import tz
from threading import Thread

config = configparser.ConfigParser(interpolation=None)
config.read('twitterkeys.ini')

api_key = config["twitter"]["APIKey"]
api_key_secret = config["twitter"]["APIKeySecret"]
bearer_token = config["twitter"]["BearerToken"]
access_token = config["twitter"]["AccessToken"]
access_token_secret = config["twitter"]["AccessTokenSecret"]

auth = tweepy.OAuth1UserHandler(api_key, api_key_secret, access_token, access_token_secret)
client = tweepy.Client(bearer_token=bearer_token, consumer_key=api_key, consumer_secret=api_key_secret, access_token=access_token, access_token_secret=access_token_secret)
api = tweepy.API(auth)

today = datetime.now()
today = today.replace(hour=7, minute=30)
try:
    tweets = client.get_users_tweets(id=3690023483, exclude="replies", start_time=today)
except:
    print("could not get")
class StockEntry():
    def __init__(self, ticker, buy_type, buy_value, buy_price):
        self.ticker = ticker
        self.buy_price = buy_price
        if "c" in buy_type:
            self.buy_type = "call"
        else:
            self.buy_type = "put"
        self.buy_value = buy_value
        self.filled = False
        self.closed = False
        self.option_amount = 0
        self.stop_loss_upper = 0.1
        self.stop_loss_lower = 0.1

    def __str__(self):
        return_string = "Ticker: " + self.ticker + "\n"
        return_string += "Option type: " + self.buy_type + "\n"
        return_string += "Option value: " + self.buy_value + "\n"
        return_string += "Bought price: " + self.buy_price + "\n"
        return return_string

    def set_loss(self, stop_loss):
        self.stop_loss_lower = stop_loss[0]
        self.stop_loss_upper = stop_loss[1]

    def buy(self):
        print("Buying " + self.ticker + "...")

include_lotto = False
stock_entries = []
for tweet in tweets.data:
    if "entry" in str(tweet):
        if "LOTTO" in str(tweet) and not include_lotto:
            pass
        else:
            words = str(tweet).split(" ")
            print(words)
            ticker = words[1]
            buy_type = words[2][-1]
            buy_value = words[2][:-1]
            buy_price = words[4]
            stock_entries.append(StockEntry(ticker, buy_type, buy_value, buy_price))

for entry in stock_entries:
    print(entry)


import re;
import sys;

i = 1;
for line in sys.stdin:
    arr = re.split("[ \t]+", line.strip());
    j = 1;
    for v in arr:
        print("%d %d\t%s" % (i, j, v));
        j += 1;
    i += 1;


from modules.base_classes import *
import modules.comp_map as cmp

def main():
    n = int(input())
    rectangles = [Rectangle(*(map(int, input().split()))) for _ in range(n)]

    m = int(input())
    points = [Point2D(*map(int, input().split())) for _ in range(m)]

    x_values, y_values = cmp.fill_zipped_coord(rectangles)

    matrix = cmp.fill_matrix(x_values, y_values, rectangles)

    counts = cmp.get_count(points, x_values, y_values, matrix)
    return counts

if __name__ == "__main__":
    print(*main())