class Component:
    def __init__(self, name, package=None, **kwargs):
        self.name: str = name
        self.dependencies: [Component] = []
        self.package: str = package
        # describes the instability rating,
        # it's a value between 0 and 1, where 1 means completely
        # unstable and 0 means stable. Stability is defined by
        # |d_out| / |d_out|+|d_in|
        self.instability_rating: float = -1
        self.abstraction_degree: float = -1
        # a bum is a component that depends on this component
        self.bums: [Component] = []
        self.is_abstraction = kwargs.get('is_abstraction', None)

    def __repr__(self):
        return f'{self.name} - Dependencies: {len(self.dependencies)} Bums: {len(self.bums)} Stability: {self.instability_rating} Abstraction Degree: {self.abstraction_degree}' \
               f' abstraction: {self.is_abstraction}'

    def add_dependency(self, dependency):
        self.dependencies.append(dependency)

    def to_dict(self):
        return {
            'name': self.name,
            'package': self.package,
            'is_abstraction': self.is_abstraction,
            'dependencies': [i.name for i in self.dependencies],
            'bums': [i.name for i in self.bums],
            'instability_rating': self.instability_rating,
            'abstraction_degree': self.abstraction_degree
        }


f = open("C:/python_code/new_folder/data_Tex_64.txt")
allPlantInOne = {}
arr = []
for printLinByLine in f:
    a = (printLinByLine.strip().split(','))
    a[0] = a[0].replace(" ", "_")
    if not a[0] in allPlantInOne:
        allPlantInOne[a[0]] = []
    allPlantInOne[a[0]].append(a)
    if not a[0] in arr:
        arr.append(a[0])
arr = sorted(arr)
str = ""
for pName in arr:
    for pAll in allPlantInOne[pName]:
        str += ','.join(pAll)
        str += '\n'
#print(str)

with open('C:/python_code/new_folder/d_tex_ready.txt', 'w') as f:
        f.write(str)


# join() ['a', 'n', 'e', 'm'] ==> a-b-c-d

"""
def myjoin(x_list, del_str):
    output = ""
    for item in  x_list:
        output = output+item+del_str
    print(output)

x = ['a', 'n', 'e', 'm']

myjoin(x, '-')
"""

def myjoin(x_list, del_str):
    output = ""
    for i in range(0, len(x_list)-1):
        output = output+x_list[i]+del_str
    #print(output)
    output = output+x_list[-1]
    return output

x = ['a', 'n', 'e', 'm']

myjoin(x, '-')


# -*- coding: utf-8 -*-
"""
Created on Sat Oct 17 12:07:43 2020

@author: adity
"""

def binarySearch(arr, val):
    upper = len(arr)-1
    lower = 0
    middle = (upper + lower) // 2

    while lower <= upper:

        if arr[middle] < val:
            lower = middle + 1
        elif arr[middle] > val:
            upper = middle - 1
        elif arr[middle] == val:
            return middle
        middle = (upper + lower) // 2
    return "value not found"


"""

In this kata, you will be given a string of text and valid parentheses, such as "h(el)lo".
You must return the string, with only the text inside parentheses reversed, so "h(el)lo" becomes
"h(le)lo". However, if said parenthesized text contains parenthesized text itself, then that too must
reversed back, so it faces the original direction. When parentheses are reversed, they should switch
directions, so they remain syntactically correct (i.e. "h((el)l)o" becomes "h(l(el))o"). This pattern
should repeat for however many layers of parentheses. There may be multiple groups of parentheses at any
level (i.e. "(1) (2 (3) (4))"), so be sure to account for these.

reverse_in_parentheses("h(el)lo") == "h(le)lo"
reverse_in_parentheses("a ((d e) c b)") == "a (b c (d e))"
reverse_in_parentheses("one (two (three) four)") == "one (ruof (three) owt)"
reverse_in_parentheses("one (ruof ((rht)ee) owt)") == "one (two ((thr)ee) four)"

"""
def reverse_in_parentheses(string: str) -> str:
    result = [""]
    i = 0

    for char in string:
        if char == "(":
            # прибавляем +1 если открывается скобка в строке
            i += 1
            result.append("")

        elif char == ")":

            current_string = result.pop()
            # отнимаем -1 если закрывается скобка в строке
            i -= 1
            built_string = "(" + current_string + ")"

            if i % 2 == 1:
                result[i] = built_string + result[i]
            else:
                result[i] += built_string
        else:
            # добавляем в обратном порядке, если у нас нечетное количество скобок
            if i % 2 == 1:
                result[i] = char + result[i]
            # иначе просто прибавляем символ
            else:
                result[i] += char

    return result.pop()


# -*- coding: utf-8 -*-
import os
import sys

file_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(file_dir, "../"))
sys.path.append(os.path.join(file_dir, "../../"))

from graphs import create_graph
from kruskal import kruskal

def kruskal_test(G, expected):
    mst = kruskal(G)

    assert len(mst) == len(G.get_vertices()) - 1

    mst_cost = 0
    unvisited = set(G.get_vertices())
    for u, v in mst:
        unvisited.discard(u)
        unvisited.discard(v)
        mst_cost += G.get_weight(u, v)

    assert len(unvisited) == 0, f"Tkrus is not spanning:  {mst}"

    unvisited = set(G.get_vertices())
    expt_cost = 0
    for u, v in expected:
        unvisited.discard(u)
        unvisited.discard(v)
        expt_cost += G.get_weight(u, v)

    assert len(unvisited) == 0, f"Expected is not spanning:  {expected}"

    assert mst_cost == expt_cost

def test_kruskal_1():
    kruskal_test(
        create_graph(
            ["A", "B", "C", "D", "E", "F", "G", "H"],
            [
                ("A", "B", 4),
                ("A", "C", 6),
                ("A", "D", 1),
                ("B", "C", 8),
                ("C", "D", 5),
                ("C", "F", 2.5),
                ("C", "H", 7),
                ("D", "E", 2),
                ("E", "F", 3),
                ("F", "G", 7.5),
                ("G", "H", 9),
            ],
            directed=False,
        ),
        [
            ("A", "B"),
            ("A", "D"),
            ("D", "E"),
            ("E", "F"),
            ("C", "F"),
            ("C", "H"),
            ("F", "G"),
        ],
    )


from PIL import Image, ImageDraw,ImageFont,ImageEnhance
import numpy
import time,os

class Main:
    def __init__(self):
        self.__sequence = [1,1,2,2,3,4,4,5,5,6,6,7,8,8,9,9]
        self.swapCount = 0
        self.comparisonCount = 0

    def getShuffledArray(self):
        a = numpy.array([])
        for i in range(120):
            for j in self.__sequence:
                a = numpy.append(a, j+i*9)
        numpy.random.shuffle(a)
        return a

    def createImage(self, array):
        im = Image.new("RGB", (1920, 1080), "black")
        pixels = im.load()
        for index, value in enumerate(array):
            for i in range(int(value)):
                pixels[index, 1079-i]= (255, 255, 255)

        font = ImageFont.truetype("arial",size=25)

        text = f"{self.currentSortingAlgorithm} | Comparisons: {self.comparisonCount} | Swaps: {self.swapCount}"
        textSize = font.getsize(text)
        button_size = (textSize[0]+20, textSize[1]+20)
        Box = Image.new("RGB", button_size, "blue")
        boxDraw = ImageDraw.Draw(Box)

        boxDraw.text((10,10),text,font=font)
        im.paste(Box, (0, 0))
        im.save(f"{os.getcwd()}/frames/frame-{self.swapCount}.png")
        print(f"Created image [{self.swapCount}]")

    def bubbleSortMain(self, array):
        length = len(array)

        for i in range(length):
            for j in range(0, length-i-1):
                self.comparisonCount += 1
                if array[j] > array[j+1]:
                    self.swap(array, j, j+1)

    def swap(self, array, index1, index2):
        array[index1], array[index2] = array[index2], array[index1]
        self.swapCount += 1

        if self.swapCount%100==0:
            self.createImage(array)

        return array

    def __call__(self, sort):
        self.currentSortingAlgorithm = sort
        myArray = self.getShuffledArray()
        self.createImage(myArray)
        startTime = time.time()
        self.bubbleSortMain(myArray)

        print("time: ", time.time()- startTime)
        self.createImage(myArray)

if __name__ == "__main__":
    program = Main()

    program("Bubble Sort")


import gym
from gym import wrappers
import numpy as np
from rlgraph.agents import Agent
from rlgraph.environments import OpenAIGymEnv
from rlgraph.execution import SingleThreadedWorker
import json
import os

### NOTE ###
#if you want to use PPO that never selects the same hint twice sequentially, use the ppo_agentSmartPrimer.py
#file in gym_SmartPrimer/agents/. Copy that file and place it into the rlraph/agents directory, under
# the name 'ppo_agent' (replace the old one)

np.random.seed(2)

#configure the agent settings in this file
agent_config_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + '/agents/ppoSmartPrimer_config.json'

with open(agent_config_path, 'rt') as fp:
	agent_config = json.load(fp)

#create the environment
env = OpenAIGymEnv.from_spec({
        "type": "openai",
        "gym_env": 'gym_SmartPrimer:SmartPrimer-realistic-v2'
    })

#retreive the agent from RLgraph
agent = Agent.from_spec(
        agent_config,
        state_space=env.state_space,
        action_space=env.action_space
    )

#define number of children to simulate
episode_count = 1000

episode_returns = []
def episode_finished_callback(episode_return, duration, timesteps, *args, **kwargs):
	episode_returns.append(episode_return)
	if len(episode_returns) % 100 == 0:
		print("Episode {} finished: reward={:.2f}, average reward={:.2f}.".format(
			len(episode_returns), episode_return, np.mean(episode_returns[-100:])
		))

# create the worker
worker = SingleThreadedWorker(env_spec=lambda: env, agent=agent, render=False, worker_executes_preprocessing=False,
                                  episode_finish_callback=episode_finished_callback)

# Use exploration is true for training, false for evaluation.
worker.execute_episodes(episode_count, use_exploration=True)

#make the plots
env.gym_env.render()


from ..base import Konvalidator, KonversionError, ValidationError, And, Or, If, IfElse, Default, strings, types

import ipaddress

class IsAlpha(And):
	''' Accepts only strings with alphabetical characters, spaces, underscores or dashes '''

	def __init__(self):
		super(IsAlpha, self).__init__(
			(
				types.ToType(unicode),
				strings.IsRegexMatch(r'^[A-Z \-_]+$')
			),
			'The specified value "{value}" is not a purely alphabetical string.'
		)

class IsAlphaNumeric(And):
	''' Accepts only strings with alphabetical characters, spaces and dashes '''

	def __init__(self):
		super(IsAlphaNumeric, self).__init__(
			(
				types.ToType(unicode),
				strings.IsRegexMatch(r'^[A-z \-_0-9]+$')
			),
			'The specified value "{value}" is not a purely alphanumeric string.'
		)

class IsEmailAddress(And):
	def __init__(self):
		super(IsEmailAddress, self).__init__(
			(
				types.ToType(unicode),
				strings.IsRegexMatch(r'^[a-z0-9\._\+%-]+@[a-z0-9\.-]+(\.[A-Z]{2,4})+$')
			),
			'The specified value "{value}" is not a valid email address'
		)

class IsName(And):
	'''
	Checks to make sure the string is a name. In otherwords,
	no numbers, symbols, or wierd punctuation.
	'''

	def __init__(self):
		super(IsName, self).__init__(
			(
				types.ToType(unicode),
				strings.IsRegexMatch(r'^[a-z \.]+$')
			),
			'The specified value "{value}" is not a valid name.'
		)

class IsIpv4(And):
	'''
	See if string is a valid IP address
	'''

	def __init__(self):
		super(IsIpv4, self).__init__(
			(
				types.ToType(unicode),
				strings.IsRegexMatch(r'^([0-9]{1,3}\.){3}[0-9]{1,3}$'),
				types.ToType(ipaddress.IPv4Address)
			),
			'The specified value "{value}" is not a valid IPV4 address.'
		)


import numpy as np
from datetime import datetime
import copy

import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader

from waku.sentiment_analysis.model import LSTMLearner
from waku.sentiment_analysis.dataloader import SST
from waku.sentiment_analysis.helpers import reduce_preprocess_embedding, pad_collate

def trainModel(embeddings_dict, embeddings_weights, trainData, valData, epochs=25, learning_rate=0.001,
               batch_size=512, hidden_size=300, rnn_layers=1, mlp_layer_widths=100, checkpoint_filepath='checkpoint.pth.tar'):
    """
    Train LSTMLearner model.

    Parameters:
    -----------
    embeddings_dict : `dict`
        Dict mapping words to int, which indexes row of weight matrix
    embeddings_weights : `numpy.ndarray``
        (len(vocab), 300) Matrix of word embeddings
    trainData : `torch.Dataloader`
        Batched training data
    valData : `torch.Dataloader`
        Batched validation data
    epochs : `int`
        Number of epochs for training.
    learning_rate : `float`
        The learning rate for backpropogation.
    batch_size : `int`
        Number of word pairs in each batch.
    hidden_size : `int`
        Number of features in hidden state of LSTM
    rnn_layers : `int`
        Number of recurrent layers
    mlp_layer_widths : `int`
        Size of fully connected layer
    checkpoint_filepath : `str`
        The path to save the checkpoints.

    Returns:
    --------
    model : `torch.Module`
        Trained LSTMLearner model
    losses : `list` of `float`
        The total training loss in each epoch.
    valLosses : `list` of `float`
        The total validation loss in each epoch.
    accuracy : `list` of `float`
        The training accuracy in each epoch.
    valAccuracy : `list` of `float`
        The validation accuracy in each epoch.
    """
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    #create the data
    dataLoad = DataLoader(trainData, batch_size=batch_size, shuffle=True,
                          collate_fn=pad_collate)
    valDataLoad = DataLoader(valData, batch_size=len(valData),
                          collate_fn=pad_collate)

    # lists to store progress of the model at each epoch
    epoch = 0
    losses, valLosses, accuracy, valAccuracy = [], [], [], []

    #copy weights & dict so original weight matrix isn't corrupted by a training run
    weightsCopy = embeddings_weights.clone().detach()
    dictCopy = embeddings_dict
    # create an instance of the model
    model = LSTMLearner(dictCopy, weightsCopy, hidden_size, rnn_layers, mlp_layer_widths)
    # move the model to the gpu
    model = model.to(device)

    # The question specifies using the cross entropy loss function
    loss_function = nn.CrossEntropyLoss()
    # AdamW was chosen as the optimiser as it seemed to work best
    # with the short amount of exploration done
    optimizer = optim.AdamW(filter(lambda p: p.requires_grad,
                                   model.parameters()), lr=learning_rate)

    for ep in range(epochs):
        total_loss = 0
        correct = 0
        # set model to train mode so dropout and batch normalisation layers work as expected
        model.train()

        for _, ((text_pad, phrase_lengths), labels) in enumerate(dataLoad):
            # move the data to the gpu
            text_pad, phrase_lengths, labels = text_pad.to(device), phrase_lengths.to(device), labels.to(device)
            model.zero_grad()
            out = model(text_pad, phrase_lengths)
            loss = loss_function(out, labels)
            loss.backward()
            optimizer.step()
            total_loss += loss.item()*len(labels)
            # make predictions using the argmax of the output layer
            _, pred = out.max(1)
            correct += (pred == labels).sum().item()

        #after each epoch, collect statistics
        losses.append(total_loss/len(trainData))
        accuracy.append(100*correct/len(trainData))
        # statistics about the validation set
        with torch.no_grad():
            # set model to evaluation mode so dropout is no-longer used and all
            # nodes of the model help in making the prediction
            model.eval()
            (text_pad, phrase_lengths), labels = next(iter(valDataLoad))
            text_pad, phrase_lengths, labels = text_pad.to(device), phrase_lengths.to(device), labels.to(device)
            out = model(text_pad, phrase_lengths)
            valLosses.append(loss_function(out, labels).item() / len(valData))
            _, pred = out.max(1)
            valAccuracy.append(100*(pred == labels).sum().item()/len(valData))

        #if validation improved, save new best model
        if valAccuracy[-1] == max(valAccuracy):
            best_model = deepcopy(model)
        epoch += 1

    #clean up
    model = model.to(torch.device("cpu"))
    del text_pad, phrase_lengths, labels, out, _, pred, weightsCopy

    return best_model, losses, valLosses, accuracy, valAccuracy

def plot_model(accuracy, valAccuracy, losses, valLosses):
    """
    Creates plots for accuracies, losses vs epoch.

    Parameters:
    -----------
    losses : `list` of `float`
        The total training loss in each epoch.
    valLosses : `list` of `float`
        The total validation loss in each epoch.
    accuracy : `list` of `float`
        The training accuracy in each epoch.
    valAccuracy : `list` of `float`
        The validation accuracy in each epoch.
    """
    # epoch on which the best validation set accuracy occured
    bestEpoch = np.argmax(valAccuracy)

    # a plot of the loss as a function of epoch, with the epoch at which early
    # stopping is performed marked with a red line
    plt.figure(1)
    plt.plot(losses, label="training set")
    plt.plot(valLosses, label="Validation set")
    plt.axvline(x=bestEpoch, color="r", label="Early stopping epoch")
    plt.ylabel("Loss")
    plt.xlabel("Epoch")
    plt.legend()
    plt.figure(2)
    plt.plot(accuracy, label="training set")
    plt.plot(valAccuracy, label="Validation set")
    plt.axvline(x=bestEpoch, color="r", label="Early stopping epoch")
    plt.ylabel("Accuracy")
    plt.xlabel("Epoch")
    plt.legend()

def test_model(model, TestData, accuracy, valAccuracy, print_accuracies=False, checkpoint_filepath="checkpoint.pth.tar"):
    """
    Test LSTMLearner model on unseen test data.

    Parameters:
    -----------
    model : `torch.Module`
        Trained LSTMLearner model
    TestData : `torch.Dataloader`
        Batched test data
    accuracy : `list` of `float`
        The training accuracy in each epoch.
    valAccuracy : `list` of `float`
        The validation accuracy in each epoch.
    print_accuracies : `bool`
        Print out dict of training, validation, test accuracies
    checkpoint_filepath : `str`
        The path to load model parameters.

    Returns:
    --------
    bestAccuracy : `dict`, {dataset, accuracy}
        Dictionary of training, validation, test accuracies
    """
    with torch.no_grad():
        # load test data
        testDataLoad = DataLoader(TestData, batch_size=len(TestData),
                            collate_fn=pad_collate)
        ((text_pad, phrase_lengths), labels) = next(iter(testDataLoad))

        # load trained model
        bestModel = copy.deepcopy(model)
        bestModel.load_state_dict(model.state_dict())

        # set model to evaluation mode so dropout works as intended
        bestModel.eval()
        out = bestModel(text_pad, phrase_lengths)
        _, pred = out.max(1)

    # epoch on which the best validation set accuracy occured
    bestEpoch = np.argmax(valAccuracy)

    # Training and validation set accuracy at that epoch
    bestAccuracy = {"training set":round(accuracy[bestEpoch], 2),
                    "validation set":round(valAccuracy[bestEpoch], 2)}

    bestAccuracy["test set"] = round(100*(pred == labels).sum().item()/len(TestData), 2)

    if print_accuracies:
        for k, v in bestAccuracy.items():
            print(k, ": ", v)

    return bestAccuracy

class Extrinsic_Sentiment_Analysis:
    """
    Experiment for sentiment analysis on Stanford Sentinent Treebank
    """
    def __init__(self, SST, input_embedding_dict, input_embedding_weights):
        """
        Arguments:
        ----------
        SST : `Class`
            Data for experiment
        input_embedding_dict : `dict`
            Dict mapping words to int, which indexes row of weight matrix
        input_embedding_weights : `numpy.ndarray`
            (len(vocab), 300) Matrix of word embeddings
        """
        self.X_train = SST.X_train
        self.X_val = SST.X_val
        self.X_test = SST.X_test
        self.input_embedding_dict = input_embedding_dict
        self.input_embedding_weights = input_embedding_weights

        # Reduce embedding to vocab in experiment
        self.embedding_dict, self.embedding_weights = reduce_preprocess_embedding(self.X_train, self.X_val, self.X_test, input_embedding_dict, input_embedding_weights)

        # Process sentences of words into indices which map to embeddings
        SST.embed(self.embedding_dict)
        self.TrainData = SST.TrainData
        self.ValData = SST.ValData
        self.TestData = SST.TestData

    def train(self,epochs=100, learning_rate=0.001, batch_size=512, hidden_size=300, rnn_layers=2, mlp_layer_widths=100, checkpoint_filepath="checkpoint.pth.tar"):
        """
        Train LSTMLearner model.

        Parameters:
        -----------
        epochs : `int`
            Number of epochs for training.
        learning_rate : `float`
            The learning rate for backpropogation.
        batch_size : `int`
            Number of word pairs in each batch.
        hidden_size : `int`
            Number of features in hidden state of LSTM
        rnn_layers : `int`
            Number of recurrent layers
        mlp_layer_widths : `int`
            Size of fully connected layer
        checkpoint_filepath : `str`
            The path to save the checkpoints.
        """
        self.hidden_size = hidden_size
        self.rnn_layers = rnn_layers
        self.mlp_layer_widths = mlp_layer_widths

        self.model, self.losses, self.valLosses, self.accuracy, self.valAccuracy = trainModel(self.embedding_dict, self.embedding_weights, self.TrainData, self.ValData,
                                                                                epochs=epochs, learning_rate=learning_rate, batch_size=batch_size, rnn_layers=rnn_layers,
                                                                                mlp_layer_widths=mlp_layer_widths, checkpoint_filepath=checkpoint_filepath)

    def test(self, print_accuracies=False, save_test_acc=False, file_path=None, checkpoint_filepath="checkpoint.pth.tar"):
        """
        Test LSTMLearner model on unseen test data.

        Parameters:
        -----------
        model : `torch.Module`
            Trained LSTMLearner model
        TestData : `torch.Dataloader`
            Batched test data
        accuracy : `list` of `float`
            The training accuracy in each epoch.
        valAccuracy : `list` of `float`
            The validation accuracy in each epoch.
        print_accuracies : `bool`
            Print out dict of training, validation, test accuracies
        save_test_acc : `bool`
            Save results
        file_path : `str`
            Path to save results of experiment
        checkpoint_filepath : `str`
            The path to load model parameters.
        """
        try:
            self.bestAccuracy = test_model(self.model, self.TestData, self.accuracy, self.valAccuracy, print_accuracies=False, checkpoint_filepath=checkpoint_filepath)
        except:
            raise ValueError('Model not trained')

        if save_test_acc:
            with open('{}/{}.txt'.format(file_path,'sentiment_analysis_' + str(datetime.now())), 'w') as out:
                out.write("Test accuracy on SST: {}".format(self.bestAccuracy["test set"]))

        if print_accuracies:
            print("Test accuracy: {}".format(self.bestAccuracy["test set"]))

    def plot(self):
        """
        Plots accurracies and losses
        """
        try:
            plot_model(self.accuracy, self.valAccuracy, self.losses, self.valLosses)
        except:
            raise ValueError('Model not trained')

def evaluate(embedding_weights, data_filepath, embedding_dict, checkpoint_filepath):
    """
    Run downstream sentiment analysis experiment on a set of embeddings

    Parameters:
    -----------
    embeddings_weights : `numpy.ndarray`
        (len(vocab), 300) Matrix of word embeddings
    data_filepath : `str`
        Path for raw Stanford Sentiment Treeback data
    embedding_dict : `dict`
        Dict mapping words to int, which indexes row of weight matrix
    checkpoint_filepath : `str`
        The path to load model parameters.

    Returns:
    --------
    Experiment.bestAccuracy["test set"] : `float`
        Accuracy on test set
    """
    # Instantiate a *SST* to load Stanford Sentiment Treebank train/test/val data
    SST_instance = SST(data_filepath)

    # Load an instance of the *Extrinsic_Sentiment_Analysis* class with a given dictionary and
    # weights at which point we reduce the vocabulary into words present in SST
    Experiment = Extrinsic_Sentiment_Analysis(SST_instance, embedding_dict, embedding_weights)

    # Train a specified LSTM model for a given number of epochs using the *train* function
    Experiment.train(epochs=100, learning_rate=0.001, batch_size=512, hidden_size=300, rnn_layers=2, mlp_layer_widths=100, checkpoint_filepath=checkpoint_filepath)

    # Calculate accuracy on the test set and save
    Experiment.test(print_accuracies=False, save_test_acc=False, file_path=None, checkpoint_filepath=checkpoint_filepath)

    # reset data class
    SST_instance.reset()

    return Experiment.bestAccuracy["test set"]


try:
    import ConfigParser as configparser
except ImportError:
    import configparser
import os
import pytest

def pytest_addoption(parser):
    parser.addoption('--config', action='store', default='3.0-nginx')
    parser.addoption('--output', action='store', default='output.csv')

def pytest_sessionfinish(session, exitstatus):
    # print(session.config.option.__dict__)
    summary = f'''

    [+] Summary result:

    Test: {session.config.option.output}
    Total: {session.testscollected}
    Number Test Pass: {session.testscollected - session.testsfailed}
    Number Test Failed: {session.testsfailed}
    '''

    print(summary)

@pytest.fixture(scope='session')
def config(request):
    cp = configparser.RawConfigParser()
    cp.read(os.path.join(os.path.dirname(__file__), 'config.ini'))
    return dict(cp.items(request.config.getoption('--config')))


"""CLI to convert a single-image checkpoint to a tracking checkpoint."""

import argparse
import logging
import os

import torch

import openpifpaf

LOG = logging.getLogger(__name__)

def cli():
    parser = argparse.ArgumentParser(description=__doc__)

    openpifpaf.plugin.register()
    openpifpaf.logger.cli(parser)
    openpifpaf.network.Factory.cli(parser)

    parser.add_argument('-o', '--output', default=None)
    args = parser.parse_args()

    openpifpaf.logger.configure(args, LOG)
    openpifpaf.network.Factory.configure(args)

    assert args.checkpoint, 'have to specify a checkpoint as input'
    if args.output is None:
        basename = 't' + os.path.basename(args.checkpoint)
        if not basename.endswith('.pkl'):
            basename += '-{}.pkl'.format(openpifpaf.__version__)
        args.output = os.path.join('outputs', basename)

    return args

def main():
    args = cli()
    model, _ = openpifpaf.network.Factory().factory()
    model.base_net = openpifpaf.network.TrackingBase(model.base_net)
    # model = openpifpaf.network.batchrenorm.BatchRenorm2d.convert_to(model)

    LOG.info('saving %s', args.output)
    torch.save({
        'model': model,
        'epoch': 0,
        'meta': {
            'image-source': args.checkpoint,
        },
    }, args.output)

if __name__ == '__main__':
    main()


__copyright__ = 'Copyright 2023, The RADICAL-Cybertools Team'
__license__   = 'MIT'

from typing import Optional, Dict, List

import radical.utils as ru

from .. import Pipeline

# ------------------------------------------------------------------------------
#
def get_provenance_graph(pipelines: List[Pipeline],
                         output_file: Optional[str] = None) -> Dict:
    """
    Using UIDs of all entities to build a workflow provenance graph.
    """

    graph = {}

    pipelines = ru.as_list(pipelines)

    for pipeline in pipelines:
        graph[pipeline.uid] = {}

        for stage in pipelines.stages:
            graph[pipeline.uid][stage.uid] = {}

            for task in stage.tasks:
                g_task = graph[pipeline.uid][stage.uid].setdefault(task.uid, {})
                if task.annotations:
                    g_task.update(task.annotations.as_dict())

    if output_file:
        if not output_file.endswith('.json'):
            output_file += '.json'
        ru.write_json(graph, output_file)

    return graph

# ------------------------------------------------------------------------------
#
def extract_provenance_graph(session_json: str,
                             output_file: Optional[str] = None) -> Dict:
    """
    Using session JSON file to build a workflow provenance graph.
    """

    session_entities = ru.read_json(session_json)

    if not session_entities.get('task'):
        raise ValueError('No task entities in provided session')

    graph = {}

    for task in session_entities['task']:
        task_uid, _, stage_uid, _, pipeline_uid, _ = task['name'].split(',')
        graph.\
            setdefault(pipeline_uid, {}).\
            setdefault(stage_uid, {}).\
            setdefault(task_uid,
                       task['description']['metadata'].get('data') or {})

    for pipeline_uid in graph:
        for stage_uid in graph[pipeline_uid]:
            graph[pipeline_uid][stage_uid].sort()

    if output_file:
        if not output_file.endswith('.json'):
            output_file += '.json'
        ru.write_json(graph, output_file)

    return graph

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


from threading import Thread
import socket
import json
import time

class GameSocket(Thread):
    def __init__(self, host, port, accept_num=2):
        super().__init__()

        self.client_list = []
        self.flag = True
        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_socket.bind((host, port))
        self.server_socket.listen(accept_num)

    def terminate(self):
        self.flag = False

    def serve(self):
        self.start()

    def run(self):
        while self.flag:
            connection, address = self.server_socket.accept()
            self.client_list.append((address, connection))

            print("{} connected".format(address))

            self.new_connection(connection=connection,
                                address=address)
            if(len(self.client_list)==2):
                break
    def new_connection(self, connection, address):
        Thread(target=self.receive_message_from_client,
               kwargs={
                   "connection": connection,
                   "address": address}, daemon=True).start()

    def receive_message_from_client(self, connection, address):
        keep_going = True
        while keep_going:
            try:
                message = connection.recv(1024).strip().decode()
            except:
                print(message)
                print('[GameSocket EXCEPTION]')
                keep_going = False
            else:
                if not message:
                    break

                print('[TEST] {}'.format(message))
                message = json.loads(message)

                print('  server received: {} form {}'.format(message, address))

                if message['command'] == "close":
                    break
                else:
                    self.client_list[0][1].send(json.dumps(message).encode())
                    self.client_list[1][1].send(json.dumps(message).encode())

        connection.close()
        print("close connection")

    # def find_other(self, address):
    #     if(address[1] == self.client_list[0][0][1]):
    #         return 1
    #     return 0


#!/usr/bin/python3
"""
a class Node that defines a node of a singly linked list
"""

class Node:
    """A class creates a single Node in a Linked List.
    """
    def __init__(self, data, next_node=None):
        self.data = data
        self.next_node = next_node

    @property
    def data(self):
        return self.__data

    @data.setter
    def data(self, value):
        if type(value) != int:
            raise TypeError("data must be an integer")
        self.__data = value

    @property
    def next_node(self):
        return self.__next_node

    @next_node.setter
    def next_node(self, value):
        if not (value is None or type(value) is Node):
            raise TypeError("next_node must be a Node object")
        self.__next_node = value

class SinglyLinkedList:
    """A class that creates a Singly Linked List.
    """
    def __init__(self):
        self.__head = None

    def __repr__(self):
        temp = self.__head
        total = ""
        while temp:
            total += "{:d}".format(temp.data)
            temp = temp.next_node
            if temp:
                total += "\n"
        return total

    def sorted_insert(self, value):
        if self.__head is None:
            self.__head = Node(value)
        else:
            curr = self.__head
            prev = None
            while curr and value > curr.data:
                prev = curr
                curr = curr.next_node
            if curr is None:
                prev.next_node = Node(value)
            elif curr is self.__head and prev is None:
                self.__head = Node(value, curr)
            else:
                newNode = Node(value, curr)
                prev.next_node = newNode


import json
import re
from dataclasses import dataclass, field, fields, replace
from pathlib import Path
from typing import Dict, FrozenSet, List, Optional, Set, Tuple, Union

import tornado
from jupyterlab_server.translation_utils import translator
from traitlets.config import Configurable, LoggingConfigurable

from jupyterlab.commands import (
    _AppHandler,
    _ensure_options,
    disable_extension,
    enable_extension,
    get_app_info,
)

PYTHON_TO_SEMVER = {"a": "-alpha.", "b": "-beta.", "rc": "-rc."}

def _ensure_compat_errors(info, app_options):
    """Ensure that the app info has compat_errors field"""
    handler = _AppHandler(app_options)
    info["compat_errors"] = handler._get_extension_compat()

_message_map = {
    "install": re.compile(r"(?P<name>.*) needs to be included in build"),
    "uninstall": re.compile(r"(?P<name>.*) needs to be removed from build"),
    "update": re.compile(r"(?P<name>.*) changed from (?P<oldver>.*) to (?P<newver>.*)"),
}

def _build_check_info(app_options):
    """Get info about packages scheduled for (un)install/update"""
    handler = _AppHandler(app_options)
    messages = handler.build_check(fast=True)
    # Decode the messages into a dict:
    status = {"install": [], "uninstall": [], "update": []}
    for msg in messages:
        for key, pattern in _message_map.items():
            match = pattern.match(msg)
            if match:
                status[key].append(match.group("name"))
    return status

@dataclass(frozen=True)
class ExtensionPackage:
    """Extension package entry.

    Attributes:
        name: Package name
        description: Package description
        homepage_url: Package home page
        pkg_type: Type of package - ["prebuilt", "source"]
        allowed: [optional] Whether this extension is allowed or not - default True
        approved: [optional] Whether the package is approved by your administrators - default False
        companion: [optional] Type of companion for the frontend extension - [None, "kernel", "server"]; default None
        core: [optional] Whether the package is a core package or not - default False
        enabled: [optional] Whether the package is enabled or not - default False
        install: [optional] Extension package installation instructions - default None
        installed: [optional] Whether the extension is currently installed - default None
        installed_version: [optional] Installed version - default ""
        latest_version: [optional] Latest available version - default ""
        status: [optional] Package status - ["ok", "warning", "error"]; default "ok"
        author: [optional] Package author - default None
        license: [optional] Package license - default None
        bug_tracker_url: [optional] Package bug tracker URL - default None
        documentation_url: [optional] Package documentation URL - default None
        package_manager_url: Package home page in the package manager - default None
        repository_url: [optional] Package code repository URL - default None
    """

    name: str
    description: str
    homepage_url: str
    pkg_type: str
    allowed: bool = True
    approved: bool = False
    companion: Optional[str] = None
    core: bool = False
    enabled: bool = False
    install: Optional[dict] = None
    installed: Optional[bool] = None
    installed_version: str = ""
    latest_version: str = ""
    status: str = "ok"
    author: Optional[str] = None
    license: Optional[str] = None  # noqa
    bug_tracker_url: Optional[str] = None
    documentation_url: Optional[str] = None
    package_manager_url: Optional[str] = None
    repository_url: Optional[str] = None

@dataclass(frozen=True)
class ActionResult:
    """Action result

    Attributes:
        status: Action status - ["ok", "warning", "error"]
        message: Action status explanation
        needs_restart: Required action follow-up - Valid follow-up are "frontend", "kernel" and "server"
    """

    # Note: no simple way to use Enum in dataclass - https://stackoverflow.com/questions/72859557/typing-dataclass-that-can-only-take-enum-values
    #       keeping str for simplicity
    status: str
    message: Optional[str] = None
    needs_restart: List[str] = field(default_factory=list)

@dataclass(frozen=True)
class PluginManagerOptions:
    """Plugin manager options.

    Attributes:
        lock_all: Whether to lock (prevent enabling/disabling) all plugins.
        lock_rules: A list of plugins or extensions that cannot be toggled.
            If extension name is provided, all its plugins will be disabled.
            The plugin names need to follow colon-separated format of `extension:plugin`.
    """

    lock_rules: FrozenSet[str] = field(default_factory=frozenset)
    lock_all: bool = False

@dataclass(frozen=True)
class ExtensionManagerOptions(PluginManagerOptions):
    """Extension manager options.

    Attributes:
        allowed_extensions_uris: A list of comma-separated URIs to get the allowed extensions list
        blocked_extensions_uris: A list of comma-separated URIs to get the blocked extensions list
        listings_refresh_seconds: The interval delay in seconds to refresh the lists
        listings_tornado_options: The optional kwargs to use for the listings HTTP requests as described on https://www.tornadoweb.org/en/stable/httpclient.html#tornado.httpclient.HTTPRequest
    """

    allowed_extensions_uris: Set[str] = field(default_factory=set)
    blocked_extensions_uris: Set[str] = field(default_factory=set)
    listings_refresh_seconds: int = 60 * 60
    listings_tornado_options: dict = field(default_factory=dict)

@dataclass(frozen=True)
class ExtensionManagerMetadata:
    """Extension manager metadata.

    Attributes:
        name: Extension manager name to be displayed
        can_install: Whether the extension manager can un-/install packages (default False)
        install_path: Installation path for the extensions (default None); e.g. environment path
    """

    name: str
    can_install: bool = False
    install_path: Optional[str] = None

@dataclass
class ExtensionsCache:
    """Extensions cache

    Attributes:
        cache: Extension list per page
        last_page: Last available page result
    """

    cache: Dict[int, Optional[Dict[str, ExtensionPackage]]] = field(default_factory=dict)
    last_page: int = 1

class PluginManager(LoggingConfigurable):
    """Plugin manager enables or disables plugins unless locked.

    It can also disable/enable all plugins in an extension.

    Args:
        app_options: Application options
        ext_options: Plugin manager (subset of extension manager) options
        parent: Configurable parent

    Attributes:
        app_options: Application options
        options: Plugin manager options
    """

    def __init__(
        self,
        app_options: Optional[dict] = None,
        ext_options: Optional[dict] = None,
        parent: Optional[Configurable] = None,
    ) -> None:
        super().__init__(parent=parent)
        self.app_options = _ensure_options(app_options)
        plugin_options_field = {f.name for f in fields(PluginManagerOptions)}
        plugin_options = {
            option: value
            for option, value in (ext_options or {}).items()
            if option in plugin_options_field
        }
        self.options = PluginManagerOptions(**plugin_options)

    async def plugin_locks(self) -> dict:
        """Get information about locks on plugin enabling/disabling"""
        return {
            "lockRules": list(self.options.lock_rules),
            "allLocked": self.options.lock_all,
        }

    def _find_locked(self, plugins_or_extensions: List[str]) -> FrozenSet[str]:
        """Find a subset of plugins (or extensions) which are locked"""
        if self.options.lock_all:
            return set(plugins_or_extensions)
        locked_subset = set()
        extensions_with_locked_plugins = {
            plugin.split(':')[0] for plugin in self.options.lock_rules
        }
        for plugin in plugins_or_extensions:
            if ':' in plugin:
                # check directly if this is a plugin identifier (has colon)
                if plugin in self.options.lock_rules:
                    locked_subset.add(plugin)
            elif plugin in extensions_with_locked_plugins:
                # this is an extension - we need to check for >any< plugin
                # belonging to said extension
                locked_subset.add(plugin)
        return locked_subset

    async def disable(self, plugins: Union[str, List[str]]) -> ActionResult:
        """Disable a set of plugins (or an extension).

        Args:
            plugins: The list of plugins to disable
        Returns:
            The action result
        """
        plugins = plugins if isinstance(plugins, list) else [plugins]
        locked = self._find_locked(plugins)
        trans = translator.load("jupyterlab")
        if locked:
            return ActionResult(
                status="error",
                message=trans.gettext(
                    "The following plugins cannot be disabled as they are locked: "
                )
                + ", ".join(locked),
            )
        try:
            for plugin in plugins:
                disable_extension(plugin, app_options=self.app_options)
            return ActionResult(status="ok", needs_restart=["frontend"])
        except Exception as err:
            return ActionResult(status="error", message=repr(err))

    async def enable(self, plugins: Union[str, List[str]]) -> ActionResult:
        """Enable a set of plugins (or an extension).

        Args:
            plugins: The list of plugins to enable
        Returns:
            The action result
        """
        plugins = plugins if isinstance(plugins, list) else [plugins]
        locked = self._find_locked(plugins)
        trans = translator.load("jupyterlab")
        if locked:
            return ActionResult(
                status="error",
                message=trans.gettext(
                    "The following plugins cannot be enabled as they are locked: "
                )
                + ", ".join(locked),
            )
        try:
            for plugin in plugins:
                enable_extension(plugin, app_options=self.app_options)
            return ActionResult(status="ok", needs_restart=["frontend"])
        except Exception as err:
            return ActionResult(status="error", message=repr(err))

class ExtensionManager(PluginManager):
    """Base abstract extension manager.

    Note:
        Any concrete implementation will need to implement the five
        following abstract methods:
        - :ref:`metadata`
        - :ref:`get_latest_version`
        - :ref:`list_packages`
        - :ref:`install`
        - :ref:`uninstall`

        It could be interesting to override the :ref:`get_normalized_name`
        method too.

    Args:
        app_options: Application options
        ext_options: Extension manager options
        parent: Configurable parent

    Attributes:
        log: Logger
        app_dir: Application directory
        core_config: Core configuration
        app_options: Application options
        options: Extension manager options
    """

    def __init__(
        self,
        app_options: Optional[dict] = None,
        ext_options: Optional[dict] = None,
        parent: Optional[Configurable] = None,
    ) -> None:
        super().__init__(app_options=app_options, ext_options=ext_options, parent=parent)
        self.log = self.app_options.logger
        self.app_dir = Path(self.app_options.app_dir)
        self.core_config = self.app_options.core_config
        self.options = ExtensionManagerOptions(**(ext_options or {}))
        self._extensions_cache: Dict[Optional[str], ExtensionsCache] = {}
        self._listings_cache: Optional[dict] = None
        self._listings_block_mode = True
        self._listing_fetch: Optional[tornado.ioloop.PeriodicCallback] = None

        if len(self.options.allowed_extensions_uris) or len(self.options.blocked_extensions_uris):
            self._listings_block_mode = len(self.options.allowed_extensions_uris) == 0
            if not self._listings_block_mode and len(self.options.blocked_extensions_uris) > 0:
                self.log.warning(
                    "You have define simultaneously blocked and allowed extensions listings. The allowed listing will take precedence."
                )

            self._listing_fetch = tornado.ioloop.PeriodicCallback(
                self._fetch_listings,
                callback_time=self.options.listings_refresh_seconds * 1000,
                jitter=0.1,
            )
            self._listing_fetch.start()

    def __del__(self):
        if self._listing_fetch is not None:
            self._listing_fetch.stop()

    @property
    def metadata(self) -> ExtensionManagerMetadata:
        """Extension manager metadata."""
        raise NotImplementedError()

    async def get_latest_version(self, extension: str) -> Optional[str]:
        """Return the latest available version for a given extension.

        Args:
            pkg: The extension name
        Returns:
            The latest available version
        """
        raise NotImplementedError()

    async def list_packages(
        self, query: str, page: int, per_page: int
    ) -> Tuple[Dict[str, ExtensionPackage], Optional[int]]:
        """List the available extensions.

        Args:
            query: The search extension query
            page: The result page
            per_page: The number of results per page
        Returns:
            The available extensions in a mapping {name: metadata}
            The results last page; None if the manager does not support pagination
        """
        raise NotImplementedError()

    async def install(self, extension: str, version: Optional[str] = None) -> ActionResult:
        """Install the required extension.

        Note:
            If the user must be notified with a message (like asking to restart the
            server), the result should be
            {"status": "warning", "message": "<explanation for the user>"}

        Args:
            extension: The extension name
            version: The version to install; default None (i.e. the latest possible)
        Returns:
            The action result
        """
        raise NotImplementedError()

    async def uninstall(self, extension: str) -> ActionResult:
        """Uninstall the required extension.

        Note:
            If the user must be notified with a message (like asking to restart the
            server), the result should be
            {"status": "warning", "message": "<explanation for the user>"}

        Args:
            extension: The extension name
        Returns:
            The action result
        """
        raise NotImplementedError()

    @staticmethod
    def get_semver_version(version: str) -> str:
        """Convert a Python version to Semver version.

        It:

        - drops ``.devN`` and ``.postN``
        - converts ``aN``, ``bN`` and ``rcN`` to ``-alpha.N``, ``-beta.N``, ``-rc.N`` respectively

        Args:
            version: Version to convert
        Returns
            Semver compatible version
        """
        return re.sub(
            r"(a|b|rc)(\d+)$",
            lambda m: f"{PYTHON_TO_SEMVER[m.group(1)]}{m.group(2)}",
            re.subn(r"\.(dev|post)\d+", "", version)[0],
        )

    def get_normalized_name(self, extension: ExtensionPackage) -> str:
        """Normalize extension name.

        Extension have multiple parts, npm package, Python package,...
        Sub-classes may override this method to ensure the name of
        an extension from the service provider and the local installed
        listing is matching.

        Args:
            extension: The extension metadata
        Returns:
            The normalized name
        """
        return extension.name

    async def list_extensions(
        self, query: Optional[str] = None, page: int = 1, per_page: int = 30
    ) -> Tuple[List[ExtensionPackage], Optional[int]]:
        """List extensions for a given ``query`` search term.

        This will return the extensions installed (if ``query`` is None) or
        available if allowed by the listing settings.

        Args:
            query: [optional] Query search term.

        Returns:
            The extensions
            Last page of results
        """
        if query not in self._extensions_cache or page not in self._extensions_cache[query].cache:
            await self.refresh(query, page, per_page)

        # filter using listings settings
        if self._listings_cache is None and self._listing_fetch is not None:
            await self._listing_fetch.callback()

        cache = self._extensions_cache[query].cache[page]
        if cache is None:
            cache = {}
        extensions = list(cache.values())
        if query is not None and self._listings_cache is not None:
            listing = list(self._listings_cache)
            extensions = []
            if self._listings_block_mode:
                for name, ext in cache.items():
                    if name not in listing:
                        extensions.append(replace(ext, allowed=True))
                    elif ext.installed_version:
                        self.log.warning(f"Blocked extension '{name}' is installed.")
                        extensions.append(replace(ext, allowed=False))
            else:
                for name, ext in cache.items():
                    if name in listing:
                        extensions.append(replace(ext, allowed=True))
                    elif ext.installed_version:
                        self.log.warning(f"Not allowed extension '{name}' is installed.")
                        extensions.append(replace(ext, allowed=False))

        return extensions, self._extensions_cache[query].last_page

    async def refresh(self, query: Optional[str], page: int, per_page: int) -> None:
        """Refresh the list of extensions."""
        if query in self._extensions_cache:
            self._extensions_cache[query].cache[page] = None
        await self._update_extensions_list(query, page, per_page)

    async def _fetch_listings(self) -> None:
        """Fetch the listings for the extension manager."""
        rules = []
        client = tornado.httpclient.AsyncHTTPClient()
        if self._listings_block_mode:
            if len(self.options.blocked_extensions_uris):
                self.log.info(
                    f"Fetching blocked extensions from {self.options.blocked_extensions_uris}"
                )
                for blocked_extensions_uri in self.options.blocked_extensions_uris:
                    r = await client.fetch(
                        blocked_extensions_uri,
                        **self.options.listings_tornado_options,
                    )
                    j = json.loads(r.body)
                    rules.extend(j.get("blocked_extensions", []))
        elif len(self.options.allowed_extensions_uris):
            self.log.info(
                f"Fetching allowed extensions from { self.options.allowed_extensions_uris}"
            )
            for allowed_extensions_uri in self.options.allowed_extensions_uris:
                r = await client.fetch(
                    allowed_extensions_uri,
                    **self.options.listings_tornado_options,
                )
                j = json.loads(r.body)
                rules.extend(j.get("allowed_extensions", []))

        self._listings_cache = {r["name"]: r for r in rules}

    async def _get_installed_extensions(
        self, get_latest_version=True
    ) -> Dict[str, ExtensionPackage]:
        """Get the installed extensions.

        Args:
            get_latest_version: Whether to fetch the latest extension version or not.
        Returns:
            The installed extensions as a mapping {name: metadata}
        """
        app_options = self.app_options
        info = get_app_info(app_options=app_options)
        build_check_info = _build_check_info(app_options)
        _ensure_compat_errors(info, app_options)
        extensions = {}

        # TODO: the three for-loops below can be run concurrently
        for name, data in info["federated_extensions"].items():
            status = "ok"
            pkg_info = data
            if info["compat_errors"].get(name, None):
                status = "error"

            normalized_name = self._normalize_name(name)
            pkg = ExtensionPackage(
                name=normalized_name,
                description=pkg_info.get("description", ""),
                homepage_url=data.get("url", ""),
                enabled=(name not in info["disabled"]),
                core=False,
                latest_version=ExtensionManager.get_semver_version(data["version"]),
                installed=True,
                installed_version=ExtensionManager.get_semver_version(data["version"]),
                status=status,
                install=data.get("install", {}),
                pkg_type="prebuilt",
                companion=self._get_companion(data),
                author=data.get("author", {}).get("name", data.get("author")),
                license=data.get("license"),
                bug_tracker_url=data.get("bugs", {}).get("url"),
                repository_url=data.get("repository", {}).get("url", data.get("repository")),
            )

            if get_latest_version:
                pkg = replace(pkg, latest_version=await self.get_latest_version(pkg.name))

            extensions[normalized_name] = pkg

        for name, data in info["extensions"].items():
            if name in info["shadowed_exts"]:
                continue
            status = "ok"

            if info["compat_errors"].get(name, None):
                status = "error"
            else:
                for packages in build_check_info.values():
                    if name in packages:
                        status = "warning"

            normalized_name = self._normalize_name(name)
            pkg = ExtensionPackage(
                name=normalized_name,
                description=data.get("description", ""),
                homepage_url=data["url"],
                enabled=(name not in info["disabled"]),
                core=False,
                latest_version=ExtensionManager.get_semver_version(data["version"]),
                installed=True,
                installed_version=ExtensionManager.get_semver_version(data["version"]),
                status=status,
                pkg_type="source",
                companion=self._get_companion(data),
                author=data.get("author", {}).get("name", data.get("author")),
                license=data.get("license"),
                bug_tracker_url=data.get("bugs", {}).get("url"),
                repository_url=data.get("repository", {}).get("url", data.get("repository")),
            )
            if get_latest_version:
                pkg = replace(pkg, latest_version=await self.get_latest_version(pkg.name))
            extensions[normalized_name] = pkg

        for name in build_check_info["uninstall"]:
            data = self._get_scheduled_uninstall_info(name)
            if data is not None:
                normalized_name = self._normalize_name(name)
                pkg = ExtensionPackage(
                    name=normalized_name,
                    description=data.get("description", ""),
                    homepage_url=data.get("homepage", ""),
                    installed=False,
                    enabled=False,
                    core=False,
                    latest_version=ExtensionManager.get_semver_version(data["version"]),
                    installed_version=ExtensionManager.get_semver_version(data["version"]),
                    status="warning",
                    pkg_type="prebuilt",
                    author=data.get("author", {}).get("name", data.get("author")),
                    license=data.get("license"),
                    bug_tracker_url=data.get("bugs", {}).get("url"),
                    repository_url=data.get("repository", {}).get("url", data.get("repository")),
                )
                extensions[normalized_name] = pkg

        return extensions

    def _get_companion(self, data: dict) -> Optional[str]:
        companion = None
        if "discovery" in data["jupyterlab"]:
            if "server" in data["jupyterlab"]["discovery"]:
                companion = "server"
            elif "kernel" in data["jupyterlab"]["discovery"]:
                companion = "kernel"
        return companion

    def _get_scheduled_uninstall_info(self, name) -> Optional[dict]:
        """Get information about a package that is scheduled for uninstallation"""
        target = self.app_dir / "staging" / "node_modules" / name / "package.json"
        if target.exists():
            with target.open() as fid:
                return json.load(fid)
        else:
            return None

    def _normalize_name(self, name: str) -> str:
        """Normalize extension name; by default does nothing.

        Args:
            name: Extension name
        Returns:
            Normalized name
        """
        return name

    async def _update_extensions_list(
        self, query: Optional[str] = None, page: int = 1, per_page: int = 30
    ) -> None:
        """Update the list of extensions"""
        last_page = None
        if query is not None:
            # Get the available extensions
            extensions, last_page = await self.list_packages(query, page, per_page)
        else:
            # Get the installed extensions
            extensions = await self._get_installed_extensions()

        if query in self._extensions_cache:
            self._extensions_cache[query].cache[page] = extensions
            self._extensions_cache[query].last_page = last_page or 1
        else:
            self._extensions_cache[query] = ExtensionsCache({page: extensions}, last_page or 1)


import PQR_optimizer
import opt_config

PQRs = {} #empty dictionary of PQRs

for system in opt_config.systems.keys():
    try:
        iters = 5
        max_iters = 8
        REDOpt = 0
        redMargin = 5
        tagetRED = 35
        while not ((tagetRED-redMargin) <= REDOpt <= (tagetRED + redMargin)) and (iters < max_iters):

            xOpt, REDOpt, PQROpt = PQR_optimizer.optimize(System=system,
                                                          targetRED=tagetRED,redMargin=redMargin,
                                                          iters=iters)
            print('Iterations = ',iters)  # Debug print
            print(f'for system {system} the calculated RED is {REDOpt}')
            iters += 1
            if iters == max_iters:
                raise Exception("not converging good enough")

        PQRs[system] = round(PQROpt,1)
        print(system,'',round(PQROpt,1))
        print(system,'',round(REDOpt,1))
    except:
        print('Solution not found for ',system)

for system in sorted(PQRs, key=PQRs.get):
     print(f'In system {system}, PQR = {PQRs[system]}[W/(m³/h)]')


#!/usr/bin/env python3
import aws_cdk
import ecs_flask
import github_role

app = aws_cdk.App()

github_action_role = github_role.GitHubActionRole(
    app, 'GitHubActionRole',
    env=aws_cdk.Environment(
        region=app.node.try_get_context('region'),
        account=app.node.try_get_context('account')
    )
)

ecs_flask.EcsFlask(
    app, "EcsFlask",
    github_role_arn=github_action_role.role.role_arn,
    env=aws_cdk.Environment(
        region=app.node.try_get_context('region'),
        account=app.node.try_get_context('account')
    )
)

app.synth()


class Solution:
    def numBusesToDestination(self, routes: List[List[int]], s: int, t: int) -> int:
        if s == t: return 0
        stops = defaultdict(list)
        for i, bus in enumerate(routes):
            for stop in bus:
                stops[stop].append(i)
        graph = [[] for _ in range(len(routes))]
        for i, bus in enumerate(routes):
            for stop in bus:
                graph[i].extend(stops[stop])
            graph[i] = set(graph[i])

        # visited = [0] * len(routes)
        stack = stops[s]
        d = [0] * len(routes)
        for i in stack:
            if i in stops[t]:
                return 1
            d[i] = 1

        while stack:
            bus = stack.pop(0)
            for i in graph[bus]:
                print(i)
                if i != bus and not d[i]:
                    stack.append(i)
                    d[i] = d[bus] + 1
                if i in stops[t]:
                    return d[i]
        return -1


# -*- coding: utf-8 -*-
"""
Created on Wed Apr 15 09:01:46 2015

@author: Nick
"""

# -*- coding: utf-8 -*-
"""
Created on Wed Apr 15 07:41:43 2015

@author: Nick
"""

import csv
from datetime import datetime

fields = ["itemID", "Name", "Description", "Owner", "Price", "Condition",
"DateRegistered"]

with open("tooldesc2.csv") as td_in:
    rdr = csv.DictReader(td_in, fieldnames=fields)
    items = [item for item in rdr]

with open("tooldesc3.csv", "w") as td_out :
    wrt = csv.DictWriter(td_out, fieldnames=fields)
    wrt.writeheader()
    wrt.writerows(items)


import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score, mean_squared_error

df = pd.read_csv('./survey_results_public.csv')
schema = pd.read_csv('./survey_results_schema.csv')

##Categorical Variables
# Question 1
cat_df = df.select_dtypes(include=['object'])
#Question 2
cat_df_dict = {'the number of columns with no missing values': 6,
               'the number of columns with more than half of the column missing': 49,
               'the number of columns with more than 75% of the column missing': 13
              }
#Question 3
sol_3_dict = {'Which column should you create a dummy variable for?': 'col1',
              'When you use the default settings for creating dummy variables, how many are created?': 2,
              'What happens with the nan values?': 'the NaNs are always encoded as 0'
             }
#Question 4
#create needed dataframe
dummy_var_df = pd.DataFrame({'col1': ['a', 'a', 'b', 'b', 'a', np.nan, 'b', np.nan],
                             'col2': [1, np.nan, 3, np.nan, 5, 6, 7, 8]
})
#dummy cols
dummy_cols_df = pd.get_dummies(dummy_var_df['col1'], dummy_na=True)

#Question 5
cat_cols_lst = cat_df.columns
def create_dummy_df(df, cat_cols, dummy_na):
    for col in  cat_cols:
        try:
            # for each cat add dummy var, drop original column
            df = pd.concat([df.drop(col, axis=1), pd.get_dummies(df[col], prefix=col, prefix_sep='_', drop_first=True, dummy_na=dummy_na)], axis=1)
        except:
            continue
    return df
df_new = create_dummy_df(df, cat_cols_lst, dummy_na=False)


from collections import Counter

def part1(areas):
    return len(get_cubes_on(areas))

def part2(areas):
    return get_cubes_optimized(areas)

def get_cubes_on(areas):
    cubes_on = set()
    for status, x, y, z in areas:
        if is_inside(*x) and is_inside(*y) and is_inside(*z):
            for i in range(x[0], x[1] + 1):
                for j in range(y[0], y[1] + 1):
                    for k in range(z[0], z[1] + 1):
                        if status == 'on':
                            cubes_on.add((i, j, k))
                        else:
                            if (i, j, k) in cubes_on:
                                cubes_on.remove((i, j, k))
    return cubes_on

def get_cubes_optimized(areas):
    preprocessed = [(1 if status == 'on' else -1, (min(x[0], x[1]), min(y[0], y[1]), min(z[0], z[1])),
                     (max(x[0], x[1]), max(y[0], y[1]), max(z[0], z[1]))) for status, x, y, z in areas]

    cubes = Counter()
    for status, (x_1, y_1, z_1), (x_2, y_2, z_2) in preprocessed:
        new_cubes = Counter()
        for (new_x_1, new_x_2, new_y_1, new_y_2, new_z_1, new_z_2), n_status in cubes.items():
            dx_1, dx_2 = max(x_1, new_x_1), min(x_2, new_x_2)
            dy_1, dy_2 = max(y_1, new_y_1), min(y_2, new_y_2)
            dz_1, dz_2 = max(z_1, new_z_1), min(z_2, new_z_2)
            if dx_1 <= dx_2 and dy_1 <= dy_2 and dz_1 <= dz_2:
                new_cubes[(dx_1, dx_2, dy_1, dy_2, dz_1, dz_2)] -= n_status
        if status > 0:
            new_cubes[(x_1, x_2, y_1, y_2, z_1, z_2)] = status
        cubes.update(new_cubes)
    count = 0
    for cube, status in cubes.items():
        count += (area(*cube) * status)
    return count

def is_inside(x1, x2):
    return -50 <= x1 <= 50 and -50 <= x2 <= 50

def area(x_1, x_2, y_1, y_2, z_1, z_2):
    return (x_2 - x_1 + 1) * (y_2 - y_1 + 1) * (z_2 - z_1 + 1)


#!/usr/bin/python3
import unittest
import rostest
import sys
import os
import rospy
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir+"/src")
import lebai_driver.param_utils as param_utils

class TestAll(unittest.TestCase):
    def __init__(self, methodName='runTest'):
        unittest.TestCase.__init__(self, methodName)

    def test_get_joint_names_case1(self):
        joint_names = param_utils.get_joint_names("controller_joint_names", "robot_description")
        # rospy.logerr(joint_names)
        self.assertEqual(["joint_1", "joint_2", "joint_3", "joint_4", "joint_5", "joint_6"],joint_names)

    def test_get_joint_names_case2(self):
        joint_names = param_utils.get_joint_names("xxx", "robot_description")
        # rospy.logerr(joint_names)
        self.assertEqual(['robot1_joint_1', 'robot1_joint_2', 'robot1_joint_3', 'robot1_joint_4',
        'robot1_joint_5', 'robot1_joint_6', 'robot2_joint_1', 'robot2_joint_2', 'robot2_joint_3',
        'robot2_joint_4', 'robot2_joint_5', 'robot2_joint_6'],joint_names)

PKG = 'lebai_driver'
NAME = 'param_utils_test'

if __name__ == '__main__':
    rospy.init_node('test_param_utils', anonymous=True)
    rostest.rosrun(PKG, NAME, TestAll, sys.argv)


# 2.12
# Másodpercek átszámítása órákká, percekké és másodpercekké

másodpercek = int(input("Add meg a másodpercek számát és én kiszámolom, hogy \
az hány óra és hány perc. >> "))

órák = másodpercek // 3600
percek = (másodpercek % 3600) // 60
mperc = (másodpercek % 3600) % 60

print(f"{másodpercek} másodperc = {órák} óra, {percek} perc és {mperc} másodperc")

# 2.14.5

C = int(10000)
m = int(12)
r = float(0.08)
t = int(input("Hány évre kössük le a betétet: > "))

FV = float(C * (1 + (r / m)) ** (m * t))
print("A futamidő végén a kivehető összeg: ", FV)

# 2.14.7

v = int(14 + 51) % 24
print(f"Az óra {v} órakor fog megszólalni.")

# 2.14.8

óra = int(input("Hány óra van? > "))
ébresztő = int(input("Hány órával később csörögjön? > "))
csörög = int((óra + ébresztő) % 24)
print(f"Az óra {csörög} órakor fog ébreszteni.")

# 2.14.8.1 (profibb megoldás)

x = int(input("Hány óra van most? >: "))
t = int(input("Hány órával később szólaljon meg az óra?>: "))
y = int((x + t) % 24)
if y == 12:
    print('Az óra délben csörög')
elif y == 0:
    print('Az óra éjfélkor csörög')
elif y < 12:
    print(f'Az óra délelőtt {y} órakor csörög')
elif y > 12:
    z = y - 12
    print(f'Az óra délután {z} órakor csörög')


#THIS PROGRAM DISPLAYS A TABLE CONTAINING EMPLOYEE NAMES AND AVG CUSTOMER SATISFACTION SURVEY SCORES FOR EACH EMPLOYEE
 #BASED ON RECORDS FROM A .CSV FILE. IT ALSO DISPLAYS THE NAME OF THE INDIVIDUAL WITH THE HIGHEST AVG. LASTLY, THE PROGRAM
  #ALLOWS THE USER TO CONDUCT FILTERED SEARCHES WITHIN THE DATA RECORDS BY ENTERING AN EMPLOYEE NAME, AND THE PROGRAM DISPLAYS THE
   #EMPLOYEE'S OVERALL PERFORMANCE RATING (BASED ON AVG SURVEY SCORES) AND LISTS THEIR RESPECTIVE SCORE FOR EACH SURVEY ON RECORD.

def calc_avg():
    """ Accesses customer satisfaction survey data from table and calculates performance avgs,
    displays records as a table, and identifies the employee with highest satisfaction survey average."""

    survey_scores = open('survey_scores.csv', 'r')
    high_avg = 0
    high_avg_name = ''

    print('Employee Name', '\t', 'Avg. Score')
    print('_____________', '\t', '__________', '\n')
    #EXTRACTS NAME AND SCORES FROM FILE, AND CALCULATES AVG SCORE FOR EACH EMP.
    for aLine in survey_scores:
        values = aLine.split(',')
        emp_name = values[0]
        score1 = int(values[1])
        score2 = int(values[2])
        score3 = int(values[3])
        calc_ = float((score1 + score2 + score3) / 3)
        #PRINTS EMP NAME AND AVG SCORE IN A TABLE; FORMATS FLOAT TO 2 DECIMAL PLACES, AND ADJUST SPACING OF COLUMNS.
        print('{:18} {:}'.format(emp_name, "%.2f" % calc_))
        #IDENTIFIES THE EMPLOYEE WITH THE HIGHEST AVG SCORE.
        if calc_ > high_avg:
            high_avg = calc_
            high_avg_name = emp_name
    #PRINTS THE NAME AND AVG OF THE EMPLOYEE IDENTIFIED AS HAVING THE HIGHEST AVG SCORE.
    print('\n' + 'The employee with the highest customer satisfaction survey average is:', high_avg_name, '(', "%.2f" % high_avg, ').', '\n')

    survey_scores.close()

def searchable_by_user():
    '''Allows the user to conduct filtered searches within the data records by entering an employee's name.
    The function returns the employee's performance evaluation rating (based on a avg score) and
    also displays the employee's individual survey scores from the csv data file'''

    survey_scores = open('survey_scores.csv', 'r')
    user_search = input( "Enter an employee's name to find out their performance evaluation rating and individual survey scores:").title()
    name_in_search = False

    for aLine in survey_scores:
        values1 = aLine.split(',')
        emp_name1 = values1[0]
        scoreone = int(values1[1])
        scoretwo = int(values1[2])
        scorethree = int(values1[3])
        calc_1 = float((scoreone + scoretwo + scorethree) / 3)
        #USING THE CALCULATED AVG SCORE OF EACH EMPLOYEE, IDENTIFIES WHAT PERFORMANCE RATING THEY FALL UNDER (NEEDS IMP,CONTRIBUTOR,
        # VALUED CONT, AND KEY CONT).
        if user_search == emp_name1:
            if calc_1 >= 90:
                perf_rating = 'Key Contributor (Average Score > 90%)'
            elif calc_1 < 90 and calc_1 >= 80:
                perf_rating = 'Valued Contibutor (Average Score between 80% to 89.99%)'
            elif calc_1 < 80 and calc_1 >= 70:
                perf_rating = 'Contibutor (Average Score between 70% to 79.99%)'
            elif calc_1 < 70:
                perf_rating = 'Needs Improvement (Average Score less than 70%)'
            #PRINTS THE RESULTS REQUESTED BY USER, SHOWING RATING AND INDIVIDUAL SATISFACTION SURVEY SCORES ON RECORD (.CSV FILE).
            search_results_r = 'The customer satisfaction performance evalution for ' + emp_name1 + ' is rated as: ' + perf_rating
            search_results_ind = emp_name1 + "'s individual satisfaction survey scores are: " + str(scoreone)+ ', ' + str(scoretwo) + ', ' + str(scorethree)
            name_in_search = True
        #ADDS SEARCHABILITY OPTIMIZATION, ALLOWING USERS TO ENTER A PARTIAL NAME OF THE DESIRED EMPLOYEE, AND THE
        # PROGRAM WILL STILL IDENTIFY THE RECORD AND DISPLAY THE CORRESPONDING RATING AND SCORES. EX- FOR THE NAME 'EMILIANO' USER CAN
        #  TYPE 'EMI' AND STILL GET THE DESIRED RESULTS.
        elif user_search[:] in emp_name1[:]:
            if calc_1 >= 90:
                perf_rating = 'Key Contributor (Average Score > 90%)'
            elif calc_1 < 90 and calc_1 >= 80:
                perf_rating = 'Valued Contibutor (Average Score between 80% to 89.99%)'
            elif calc_1 < 80 and calc_1 >= 70:
                perf_rating = 'Contibutor (Average Score between 70% to 79.99%)'
            elif calc_1 < 70:
                perf_rating = 'Needs Improvement (Average Score less than 70%)'

            search_results_r = 'The customer satisfaction performance evalution for ' + emp_name1 + ' is rated as: ' + perf_rating
            search_results_ind = emp_name1 + "'s individual satisfaction survey scores are: " + str(scoreone)+ ', ' + str(scoretwo) + ', ' + str(scorethree)
            name_in_search = True
    #RELAUNCHES THE FUNCTION AND PROMPTS THE USER TO RE-ENTER VALID NAME IF THE RECEIVED INPUT DOES NOT MATCH NAMES ON RECORD.
    if name_in_search == False:
        print('The employee name you entered does not match our records. Please try again.')
        searchable_by_user()
    #DISPLAYS DESIRED RESULTS BASED ON SEARCH INPUT
    print(search_results_r)
    print(search_results_ind)

    survey_scores.close()

def main():
    '''Creates a function called main to be used to execute all main program functions'''

    calc_avg()

    print('_____________________________________________________________','\n')

    searchable_by_user()

main()

Employee Performance Scores/Employee Performance Scores (Customer Satisfaction Surveys).py


import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets

import random_forest_classification

from sklearn.model_selection import train_test_split

import pickle

with open('dataset_4.pickle', 'rb') as f:
    X_train, y_train,  X_test, y_test, IDPeopleForTraining, IDPeopleForTesting = pickle.load(f)

print(len(X_train))
print(len(y_train))
print(len(X_test))
print(len(y_test))

class_names = np.array(['Fourchette', 'Cuillere', 'Couteau', 'Assiette', 'Verre', 'Tasse', 'Casserole', 'Poele', 'Fouet',
              'Louche', 'PressoirePatates', 'Cafetiere', 'Rouleau', 'MainAVide'])

print('People ID for the training dataset :')
print('         %s' % IDPeopleForTraining)
print('People ID for the testing dataset :')
print('         %s' % IDPeopleForTesting)

########################################################################################################################
#                                    User Settings for the Random Forest Classifier                                    #
########################################################################################################################

# Create a class object that define the parameters of the random forest classifier
random_forest_parameters = random_forest_classification.RandomForestParameters()

""" Number of trees in the forest.
    It should be an integer (and by defaut=10). """
random_forest_parameters.n_estimators = 100

""" Function to measure the quality of a split
    Supported criteria are “gini” for the Gini impurity and “entropy” for the information gain.
    Note: this parameter is tree-specific. """
random_forest_parameters.criterion = 'entropy'

""" The maximum depth of the tree.
    The choices are :
                    If None, then nodes are expanded until all leaves are pure or until all leaves contain less than
                        min_samples_split samples. """
random_forest_parameters.max_depth = None

""" The minimum number of samples required to split an internal node :
    The choices are :
                      If int, then consider min_samples_leaf as the minimum number.
                      If float, then min_samples_leaf is a fraction and ceil(min_samples_leaf * n_samples) are
                        the minimum number of samples for each node."""
random_forest_parameters.min_samples_split = 2

""" The minimum number of samples required to be at a leaf node.
    The choices are :
                      If int, then consider min_samples_split as the minimum number.
                      If float, then min_samples_split is a fraction and ceil(min_samples_split * n_samples) are the
                        minimum number of samples for each split. """
random_forest_parameters.min_samples_leaf = 1

""" The number of features to consider when looking for the best split
    The choices are :
                      If int, then consider max_features features at each split.
                      If float, then max_features is a fraction and int(max_features * n_features) features are considered at each split.
                      If “auto”, then max_features=sqrt(n_features).
                      If “sqrt”, then max_features=sqrt(n_features) (same as “auto”).
                      If “log2”, then max_features=log2(n_features).
                      If None, then max_features=n_features.

    Note: the search for a split does not stop until at least one valid partition of the node samples is found, even
    if it requires to effectively inspect more than max_features features."""
random_forest_parameters.max_feature = 'sqrt'

""" Grow trees with max_leaf_nodes in best-first fashion.
    If None then unlimited number of leaf nodes. """
random_forest_parameters.max_leaf_nodes = None

""" The number of jobs to run in parallel for both fit and predict.
    If -1, then the number of jobs is set to the number of CPU cores.
    The value by default is default=1. """
random_forest_parameters.n_jobs = 1

# Create a class object that define the performances container of the random forest classifier
performances = random_forest_classification.PerformancesRandomForest()

########################################################################################################################
#                                  Execute the Random Forest Classifier on the Dataset                                 #
########################################################################################################################

# Create and train the random forest classifier
random_forest_classifier, training_running_time = \
    random_forest_classification.train_random_forest_classifier(X_train, y_train, random_forest_parameters)

# Print information in the console
print("The training process of random forest classifier took : %.8f second" % training_running_time)

# Test the random forest classifier
y_test_predicted, testing_running_time = \
    random_forest_classification.test_random_forest_classifier(X_test, random_forest_classifier)

# Print information in the console
print("The testing process of random forest classifier took : %.8f second" % testing_running_time)

# Compute the performances of the random forest classifier
cm = random_forest_classification.compute_performances_for_multiclass(y_test, y_test_predicted, class_names, performances)

# Display the results
random_forest_classification.display_confusion_matrix(performances, class_names)
random_forest_classification.display_features_and_classification_for_rf_classifier(X_test, y_test, class_names,
                                                                                random_forest_classifier,
                                                                                random_forest_parameters)

plt.show()


import hero
import json
##json map as second File

#load save information from File
def Load(fileName):
  try:
    with open(fileName, 'rt'):
      f = open(fileName, "rt")
      heroName = f.readline().strip().lower()
      race = f.readline().strip().lower()
      heroClass  = f.readline().strip().lower()
      level = f.readline().strip()
      if heroClass == "warrior":
        char = hero.Warrior(heroName,race,heroClass)
        print(level)
        for i in range(int(level)-1):
          char.levelUp()
        print(char.level)
        return char
      elif heroClass == "wizard":
        char = hero.Wizard(heroName,race,heroClass)
        for i in range(int(level)-1):
          char.levelUp()
        return char
      elif heroClass == "ranger":
        char = hero.Ranger(heroName,race,heroClass)
        for i in range(int(level)-1):
          char.levelUp()
        return char

  except FileNotFoundError:
    print("not found")# Keep preset values

#load Map File
def mapLoad(fileName):
  mapFile=fileName+'map'
  try:
    with open(mapFile, 'r') as filehandle:
      basicList = json.load(filehandle)
      return basicList
  except FileNotFoundError:
    print("notfound")

#write hero and map information to file
def Save(fileName,hero,maped):
  try:
    with open(fileName, 'wt'):
      f = open(fileName, "w")
      f.write(hero.name + '\n' + hero.race + '\n' + hero.heroClass + '\n' + str(hero.level)+'\n')
      mapFile= fileName+'map'
      d=open(mapFile,'wt')
      json.dump(maped, d)

  except FileNotFoundError:
    print("not found")# Keep preset values


from newsfeeds.models import NewsFeed
from friendships.models import Friendship
from testing.testcases import TestCase
from rest_framework.test import APIClient
from rest_framework import status

NEWSFEEDS_URL = '/api/newsfeeds/'
POST_TWEETS_URL = '/api/tweets/'
FOLLOW_URL = '/api/friendships/{}/follow/'

class NewsFeedApiTest(TestCase):

    def setUp(self):
        self.gongzi = self.create_user('gongzi')
        self.gongzi_client = APIClient()
        self.gongzi_client.force_authenticate(self.gongzi)

        self.xiaoweige = self.create_user('xiaoweige')
        self.xiaoweige_client = APIClient()
        self.xiaoweige_client.force_authenticate(self.xiaoweige)

        # create following and followers
        for i in range(2):
            follower = self.create_user(f'xiaoweige_follower{i}')
            Friendship.objects.create(from_user=follower, to_user=self.xiaoweige)
        for i in range(3):
            following = self.create_user(f'xiaoweige_following{i}')
            Friendship.objects.create(from_user=self.xiaoweige, to_user=following)

    def test_list(self):
        # test login account
        response = self.anonymous_client.get(NEWSFEEDS_URL)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

        # test POST
        response = self.gongzi_client.post(NEWSFEEDS_URL)
        self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)

        # initial state, has nothing
        response = self.gongzi_client.get(NEWSFEEDS_URL)
        self.assertEqual(len(response.data['newsfeeds']), 0)

        self.gongzi_client.post(POST_TWEETS_URL, {'content': 'Hello Gongzi'})
        response = self.gongzi_client.get(NEWSFEEDS_URL)
        self.assertEqual(len(response.data['newsfeeds']), 1)

        self.gongzi_client.post(FOLLOW_URL.format(self.xiaoweige.id))
        response = self.xiaoweige_client.post(POST_TWEETS_URL, {
            'content': 'Hello Gongzi',
        })
        posted_tweet_id = response.data['id']
        response = self.gongzi_client.get(NEWSFEEDS_URL)
        self.assertEqual(len(response.data['newsfeeds']), 2)
        self.assertEqual(response.data['newsfeeds'][0]['tweet']['id'], posted_tweet_id)