from django.db import models, transaction
from rest_framework.authtoken.models import Token

from .element_types import INPUT, DATETIME, SELECT, RADIO, CHECKBOX, DATE, TIME, INT, FLOAT, TEXTAREA, BOOLEAN, \
    FILE_INPUT
from django.contrib.auth.models import User

from .sub_form_fields import get_related_attrs

class UserProfile(models.Model):
    """User profile"""
    user = models.OneToOneField(User, related_name="user_profile", on_delete=models.CASCADE)

    access_level = models.PositiveBigIntegerField(default=0)

    @property
    def token(self) -> str:
        token, created = Token.objects.get_or_create(user=self.user)
        return str(token.key)

    def __str__(self):
        return str(self.user)

class Template(models.Model):
    """Base template for a form that can be filled later"""
    creator = models.ForeignKey(UserProfile, related_name="templates",
                                on_delete=models.CASCADE)
    title = models.CharField(max_length=255, default="")

    access_level = models.PositiveBigIntegerField(default=0)

    is_paginated = models.BooleanField(default=False)

    @property
    def forms_count(self):
        return self.forms.count()

    def __str__(self):
        return str(self.title)

class Form(models.Model):
    """A Form is like a fork of a template, it includes elements,
    but elements are regarded as answers to the base template"""

    filler = models.ForeignKey(UserProfile, related_name="filled_forms", on_delete=models.CASCADE)
    fork_date = models.DateTimeField(auto_now_add=True)
    last_change_date = models.DateTimeField(auto_now=True)
    template = models.ForeignKey(Template, related_name="forms", on_delete=models.CASCADE)
    description = models.CharField(max_length=255, default="")

    class Meta:
        unique_together = ['template', 'description']

    def __str__(self):
        return "%s - %s" % (str(self.template), str(self.description))

class SubForm(models.Model):
    """
    SubForms make up different sections of each form
    """
    title = models.CharField(max_length=255, blank=True, null=True)
    description = models.CharField(max_length=1000, blank=True, null=True)
    order = models.IntegerField(default=0)
    template = models.ForeignKey(Template, related_name="sub_forms", on_delete=models.CASCADE)

    condition_element_type = models.CharField(max_length=255, blank=True, null=True)
    condition_element_pk = models.IntegerField(blank=True, null=True)
    condition_element_value = models.CharField(max_length=255, blank=True, null=True)

    def __str__(self):
        return "%s - %s" % (str(self.template), str(self.title))

class Field(models.Model):
    """
    A field consists of one or more field elements
    """
    sub_form = models.ForeignKey(SubForm, related_name="fields", on_delete=models.CASCADE)
    title = models.CharField(max_length=255, blank=True, null=True)
    order = models.IntegerField(default=0)
    is_grid = models.BooleanField(default=False)

    condition_element_type = models.CharField(max_length=255, blank=True, null=True)
    condition_element_pk = models.IntegerField(blank=True, null=True)
    condition_element_value = models.CharField(max_length=255, blank=True, null=True)

    def __str__(self):
        return "%s - %s" % (str(self.sub_form), str(self.title))

class Element(models.Model):
    """
    Base class for an input element (defaults to simple text field)
    value and filters fields may be overridden
    to cope with other input types

    # this field should be set according to the data type
    # Ex.: a simple text filed may be stored in CharField
    # Ex.: to store a datetime, value may be DateTimeField
    value = models.CharField(max_length=1024, blank=True, null=True)

    # array of available filters on an elements
    filters = ['icontains', 'startswith', 'endswith']

    all sub classes of element will be related to a sub form with a related named of the
    following format: fields_class_name, where class_name is the name of the subclass :)

    """

    # if the given element is used as answer
    # title, type, order, and field will be inherited from the question field
    # when serializing the element
    title = models.CharField(max_length=255)

    # Ex.: Input, checkbox, datetime, ...
    type = "ABC"

    # show this element based on some condition
    condition_element_type = models.CharField(max_length=255, blank=True, null=True)
    condition_element_pk = models.IntegerField(blank=True, null=True)
    condition_element_value = models.CharField(max_length=255, blank=True, null=True)

    # some element may have more than one value at a given time (Ex. Check box)
    # these elements save their values in "values" field as a CharField model
    # default value field for elements is "value"
    value_field = 'value'

    # any extra data that may be needed for the given element, can be saved in Data as
    # display, value pairs
    data = models.ManyToManyField("Data", blank=True)

    # display order of the field
    order = models.IntegerField(default=0)
    field = models.ForeignKey(Field, related_name="elements_%(class)s",
                              on_delete=models.CASCADE, blank=True, null=True)

    # these fields are used for an element that
    # will be regarded as an answer to an element of the same type
    answer_of = models.ForeignKey("self", related_name="answers",
                                  on_delete=models.CASCADE, blank=True, null=True)
    form = models.ForeignKey(Form, related_name="answers_%(class)s",
                             on_delete=models.CASCADE, blank=True, null=True)

    # indicates if element is editable
    disabled = models.BooleanField(default=False)

    quantitative_filters = [{"value": '', "display": "مساوی"},
                            {"value": 'gt', "display": "بزرگتر"},
                            {"value": 'lt', "display": "کوچکتر"},
                            {"value": 'gte', "display": "بزرگتر مساوی"},
                            {"value": 'lte', "display": "کوچکتر مساوی"}
                            ]
    literal_filters = [
        {"value": "", "display": "مساوی"},
        {"value": 'icontains', "display": "دربر دارد"},
        {"value": 'startswith', "display": "شروع میشود با"},
        {"value": 'endswith', "display": "پایان میابد با"}
    ]

    @classmethod
    def related_name_to_form(cls):
        return "answers_%s" % str.lower(cls.__name__)

    def short_str(self, data):
        return data[:50] + (data[50:] and ' ...')

    @property
    def display_title(self):
        return "%s -> %s -> %s" % (self.short_str(self.field.sub_form.title) if self.field.sub_form.title else "",
                                   self.short_str(self.field.title) if self.field.title else " ",
                                   self.short_str(self.title) if self.title else "")

    @property
    def display_title_full(self):
        return "%s -> %s -> %s" % (self.field.sub_form.title if self.field.sub_form.title else "",
                                   self.field.title if self.field.title else " ",
                                   self.title if self.title else "")

    @property
    def uid(self):
        return str(self.type) + str(self.pk)

    def __str__(self):
        return "%s - %s" % (str(self.field), str(self.title))

    class Meta:
        unique_together = ['answer_of', 'form']
        abstract = True

class Input(Element):
    """ Simple Text Input """
    value = models.CharField(max_length=1024, blank=True, null=True)
    type = INPUT
    filters = Element.literal_filters

class FileInput(Element):
    """ Simple Text Input """
    value = models.FileField(blank=True, null=True)
    type = FILE_INPUT
    filters = []

class Boolean(Element):
    """ Simple Text Input """
    value = models.BooleanField(blank=True, null=True)
    type = BOOLEAN
    filters = [{"value": "", "display": "مساوی"}]

class TextArea(Element):
    """ Simple Text Input """
    value = models.CharField(max_length=10240, blank=True, null=True)
    type = TEXTAREA
    filters = Element.literal_filters

class DateTimeElement(Element):
    """Date time field """

    value = models.DateTimeField(blank=True, null=True)
    type = DATETIME
    filters = Element.quantitative_filters

class SelectElement(Element):
    """Html select element with options"""

    value = models.CharField(max_length=1024, blank=True, null=True)
    type = SELECT
    filters = [{"value": '', "display": 'مساوی'}, ]  # empty filter string means exact match

class RadioElement(Element):
    """Html radio element with options"""

    value = models.CharField(max_length=1024, blank=True, null=True)
    type = RADIO
    filters = [{"value": '', "display": 'مساوی'}, ]  # empty filter string means exact match

class CharField(models.Model):
    """Raw char field"""
    value = models.CharField(max_length=1024, blank=True, null=True)

class CheckboxElement(Element):
    """Html checkbox element with options"""

    values = models.ManyToManyField(CharField, blank=True)
    type = CHECKBOX
    filters = [{"value": 'value__contains', "display": 'شامل'}, ]  # empty filter string means exact match
    value_field = 'values'

class DateElement(Element):
    """Html date element with options"""

    value = models.DateField(blank=True, null=True)
    type = DATE
    filters = Element.quantitative_filters

class TimeElement(Element):
    """Html TimeField element with options"""

    value = models.TimeField(blank=True, null=True)
    type = TIME
    filters = Element.quantitative_filters

class IntegerField(Element):
    """Html Int element with options"""

    value = models.IntegerField(blank=True, null=True)
    type = INT
    filters = Element.quantitative_filters

class FloatField(Element):
    """Html TimeField element with options"""

    value = models.FloatField(blank=True, null=True)
    type = FLOAT
    filters = Element.quantitative_filters

class Data(models.Model):
    """ Extra data used on select, radio, checkbox elements"""
    value = models.CharField(max_length=255)
    display = models.CharField(max_length=255)

elements = {
    INPUT: Input,
    DATE: DateElement,
    TIME: TimeElement,
    DATETIME: DateTimeElement,
    RADIO: RadioElement,
    CHECKBOX: CheckboxElement,
    SELECT: SelectElement,
    INT: IntegerField,
    TEXTAREA: TextArea,
    FLOAT: FloatField,
    BOOLEAN: Boolean,
    FILE_INPUT: FileInput
}


# Import libraries and modules
# os module is used for interacting with the operating system
# time module is used for getting file modification times
import os
import time

# Function to scan a directory and return a list of files and subdirectories
# This function takes a directory path as input and returns a list of all files and subdirectories within it.
def scan_directory(directory_path):
    try:
        # os.listdir() returns a list containing the names of the entries in the directory given by path.
        entries = os.listdir(directory_path)
        return entries
    except Exception as e:
        # Return any exception that occurs as a string
        return str(e)

# Function to filter files based on multiple criteria
# This function takes a file list and several optional parameters for filtering.
# extension: to filter by file extension (e.g., '.txt')
# min_size and max_size: to filter files that are within a specified size range
# modified_after: to filter files that were modified after a certain time
def filter_files(file_list, directory_path, extension=None, min_size=None, max_size=None, modified_after=None):
    filtered_files = []
    for file in file_list:
        # Create a full file path by joining the directory path and the file name
        file_path = os.path.join(directory_path, file)

        # Check if the file extension matches the criteria
        if extension and not file.endswith(extension):
            continue

        # Check if the file size matches the criteria
        if min_size or max_size:
            # os.path.getsize() returns the size of the file in bytes
            file_size = os.path.getsize(file_path)
            if min_size and file_size < min_size:
                continue
            if max_size and file_size > max_size:
                continue

        # Check if the file modification time matches the criteria
        if modified_after:
            # os.path.getmtime() returns the time of last modification of path
            file_time = os.path.getmtime(file_path)
            if file_time < modified_after:
                continue

        # If the file meets all the criteria, add it to the filtered_files list
        filtered_files.append(file)
    return filtered_files


#! /usr/bin/python3

import sys
sys.path.extend( [ "./mlak" ] )

import unittest
import numpy.testing as npt
from tests.data_gen import *

from collections import namedtuple

import mlak.mlak as mlak

from mlak.utils import *
import DataIO as dio
import ModelAnalyzer as mo
import LinearRegression as linReg
import LogisticRegression as logReg
import NeuralNetwork as nn
import KerasSolver as ks

class Args( dict ):
	def __init__( self_,
		data_set = None,
		solution = None,
		topology = None,
		Lambda = None,
		iterations = None,
		functions = [],
		engine = None,
		cvFraction = 0.1,
		testFraction = 0.1,
		verbose = False,
		debug = False
	):
		self_.data_set = data_set
		self_.solution = solution
		self_.topology = topology
		self_.Lambda = Lambda
		self_.iterations = iterations
		self_.functions = functions
		self_.engine = engine
		self_.cvFraction = cvFraction
		self_.testFraction = testFraction
		self_.verbose = verbose
		self_.debug = debug

class TestMLAK( unittest.TestCase ):
	def setUp( self ):
		fix_random()

	def test_set_preferred_engine( self ):
		mlak.set_preferred_engine( "linreg" )
		self.assertEqual( type( mlak.preferredEngine ), type( linReg.LinearRegressionSolver ) )
		mlak.set_preferred_engine( "logreg" )
		self.assertEqual( type( mlak.preferredEngine ), type( logReg.LogisticRegressionSolver ) )
		mlak.set_preferred_engine( "struggle" )
		self.assertEqual( type( mlak.preferredEngine ), type( nn.NeuralNetworkSolver ) )
		mlak.set_preferred_engine( "keras" )
		self.assertEqual( type( mlak.preferredEngine ), type( ks.KerasSolver ) )
		with self.assertRaises( Exception ):
			mlak.set_preferred_engine( "xxx" )

	def test_linreg_10_train( self ):
		X, y = gen_regression_data()
		dio.save( "./out/lin.p", { "X": X, "y": y } )
		args = Args(
			data_set = "./out/lin.p",
			solution = "./out/lin-sol.p",
			topology = [],
			Lambda = "0.1",
			iterations = 10,
			functions = [
				"lambda x: x[0] ** 2",
				"lambda x: x[1] ** 2",
				"lambda x: x[2] ** 2"
			],
			engine = "linreg"
		)
		mlak.set_preferred_engine( args.engine )
		with CapturedStdout():
			mlak.train( args )
		solution = dio.load( "./out/lin-sol.p" )
		npt.assert_almost_equal( solution.model(), [460.3618418, 12.1335894, 30.351926, 48.5490816, 32.8447929, 131.4717615, 229.648099] )
		npt.assert_almost_equal( solution.shaper().mu(), [-1.43274854e-02, 2.85087719e-02, 2.80701754e-02, 3.68073253e+01, 3.68513389e+01, 3.68301016e+01] )
		npt.assert_almost_equal( solution.shaper().sigma(), [6.06688718, 6.07046342, 6.06871598, 32.84525096, 32.86839366, 32.80732591] )

	def test_linreg_20_test( self ):
		X, y = gen_regression_data()
		args = Args(
			data_set = "./out/lin.p",
			solution = "./out/lin-sol.p",
			engine = "linreg"
		)
		mlak.set_preferred_engine( args.engine )
		with CapturedStdout() as out:
			mlak.test( args )
		self.assertAlmostEqual( float( out.getvalue() ), 7.117094744250965e-06 )

	def test_logreg_10_train( self ):
		X, y = gen_logistic_data()
		dio.save( "./out/log.p", { "X": X, "y": y } )
		args = Args(
			data_set = "./out/log.p",
			solution = "./out/log-sol.p",
			topology = [],
			Lambda = "0.1",
			iterations = 1,
			engine = "logreg"
		)
		mlak.set_preferred_engine( args.engine )
		with CapturedStdout():
			mlak.train( args )

		with CapturedStdout() as out:
			mlak.test( args )
		self.assertEqual( out.getvalue(), "0.2666666666666667\n" )
		solution = dio.load( "./out/log-sol.p" )
		npt.assert_equal( solution.shaper()._classesIdToLabel, ["diamond", "drill", "ripple"] )
		self.assertEqual( len( solution.model() ), 3 )
		npt.assert_almost_equal( np.sum( solution.model(), axis = 1 ), [15.9547132, -0.9575446, -4.9745893] )
		self.assertAlmostEqual( np.sum( solution.shaper().mu() ), 205.85431033692913 )
		self.assertAlmostEqual( np.sum( solution.shaper().sigma() ), 296.5719143203778 )

		fix_random()
		args.iterations = 9
		with CapturedStdout():
			mlak.train( args )

		with CapturedStdout() as out:
			mlak.test( args )
		self.assertEqual( out.getvalue(), "0.0\n" )
		solution = dio.load( "./out/log-sol.p" )
		npt.assert_equal( solution.shaper()._classesIdToLabel, ["diamond", "drill", "ripple"] )
		self.assertEqual( len( solution.model() ), 3 )
		npt.assert_almost_equal( np.sum( solution.model(), axis = 1 ), [20.5073757, -13.1428356, -10.8690069] )
		self.assertAlmostEqual( np.sum( solution.shaper().mu() ), 205.85431033692913 )
		self.assertAlmostEqual( np.sum( solution.shaper().sigma() ), 296.5719143203778 )

	def test_logreg_20_test( self ):
		X, y = gen_regression_data()
		args = Args(
			data_set = "./out/log.p",
			solution = "./out/log-sol.p",
			engine = "logreg"
		)
		mlak.set_preferred_engine( args.engine )
		with CapturedStdout() as out:
			mlak.test( args )
		self.assertEqual( out.getvalue(), "0.0\n" )

	def test_struggle_10_train( self ):
		X, y = gen_logistic_data()
		dio.save( "./out/struggle.p", { "X": X, "y": y } )
		args = Args(
			data_set = "./out/struggle.p",
			solution = "./out/struggle-sol.p",
			topology = ["10"],
			Lambda = "0.1",
			iterations = 1,
			engine = "struggle"
		)
		mlak.set_preferred_engine( args.engine )
		with CapturedStdout():
			mlak.train( args )
		with CapturedStdout() as out:
			mlak.test( args )
		self.assertEqual( out.getvalue(), "0.5706666666666667\n" )
		solution = dio.load( "./out/struggle-sol.p" )
		npt.assert_equal( solution.shaper()._classesIdToLabel, ["diamond", "drill", "ripple"] )
		npt.assert_equal( solution.model()[0], [256, 10, 3] )
		t = solution.model()[1];
		npt.assert_almost_equal( [np.sum( t[0] ), np.sum( t[1] ), np.sum( t[2] )], [0.14382  ,  0.0245429, -0.132043] )
		self.assertAlmostEqual( np.sum( solution.shaper().mu() ), 205.85431033692913 )
		self.assertAlmostEqual( np.sum( solution.shaper().sigma() ), 296.5719143203778 )
		fix_random()
		args.iterations = 9
		with CapturedStdout():
			mlak.train( args )
		with CapturedStdout() as out:
			mlak.test( args )
		self.assertEqual( out.getvalue(), "0.0\n" )
		solution = dio.load( "./out/struggle-sol.p" )
		npt.assert_equal( solution.shaper()._classesIdToLabel, ["diamond", "drill", "ripple"] )
		npt.assert_equal( solution.model()[0], [256, 10, 3] )
		t = solution.model()[1];
		npt.assert_almost_equal( [np.sum( t[0] ), np.sum( t[1] ), np.sum( t[2] )], [0.3095417, -0.0072933, -0.1675103] )
		self.assertAlmostEqual( np.sum( solution.shaper().mu() ), 205.85431033692913 )
		self.assertAlmostEqual( np.sum( solution.shaper().sigma() ), 296.5719143203778 )

	def test_struggle_20_test( self ):
		X, y = gen_regression_data()
		args = Args(
			data_set = "./out/struggle.p",
			solution = "./out/struggle-sol.p",
			engine = "struggle"
		)
		mlak.set_preferred_engine( args.engine )
		with CapturedStdout() as out:
			mlak.test( args )
		self.assertEqual( out.getvalue(), "0.0\n" )

	def test_keras_10_train( self ):
		X, y = gen_logistic_data()
		dio.save( "./out/keras.p", { "X": X, "y": y } )
		args = Args(
			data_set = "./out/keras.p",
			solution = "./out/keras-sol.p",
			topology = ["N(10)"],
			Lambda = "0.1",
			iterations = 10,
			engine = "keras"
		)
		mlak.set_preferred_engine( args.engine )
		with CapturedStdout():
			mlak.train( args )
		solution = dio.load( "./out/keras-sol.p" )

	def test_keras_20_test( self ):
		X, y = gen_regression_data()
		args = Args(
			data_set = "./out/keras.p",
			solution = "./out/keras-sol.p",
			engine = "keras"
		)
		mlak.set_preferred_engine( args.engine )
		with CapturedStdout() as out:
			mlak.test( args )
		self.assertEqual( out.getvalue(), "0.0\n" )

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


"""app URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URL_conf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.conf.urls.static import static
from django.urls import path

from app import views, settings

urlpatterns = [
                  path('', views.normal),
                  path('admin/', admin.site.urls),
                  path('login', views.login),
                  path('register', views.register),
                  path('append/tie', views.append_tie),
                  path('reply/tie', views.append_floor),
                  path('reply/floor', views.append_reply_floor),
                  path('get/tie', views.get_tie_by_id),
                  path('get/tie/list', views.get_tie_list),
                  path('get/floor', views.get_floor),
                  path('get/reply_floor', views.get_reply_floor),
                  path('like', views.like),
                  path('get/user/info', views.get_user_info),
                  path('get/user/tie', views.get_user_tie),
                  path('get/ba', views.get_ba),
                  path('sign', views.sign),
                  path('subscription/ba', views.subscription_ba),
                  path('change/user_info', views.change_user_info),
                  path('change/avatar', views.change_avatar),
                  path(r'save/image', views.save_image),
                  # 就是路由-操作对
              ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""

@author: glen-dsouza
"""

import pickle
import numpy as np
import math
from numpy.random import seed
from numpy.random import rand

class Task8:

	def getRandomSimilarityMatrixFromWeightMatrix(self,weight_matrix):
	    similarity_matrix={}
	    seed(1)
	    lengths = [len(v) for v in weight_matrix.values()]
	    mx=np.random.rand(len(weight_matrix.keys()),lengths[0]) #len(weight_matrix.keys())
	    row=0
	    col=0
	    for i in weight_matrix.keys():
	        rowDict={}
	        for j in weight_matrix[i].keys():
	            val=mx[row][col]
	            rowDict.update({str(j):val})
	            col=col+1
	        col=0
	        row=row+1
	        similarity_matrix.update({i:rowDict})
	    return similarity_matrix

	def task8_Subtask1(self,S_S_WeightMatrix,n,m):
		NGraph={}
		for i in S_S_WeightMatrix:
			topKEdges=dict(sorted(S_S_WeightMatrix[i].items(), key=lambda item: item[1], reverse=True))
			N_similar_neighbors={}
			for j in topKEdges:
				N_similar_neighbors.update({j:topKEdges[j]})
			NGraph[i]=N_similar_neighbors
		TopNNodes={}
		nodeNum=0
		for i in NGraph.keys():
			nodeNum=0
			edges={}
			for j in NGraph[i].keys():
				if(nodeNum==0):
					nodeNum=nodeNum+1
					continue
				if(int(nodeNum)<=int(n)):
					edges.update({j:NGraph[i][j]})
					nodeNum=nodeNum+1
				else:
					break
				TopNNodes.update({i:edges})
		return TopNNodes

	def AscosPlus(self,weight_matrix,m):
	    #check if graph converges
	    #Optimization function for similarity_matrix
	    #similarity_matrix=np.random.rand(len(weight_matrix.keys()),len(weight_matrix.keys()))
	    similarity_matrix=self.getRandomSimilarityMatrixFromWeightMatrix(weight_matrix)
	    while(True): #To converge
	        #print('similarity score is')
	        #print(similarity_matrix)
	        current_matrix=self.copydict(similarity_matrix)
	        for i in similarity_matrix.keys():
	            for j in similarity_matrix[i].keys():
	                similarity_matrix[i][j]=self.calculateSimilarity(similarity_matrix,i,j,weight_matrix)
	        diff=self.calculateError(current_matrix,similarity_matrix)
	        if(diff==0.0):
	            break
	    # print('Ascos++')
	    # print(similarity_matrix)
	    topNodes = self.findTopMNodes(m,similarity_matrix)
	    return topNodes

	def calculateWStar(self,i,weight_matrix):
	    weight=0;
	    for k in weight_matrix[i].keys():
	        if((i in weight_matrix[k].keys())):
	            weight=weight+weight_matrix[i][k]
	    return weight

	def calculateError(self,intialMatrix, CurrentMatrix):
	    diff=0;
	    for i in intialMatrix.keys():
	        for j in intialMatrix[i].keys():
	            diff=diff+abs(intialMatrix[i][j]-CurrentMatrix[i][j])
	    return diff

	def findTopMNodes(self,m, similarity_matrix):
	    nodes={}
	    for i in similarity_matrix.keys():
	        val=0
	        N=0
	        for j in similarity_matrix[i].keys():
	            val=val+similarity_matrix[i][j]
	            N=len(j)
	        val=val/(N)
	        nodes.update({i:val})
	    topNodes=dict(sorted(nodes.items(), key=lambda item: item[1],reverse=True))
	    topMNodes={}
	    r=0
	    for i in topNodes:
	    	if(int(r)==int(m)):
	    		break
	    	r=r+1
	    	topMNodes.update({i:topNodes[i]})
	    return topMNodes

	#weight matrix is the subject subject similarity matrix
	def calculateSimilarity(self,similarity_matrix,i,j, weight_matrix):
	    #No. of incoming nodes to i is i'th column
	    c=1
	    val=0
	    if(i==j):
	        return 1.0
	    wStar = self.calculateWStar(i,weight_matrix)
	    #k is the neighbor node in consideration
	    for k in weight_matrix[i].keys():
	        if(k==i or (i not in weight_matrix[k].keys()) or (j not in weight_matrix[k].keys())):
	            continue
	        else:
	            val=val+((float(weight_matrix[i][k])/wStar)*(1-math.exp(-weight_matrix[i][k])))*similarity_matrix[k][j]
	    ans = c*val
	    return ans

	def copydict(self,similarity_matrix):
	    copy_matrix={}
	    for i in similarity_matrix.keys():
	        edges={}
	        for j in similarity_matrix[i].keys():
	            edges.update({j:similarity_matrix[i][j]})
	        copy_matrix.update({i:edges})
	    return copy_matrix


import pandas as pd
import numpy.random as nr
from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import train_test_split
from sklearn import metrics
import numpy as np

import math

from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score, f1_score

from sklearn import ensemble

from sklearn import linear_model

from sklearn.preprocessing import LabelEncoder

#funkcija koja traži najbolje hiperparametre gradient boostinga
def gbHyperparametars(X_train, y_train):
    algorithmParameters = dict() #dict hiperparametara koje ćemo testirati

    #popunjavanje algorithmParameters
    nEstimatorsValues = []
    for i in range(6, 12):
        nEstimatorsValues.append(2**i)

    algorithmParameters['n_estimators'] = nEstimatorsValues

    learningRateValues  = []
    for i in range(3):
        learningRateValues.append(0.01 * 10**i)

    algorithmParameters['learning_rate'] = learningRateValues

    algorithmParameters['max_depth'] = [2, 3, 4]

    model = ensemble.GradientBoostingClassifier()

    #traženje i ispis najboljih hiperparametara
    grid = GridSearchCV(model, algorithmParameters)
    grid.fit(X_train, y_train)

    print(grid.best_estimator_.n_estimators)
    print(grid.best_estimator_.max_depth)
    print(grid.best_estimator_.learning_rate)

    classifierGB = ensemble.GradientBoostingClassifier(n_estimators = grid.best_estimator_.n_estimators, max_depth = grid.best_estimator_.max_depth,
                                                learning_rate = grid.best_estimator_.learning_rate)

    return classifierGB

#funkcija koja traži najbolje hiperparametre random forest
def rfHyperparametars(X_train, y_train, n_estimators):

    algorithmParameters = dict()

    algorithmParameters['min_samples_split'] = [2, 3, 4, 8]

    algorithmParameters['max_features'] = [0.01, 0.05, 0.1]

    #algorithmParameters['max_depth'] = [16, 32, None]

    model = ensemble.RandomForestClassifier(n_estimators = n_estimators)

    grid = GridSearchCV(model, algorithmParameters)
    grid.fit(X_train, y_train)

    #print(grid.best_estimator_.max_depth)
    print(grid.best_estimator_.min_samples_split)
    print(grid.best_estimator_.max_features)

    classifierRF = ensemble.RandomForestClassifier(n_estimators = n_estimators, min_samples_split = grid.best_estimator_.min_samples_split,
                                                    max_depth = None, max_features = grid.best_estimator_.max_features)

    return classifierRF

#hiperparametri za logističku regresiju
def lrHyperparametars(X_train, y_train):

    algorithmParameters = dict()

    algorithmParameters['penalty'] = ['l1', 'l2']

    CValues   = []
    for i in range(1, 15):
        CValues.append(0.001 * 2**i)

    algorithmParameters['C'] = CValues

    #algorithmParameters['dual'] = [True, False]

    model = linear_model.LogisticRegression()

    grid = GridSearchCV(model, algorithmParameters)
    grid.fit(X_train, y_train)

    print(grid.best_estimator_.penalty)
    #print(grid.best_estimator_.dual)
    print(grid.best_estimator_.C)

    classifierLR = linear_model.LogisticRegression(penalty = grid.best_estimator_.penalty, C = grid.best_estimator_.C)
    #dual = grid.best_estimator_.dual)

    return classifierLR

def getUserDescription(df_train, df_test, featureName = 'description'):

    userTrain = df_train.user

    userTextTrain = []

    for dictionary in userTrain:

        temp = ''

        for key, value in dictionary.items():

            if key == featureName:
                temp = value

        userTextTrain.append(temp)

    userTextTrain = np.asarray(userTextTrain)

    userTest = df_test.user

    userTextTest = []

    for dictionary in userTest:

        temp = ''

        for key, value in dictionary.items():

            if key == featureName:
                temp = value

        userTextTest.append(temp)

    userTextTest = np.asarray(userTextTest)

    return userTextTrain, userTextTest

def getHashtag(df_train, df_test):

    firstEntities = df_train.entities

    hashtagsArray = []

    for dictionary in firstEntities:
        temp = ''

        for key, values in dictionary.items():

            if key == 'hashtags':

                for i in range(values.__len__()):

                    if isinstance(values[i], dict):
                        for keyTemp, valueTemp in values[i].items():
                            if keyTemp == 'text':

                                temp = valueTemp

        hashtagsArray.append(temp)

    hashtagsArray = np.asarray(hashtagsArray)

    firstEntities = df_test.entities

    hashtagsArrayTest = []

    for dictionary in firstEntities:
        temp = ''

        for key, values in dictionary.items():

            if key == 'hashtags':

                for i in range(values.__len__()):

                    if isinstance(values[i], dict):
                        for keyTemp, valueTemp in values[i].items():
                            if keyTemp == 'text':

                                temp = valueTemp

        hashtagsArrayTest.append(temp)

    hashtagsArrayTest = np.asarray(hashtagsArrayTest)

    return hashtagsArray, hashtagsArrayTest

def create_output_file(filename, predictions, sentences, labels = []):

    if labels.__len__() == 0:
        df = pd.DataFrame({'Tekst' : sentences, 'Predikcija' : predictions})
    else:
        df = pd.DataFrame({'Tekst' : sentences, 'Predikcija' : predictions, 'Ispravne kategorije' : labels})

    df.to_csv(filename + '.csv')

df1 = pd.read_json('C:\\Users\\viktor\\Downloads\\hs_data\\share\\sexism.json', lines=True)
df2 = pd.read_json('C:\\Users\\viktor\\Downloads\\hs_data\\share\\racism.json', lines=True)
df3 = pd.read_json('C:\\Users\\viktor\\Downloads\\hs_data\\share\\neither.json', lines=True)

frames = [df1, df2, df3]

print(df1.__len__())
print(df2.__len__())
print(df3.__len__())

df = pd.concat(frames, ignore_index = True)

print(df.__len__())

from sklearn.utils import shuffle
df = shuffle(df)

del df['contributors']
del df['coordinates']
del df['extended_entities']
del df['geo']
del df['quoted_status']
del df['is_quote_status']
del df['quoted_status_id']
del df['quoted_status_id_str']
del df['retweeted']
del df['favorited']
del df['truncated']
del df['withheld_in_countries']
del df['possibly_sensitive']
del df['possibly_sensitive_appealable']
del df['id_str']
del df['in_reply_to_status_id']
del df['in_reply_to_screen_name']
del df['in_reply_to_status_id_str']
del df['in_reply_to_user_id']
del df['in_reply_to_user_id_str']

#df['Annotation'] = df['Annotation'].astype('category').cat.codes
df['lang'] = df['lang'].astype('category').cat.codes

from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.corpus import stopwords

sw = stopwords.words("english")
vectorizer = TfidfVectorizer(lowercase=True, stop_words = sw, binary = True, sublinear_tf  = True, norm = None)

df_train, df_test = train_test_split(df, test_size = 0.2)

#tweetovi i label
X_train = df_train['text'].as_matrix()
y_train = df_train['Annotation'].as_matrix()

X_test = df_test['text'].as_matrix()
y_test = df_test['Annotation'].as_matrix()

X_train = vectorizer.fit_transform(X_train).toarray()
X_test = vectorizer.transform(X_test).toarray()

#dodaj user description
userTextTrain, userTextTest = getUserDescription(df_train, df_test)

userTextTrain = vectorizer.fit_transform(userTextTrain).toarray()
userTextTest = vectorizer.transform(userTextTest).toarray()

X_train = np.concatenate((X_train, userTextTrain), axis=1)
X_test = np.concatenate((X_test, userTextTest), axis=1)

#dodaj favorite_count
favorite_countTrain = df_train['favorite_count'].as_matrix()
favorite_countTest = df_test['favorite_count'].as_matrix()

ss = StandardScaler()

favorite_countTrain = ss.fit_transform(favorite_countTrain)
favorite_countTest = ss.transform(favorite_countTest)

favorite_countTrain = np.reshape(favorite_countTrain, (-1, 1))
favorite_countTest = np.reshape(favorite_countTest, (-1, 1))

X_train = np.concatenate((X_train, favorite_countTrain), axis=1)
X_test = np.concatenate((X_test, favorite_countTest), axis=1)

#dodaj retweet_count
retweet_countTrain = df_train['retweet_count'].as_matrix()
retweet_countTest = df_test['retweet_count'].as_matrix()

retweet_countTrain = ss.fit_transform(retweet_countTrain)
retweet_countTest = ss.transform(retweet_countTest)

retweet_countTrain = np.reshape(retweet_countTrain, (-1, 1))
retweet_countTest = np.reshape(retweet_countTest, (-1, 1))

X_train = np.concatenate((X_train, retweet_countTrain), axis=1)
X_test = np.concatenate((X_test, retweet_countTest), axis=1)

mlb = LabelEncoder()
y_train = mlb.fit_transform(y_train)
y_test = mlb.transform(y_test)

#dodaj user description
userTextTrain, userTextTest = getUserDescription(df_train, df_test, 'name')

userTextTrain = vectorizer.fit_transform(userTextTrain).toarray()
userTextTest = vectorizer.transform(userTextTest).toarray()

X_train = np.concatenate((X_train, userTextTrain), axis=1)
X_test = np.concatenate((X_test, userTextTest), axis=1)

#dodaj hashtags

hashTrain, hashTest = getHashtag(df_train, df_test)

hashTrain = vectorizer.fit_transform(hashTrain).toarray()
hashTest = vectorizer.transform(hashTest).toarray()

X_train = np.concatenate((X_train, hashTrain), axis=1)
X_test = np.concatenate((X_test, hashTest), axis=1)

#isprobaj svm
from sklearn.svm import LinearSVC

svc = LinearSVC(C = 0.0016)

svc.fit(X_train, y_train)

pred = svc.predict(X_test)

print(pred)
print(accuracy_score(pred, y_test))
print(f1_score(pred, y_test, average = 'macro'))

'''
#isprobaj lr i odredi joj hiperparametre
#lgr = lrHyperparametars(X_train, y_train)
lgr = linear_model.LogisticRegression(C = 0.128, penalty = 'l1')
lgr.fit(X_train, y_train)
pred = lgr.predict(X_test)
print(pred)
print(accuracy_score(pred, y_test))
print(f1_score(pred, y_test, average = 'macro'))
'''

#isprobaj rf i odredi mu hiperparametre
#rf = rfHyperparametars(X_train, y_train, 15) #None 0.1
rf = ensemble.RandomForestClassifier(n_estimators = 25, criterion="entropy", min_samples_split = 4, max_features = 0.05, n_jobs = -1)

rf.fit(X_train, y_train)
pred = rf.predict(X_test)
print(pred)
print(accuracy_score(pred, y_test))
print(f1_score(pred, y_test, average = 'macro'))

'''

#isprobaj gradient boosting
gb = ensemble.GradientBoostingClassifier()

gb.fit(X_train, y_train)
pred = gb.predict(X_test)
print(pred)
print(accuracy_score(pred, y_test))
print(f1_score(pred, y_test, average = 'micro'))
'''

pred = mlb.inverse_transform(pred)

create_output_file("testAll", pred, df_test['text'].as_matrix(), mlb.inverse_transform(y_test))


from django.db import models

class PhoneNumberManager(models.Manager):
    # def can_add_phone(self, user):
    #     ret = True
    #     if app_settings.MAX_EMAIL_ADDRESSES:
    #         count = self.filter(user=user).count()
    #         ret = count < app_settings.MAX_EMAIL_ADDRESSES
    #     return ret

    def add_phone(self, request, user, phone, confirm=False, signup=False):
        phone_number, created = self.get_or_create(
            user=user, phone__iexact=phone, defaults={"phone": phone}
        )

        if created and confirm:
            phone_number.send_confirmation(request, signup=signup)

        return phone_number

    def get_primary(self, user):
        try:
            return self.get(user=user, primary=True)
        except self.model.DoesNotExist:
            return None

    def get_users_for(self, phone):
        # this is a list rather than a generator because we probably want to
        # do a len() on it right away
        return [
            phone_number.user for phone_number in self.filter(verified=True, phone__iexact=phone)
        ]

    def fill_cache_for_user(self, user, phone_numbers):
        """
        In a multi-db setup, inserting records and re-reading them later
        on may result in not being able to find newly inserted
        records. Therefore, we maintain a cache for the user so that
        we can avoid database access when we need to re-read..
        """
        user._phonenumber_cache = phone_numbers

    def get_for_user(self, user, phone):
        cache_key = "_phonenumber_cache"
        phone_numbers = getattr(user, cache_key, None)
        if phone_numbers is None or (type(phone_numbers) == list and phone_numbers[0] is None):
            ret = self.get(user=user, phone__iexact=phone)
            # To avoid additional lookups when e.g.
            # EmailAddress.set_as_primary() starts touching self.user
            ret.user = user
            return ret
        else:
            for phone_number in phone_numbers:
                if phone_number.phone == phone:
                    return phone_number
            raise self.model.DoesNotExist()


import glob
import os
import re
import networkx as nx
import pandas as pd
import streamlit as st
import streamlit.components.v1 as components
from collections import defaultdict
from pyvis.network import Network

TEAM_COLORS = [ '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#1f77b4', ]

@st.cache(persist=True)
def load_nodes(input_dir, protagonist_score='Interactions', protagonist_ratio=1.75):
    """Load all nodes from from input_dir and initialise database."""
    all_nodes = {}
    all_edges = {}
    all_timed_edges = {}
    metadata = {}

    filepath = os.path.join(input_dir, '*.paragraphed_nodes.csv')
    no_of_files = len(glob.glob(filepath))

    for i, f in enumerate(sorted(glob.glob(filepath))):
        # Get filename of glob
        filename = os.path.basename(f)
        book_name = filename.split('.paragraphed_nodes.csv')[0]
        nodes = pd.read_csv(f)
        nodes.sort_values(by='Interactions', ascending=False, inplace=True)
        metadata[book_name] = {
            'author': book_name.split('_')[1],
            'title': book_name.split('_')[2],
            'year': book_name.split('_')[0],
            'no. of protagonists': None,
            'protagonists': None,
            'name': book_name
        }
        protagonists = get_protagonists(nodes, protagonist_score, protagonist_ratio)
        metadata[book_name]['protagonists'] = protagonists
        metadata[book_name]['no. of protagonists'] = len(protagonists)

        all_nodes[book_name] = nodes

        # Read edges
        df_edges = pd.read_csv(os.path.join(input_dir, f'{book_name}.paragraphed_edges.csv'))
        all_edges[book_name] = df_edges

        df_timed_edges = pd.read_csv(os.path.join(input_dir, f'{book_name}.paragraphed_edges_timestamped.csv'))
        all_timed_edges[book_name] = df_timed_edges

    return metadata, all_nodes, all_edges, all_timed_edges

@st.cache
def count_networks(all_metadata, from_year=1870, to_year=1899):
    """Statistics for network."""
    counts = {}
    years = {}
    for year in range(from_year, to_year+1):
        years[str(year)] = 0
        counts[str(year)] = defaultdict(int)
    for book in all_metadata.values():
        if book['year'] in years.keys():
            years[book['year']] += 1
        counts[book['year']][len(book['protagonists'])] += 1
    return counts

def get_protagonists(df_nodes: pd.DataFrame, col_name, protagonist_ratio) -> list:
    """Determine how many protagonists there are in the network."""
#    print(f"Determining protagonists...{col_name}, {protagonist_ratio}")
    df_copy = df_nodes.sort_values(by=col_name, ascending=False)
    prev_value = None
    protagonists = []
    for _, row in df_copy.iterrows():
        if prev_value is None:
            prev_value = row[col_name]
            protagonists.append(row['Id'])
            continue
        row_ratio = prev_value / row[col_name]
        if row_ratio >= protagonist_ratio:
            break
        protagonists.append(row['Id'])
        prev_value = row[col_name]
        if len(protagonists) >= 5:
            protagonists = []
            break
    return protagonists

def show_network(G):
    """Show the network using pyvis."""
    ner_net = Network(height='1024px', width='800px', bgcolor='#222222', font_color='white')
    ner_net.from_nx(G)

    path = '/tmp'
    ner_net.save_graph(f'{path}/pyvis_graph.html')
    HtmlFile = open(f'{path}/pyvis_graph.html','r', encoding='utf-8')

    # Load HTML file in HTML component for display on Streamlit page
    components.html(HtmlFile.read(), height=1024, width=800)

import numpy as np

def lighter(color, percent):
    '''assumes color is rgb between (0, 0, 0) and (255, 255, 255)'''
    color = np.array(hex2rgb(color))
    white = np.array([255, 255, 255])
    vector = white-color
    ligthened = tuple(color + vector * percent)
    ligthened = int(ligthened[0]), int(ligthened[1]), int(ligthened[2])
    return rgb2hex(ligthened)

def hex2rgb(hex):
    hex = hex.lstrip('#')
    return tuple(int(hex[i:i+2], 16) for i in (0, 2, 4))

def rgb2hex(rgb):
    return '#%02x%02x%02x' % rgb

def create_network(df_nodes, df_edges, size_from='Degree', teams: dict={}):
    """Create networkx network from nodes and edges."""
    protagonists = list(teams.keys())
    team_members = {}
    for protagonist, members in teams.items():
        for member, affinity_score in members:
            team_members[member] = {'leader': protagonist, 'affinity_score': affinity_score}

    if teams:
        max_teamness = max([score for xs in teams.values() for _name, score in xs])

    G = nx.Graph()
    memory = []
    for _, row in df_nodes.iterrows():
        memory.append(str(row['Id']))
        color = None
        node = row['Id']
        if team_members.get(node):
            team_leader = team_members[node]['leader']
            team = teams[team_leader]
            team_idx = protagonists.index(team_leader)
            team_color = TEAM_COLORS[team_idx]
            teamness = [score for name, score in team if name == row['Id']][0]
            teamness = teamness / max_teamness
            color = lighter(team_color, 1-(teamness/2+0.5))
        elif node in protagonists:
            team_idx = protagonists.index(node)
            team_color = TEAM_COLORS[team_idx]
            color = team_color

        G.add_node(str(row['Id']), size=int(row[size_from]), weight=int(row['Interactions']),
                   color=color)

    for _, row in df_edges.iterrows():
        if str(row['source']) in memory and str(row['target']) in memory:
            G.add_edge(str(row['source']), str(row['target']), value=int(row['weight'])*int(row["weight"]))
    show_network(G)

def create_network_from_edges(df_edges):
    """Create network from edges only."""
    G = nx.Graph()
    for _, row in df_edges.iterrows():
        G.add_edge(str(row['source']), str(row['target']), value=int(row['weight'])*int(row["weight"]))

    degrees = dict(G.degree)
    nx.draw(G, nodelist=degrees.keys(), node_size=[v * 100 for v in degrees.values()])

    show_network(G)

def split_by_timestamp(df, no_of_slices=3):
    """Split df in equal parts."""

    size = int(len(df)/no_of_slices)
    N = no_of_slices
    df_slices = [ df.iloc[i*size:(i+1)*size].copy() for i in range(N+1) ]
    # cols = st.columns(3)
    # cols[0].metric('Len. I', len(df_slices[0]))
    # cols[1].metric('Len. I', len(df_slices[1]))
    # cols[2].metric('Len. I', len(df_slices[2]))

    df_slices = [timestamp_to_weight(df_slice) for df_slice in df_slices]
    return df_slices[0], df_slices[1], df_slices[2]

def timestamp_to_weight(df):
    """Return dataframe with timestamp column converted into weight."""
    df_grouped = df.groupby(['source','target']).count()
    df_grouped.reset_index(inplace=True)
    df_grouped.rename(columns={'timestamp': 'weight'}, inplace=True)
    df_grouped['type'] = 'undirected'
    return df_grouped

@st.cache
def create_nodes(df_edges):
    """Return dataframe with nodes from edges file."""

    G = nx.from_pandas_edgelist(df_edges, edge_attr='weight')

    nodes = []
    for node_id in G.nodes:
        interactions = sum(df_edges[df_edges['source'] == node_id]['weight']) \
                       + sum(df_edges[df_edges['target'] == node_id]['weight'])
        node = {
            'Id': node_id,
            'Connections': G.degree[node_id],
            'Interactions': interactions,
            'Degree': G.degree[node_id]
        }
        nodes.append(node)

    return pd.DataFrame(nodes)

@st.cache
def get_teams(df_nodes: pd.DataFrame, df_edges: pd.DataFrame, protagonists: list, team_ratio: int = 3) -> list:
    """Determine teams for networks with more than one central character."""
    if len(protagonists) < 2:
        return {}

    edges_to_protagonists = {}
    for _, row in df_nodes.iterrows():
        if row['Id'] in protagonists:
            continue
        current_edges = {}
        for protagonist in protagonists:
            count = 0
            cell = df_edges.loc[(df_edges['source'] == row['Id'])
                                & (df_edges['target'] == protagonist), 'weight']
            if cell.values:
                count += int(cell.values[0])
            cell = df_edges.loc[(df_edges['source'] == protagonist)
                                & (df_edges['target'] == row['Id']), 'weight']
            if cell.values:
                count += int(cell.values[0])
            current_edges[protagonist] = count
        edges_to_protagonists[row['Id']] = current_edges

    teams = defaultdict(list)
    for character, character_counts in edges_to_protagonists.items():
        sorted_counts = {k: v for k, v in sorted(character_counts.items(), key=lambda item: item[1])}
        counts = list(sorted_counts.values())
        protagonist = list(sorted_counts.keys())[-1]
        if counts[-1] == 0 and counts[-2] == 0:
            continue
        elif counts[-2] == 0:
            teams[protagonist].append((character, counts[-1]))
        else:
            affinity_score = counts[-1] / counts[-2]
            if affinity_score >= team_ratio:
                teams[protagonist].append((character, affinity_score))
    for protagonist, character_teams in teams.items():
        # Sort teams by disruption score
        character_teams.sort(key=lambda x: x[1], reverse=True)
    return teams


import sys
import socket
import functools

import easycli as cli

from .constants import IGMP_PORT, IGMP_ADDRESS, VERBS, DEFAULT_DBFILE, \
    DEFAULT_TIMEOUT, BINARY_CONTENTTYPES
from . import protocol, cache, resolve, http

hostname_arg = functools.partial(cli.Argument, 'hostname')
nocache_arg = functools.partial(cli.Argument, '--nocache', action='store_true')
address_arg = functools.partial(cli.Argument, '-a', '--address')
port_arg = functools.partial(cli.Argument, '-p', '--port', type=int)
short_arg = functools.partial(
    cli.Argument,
    '-s',
    '--short',
    action='store_true'
)
timeout_arg = functools.partial(
    cli.Argument,
    '-t',
    '--timeout',
    type=int,
    default=DEFAULT_TIMEOUT,
    help=f'Seconds wait before exit, 0: infinite, default: {DEFAULT_TIMEOUT}'
)
noresolve_arg = functools.partial(
    cli.Argument,
    '--noresolve',
    action='store_true',
    help='Do not resolve the name over network.'
)
forceresolve_arg = functools.partial(
    cli.Argument,
    '-f', '--forceresolve',
    action='store_true',
    help='Force to resolve the name over network and update cache.'
)

stdout = sys.stdout
stderr = sys.stderr

def output(*a, **kw):
    print(*a, file=stdout, **kw)

def error(*a, **kw):
    print(*a, file=stderr, **kw)

def printrecord(name, addr, cache, short=False):
    if short:
        output(addr)
        return

    flags = ' [cache]' if cache else ''
    output(f'{addr} {name}{flags}')

class Answer(cli.SubCommand):
    """Answer command line interface."""

    __command__ = 'answer'
    __aliases__ = ['a', 'ans']
    __arguments__ = [
        hostname_arg(default=IGMP_ADDRESS),
        address_arg(default=IGMP_ADDRESS, help=f'Default: {IGMP_PORT}'),
        port_arg(default=IGMP_PORT, help=f'Default: {IGMP_ADDRESS}'),
    ]

    def __call__(self, args):
        output(f'Answering {args.hostname} to {args.address}:{args.port}')
        protocol.answer(args.hostname, address=args.address, port=args.port)

class Sniff(cli.SubCommand):
    """Sniff IGMP packets."""

    __command__ = 'sniff'
    __aliases__ = ['s']
    __arguments__ = [
    ]

    def __call__(self, args):
        output(f'Listening to {IGMP_ADDRESS}:{IGMP_PORT}')
        try:
            for verb, name, addr, port in protocol.sniff():
                output(f'{addr}:{port} {VERBS.get(verb)} {name}')
        except KeyboardInterrupt:
            error('Terminated by user.')
            return 3

class Find(cli.SubCommand):
    """Find an ip address by it's name."""

    __command__ = 'find'
    __aliases__ = ['f']
    __arguments__ = [
        cli.Argument('pattern'),
        nocache_arg(),
        short_arg(),
        timeout_arg(),
    ]

    def __call__(self, args):
        if not args.nocache:
            with cache.DB(args.dbfile) as db:
                for name, addr in db.find(args.pattern):
                    printrecord(name, addr, True, short=args.short)

        # Searching network
        for n, a in protocol.find(args.pattern, timeout=args.timeout):
            printrecord(n, a, False, short=args.short)

class Resolve(cli.SubCommand):
    """Resolve an ip address by it's name."""

    __command__ = 'resolve'
    __aliases__ = ['r', 'd']
    __arguments__ = [
        hostname_arg(),
        cli.Mutex(
            noresolve_arg(),
            forceresolve_arg(),
        ),
        short_arg(),
        timeout_arg(),
    ]

    def __call__(self, args):
        addr, cached = resolve(
            args.hostname,
            args.timeout,
            args.forceresolve,
            args.noresolve,
            args.dbfile
        )
        printrecord(args.hostname, addr, cached, short=args.short)

class HTTP(cli.SubCommand):
    """Send HTTP request to a UNS host."""

    __command__ = 'http'
    __aliases__ = ['h']
    __arguments__ = [
        cli.Mutex(
            noresolve_arg(),
            forceresolve_arg(),
        ),
        timeout_arg(),
        cli.Argument(
            '-H', '--header',
            action='append',
            default=[],
            help='Extra  header to include in the request when sending HTTP '
                 'to a server.'
        ),
        cli.Argument(
            '-i', '--include-headers',
            action='store_true',
            help='Include protocol response headers in the output.'
        ),
        cli.Argument(
            '-b', '--binary-output',
            action='store_true',
            help='Treat response as binary.'
        ),
        port_arg(),
        cli.Argument('verb'),
        cli.Argument('url'),
        cli.Argument(
            'fields',
            metavar='[?|@]NAME=VALUE',
            default=[],
            nargs='*',
            help='You may use ":" prefix to send a file as request body '
                 'for example: "uns http put hostname :path/to/file".'
        )
    ]

    def __call__(self, args):
        query = []
        fields = []
        files = []
        body = ''
        path_ = ''
        port = ''

        urlparts = args.url.split('/')
        addr, _ = resolve(
            urlparts[0],
            args.timeout,
            args.forceresolve,
            args.noresolve,
            args.dbfile
        )
        if len(urlparts) > 1:
            path_ = '/' + '/'.join(urlparts[1:])

        if args.port:
            port = f':{args.port}'

        # Fields
        for f in args.fields:
            if '=' not in f:
                body = ' '.join(args.fields)
                break

            k, v = f.split('=')
            if k[0] == '?':
                query.append((k[1:], v))
            elif k[0] == '@':
                files.append((k[1:], open(v, mode='rb')))
            else:
                fields.append((k, v))

        # Open body if it's a filename
        if body and body.startswith(':'):
            body = open(body[1:], mode='rb')

        # Request headers
        reqheaders = {
            k.strip(): v.strip() for k, v in
            [h.split(':') for h in args.header]
        }
        resp = http.request(
            args.verb.upper(),
            f'http://{addr}{port}{path_}',
            query=query,
            form=body if body else fields,
            files=files,
            headers=reqheaders
        )

        # Response headers
        respheaders = [
            f'{resp.status_code} {resp.reason} '
            f'HTTP/{resp.raw.version / 10:.1f}'
        ]
        respheaders += [f'{k}: {v}' for k, v in resp.headers.items()]
        if args.include_headers:
            output('\n'.join(respheaders), end='\n\n')

        # Handle exception
        if resp.status_code >= 400:
            error(resp.text, end='')
            return 1

        # Binary output
        binary = False
        if args.binary_output:
            binary = True

        elif resp.headers.get('content-type'):
            for c in BINARY_CONTENTTYPES:
                if c.match(resp.headers['content-type']):
                    binary = True
                    break

        if binary:
            stdout.buffer.write(resp.content)
        else:
            output(resp.text, end='')

class UNS(cli.Root):
    """UNS root command line handler."""

    __completion__ = True
    __help__ = 'UNS utility.'
    __arguments__ = [
        cli.Argument('-v', '--version', action='store_true'),
        cli.Argument(
            '--dbfile',
            default=DEFAULT_DBFILE,
            help=f'Database file, default: {DEFAULT_DBFILE}'
        ),
        Resolve,
        Answer,
        Sniff,
        Find,
        HTTP,
    ]

    def main(self, *a, **k):
        try:
            return super().main(*a, **k)
        except socket.timeout:
            error('Timeout reached.')
            return 2

        except KeyboardInterrupt:
            error('Terminated by user.')
            return 3

        except cache.InvalidDBFileError as ex:
            error(f'Invalid input file: {ex}')
            return 4

        except cache.HostNotFoundError as ex:
            error(f'Cannot find: {ex}.')
            return 5

    def __call__(self, args):
        if args.version:
            import uns
            output(uns.__version__)
            return

        self._parser.print_help(file=stdout)


"""
Un palíndromo, también llamado palíndroma o palindroma, es una palabra o frase que se lee
igual en un sentido que en otro (por ejemplo; Ana, Anna, Otto). Si se trata de números en
lugar de letras, se llama capicúa.
Juanito, que trabaja en bienestar, necesita saber si una frase-palabra es Palíndroma, o un
número es capicúa.
Dado una cadena Input, el programa debe imprimir si es PALINDROMO o no.
"""

def is_palindrome(string):
    invert_string = string[::-1]
    return string == invert_string

def main():
    input_string = input("Ingresa una palabra:")

    while not input_string.strip():
        print("No has ingresado nada, por favor ingresa una palabra")
        input_string = input("Ingresa una palabra:")

    input_string = input_string.lower().strip()
    if is_palindrome(input_string):
        print(input_string + " Es palindroma")
    else:
        print(input_string + " No es palindroma")

main()


from abc import ABCMeta, abstractmethod

from bs4 import BeautifulSoup

class BaseScraper(metaclass=ABCMeta):
    @abstractmethod
    def __init__(self):
        pass

    @abstractmethod
    def scrape(self):
        raise NotImplementedError

class Kolouri(BaseScraper):
    def __init__(self, page):
        self.page = page
        self.urls = []
        super().__init__()

    def scrape(self):
        if self.urls:
            self.urls.clear()

        soup = BeautifulSoup(self.page, "html.parser")
        for ul in soup.find_all('ul', class_='latest-news'):
            for li in ul.find_all('li'):
                a = li.find('a')
                self.urls.append(a['href'])

        return self.urls


""" try except """
a = 1
b = 0

# Try except
try:
    print(f"Podíl dvou čísel {a} / {b} = {a / b}")

except ZeroDivisionError:
    print("Neděl nulou!")
    raise

except TypeError:
    print("Zkoušíš dělit řetězce, jo?")

print("A toto je konec.")


from header import *

def install_dependencies():
    option = input("Install all dependencies with pip3? (y/n)")
    if(option=="y"):
        os.system('sudo apt update')
        os.system('sudo apt install python3-pip')
        os.system('pip3 install numpy')
        os.system('pip3 install pandas')
        os.system('pip3 install matplotlib')
        os.system('pip3 install librosa')
        os.system('pip3 install essentia')
        os.system('pip3 install ffmpeg')
        os.system('pip3 install opencv-python')
        os.system('pip3 install torch')
        os.system('pip3 install torchvision')
        os.system('pip3 install importlib')
        os.system('pip3 install dijkstra')
        os.system('pip3 install dtw')
    else:
        print("Install aborted!")

def wait(message="Press enter!"):
    input(message)

def clear_pycache():
    os.system('find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf')

def point_distance(x1,y1,x2,y2):
    return math.sqrt((x2-x1)**2+(y2-y1)**2)

def time2minsec(value):
    minutes = int(value/60)
    seconds = value%60
    str1 = "0" if(minutes<10) else ""
    str2 = "0" if(seconds<10) else ""
    strf = str1+str(minutes)+":"+str2+str(seconds)
    return strf

def minsec2time(value):
    strf = int(value[0:2])*60 + int(value[3:5])
    return strf

def max_position(vect):
    max_pos = 0
    for i in range(len(vect)):
        if(vect[i]>vect[max_pos]):
            max_pos = i
    return max_pos

def save_log(log_str,log_filename):
    print(log_str)
    try:
        with open(log_filename,'a') as f:
            f.write("\n")
            f.write(log_str)
    except:
        print("Cannot save log!")

def show_progress(message,count,amount):
    amount = max(1,amount-1)
    percent = int(100*count/amount)
    interval = max(1,int(amount/100))
    if(percent>98):
        interval = 1
    if(count%interval==0):
        if(count!=0):
            sys.stdout.write("\033[F")
        print(message+str(min(count,amount))+"/"+str(amount)+" ("+str(percent)+"%)"+"                ")

def git_clean():
    os.system('git checkout --orphan newBranch')
    os.system('git add -A')
    os.system('git commit -am "Initial commit"')
    os.system('git branch -D master')
    os.system('git branch -m master')
    os.system('git push -f origin master')

def git_push(message):
    os.system('find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf')
    os.system('git add -A')
    os.system('git commit -a -m "'+str(message)+'"')
    os.system('git push')

def git_pull():
    os.system('git pull')

def gotoxy(x,y):
    print ("%c[%d;%df" % (0x1B, y, x), end='')

def count_wave_parts():
    while(True):
        count=0
        for root, dirs, files in os.walk("/srv/storage/datasets/Diognei/Audio/DEAM/songs/"):
            for file in files:
                if file.endswith(".wav"):
                    count+=1
        os.system("clear")
        print(count)
        time.sleep(3)

def check_audio_samples(audio_list):
    df = pd.read_csv(audio_list)
    for i in range(df.shape[0]):
        print(str(i)+"/"+str(df.shape[0]))
        filename = df.iloc[i,0]
        j=-1
        while(filename[j]!='-'):
            j-=1
        filename = filename[:j-4]+"/"+filename[j+1:]+".csv"
        df_feat = pd.read_csv(filename)
        features = df_feat.iloc[:,0]
        if(len(features)!=48):
            print(filename)
            utils.wait()

def str2bool(s):
    b = True if(s in ["true","True","1","y","Y"]) else False
    return b

def copy_file_to_temp_dir(filename):
    i=-1
    while(filename[i]!="/"):
        i-=1
    filename_cp = cache_dir+filename[i+1:]
    shutil.copyfile(filename,filename_cp)
    return filename_cp

def copy_file_to_out_dir(filename):
    i=-1
    while(filename[i]!="/"):
        i-=1
    filename_cp = out_dir+filename[i+1:]
    shutil.copyfile(filename,filename_cp)
    return filename_cp

def copy_file_to_any_dir(filename,dir):
    i=-1
    while(filename[i]!="/"):
        i-=1
    filename_cp = dir+filename[i+1:]
    shutil.copyfile(filename,filename_cp)
    return filename_cp

def save_model_as_main(version_name):

    model_dir = models_dir+"/"+version_name+"/"

    if(os.path.exists(model_dir)):

        config_file = model_dir+"config.json"
        config_dict = json.load(open(config_file))

        model_name = config_dict["model_name"]
        dataset_name = config_dict["dataset_name"]
        labels_suffix = config_dict["labels_suffix"]

        labels_suffix = "_"+labels_suffix[0]

        if(dataset_name=="DEAM"):
            data_type = "audio"
        else:
            data_type = "video"

        in_fnam = models_dir+version_name+"/model.pth"
        out_dir = saved_models_dir+data_type+"_main/"
        out_fnam = out_dir+model_name+labels_suffix+".pth"

        opt = "y"
        if(os.path.exists(out_fnam)):
            opt = input("Overwrite file "+str(out_fnam)+" ? (y/n) ")

        if(opt=="y"):
            shutil.copyfile(in_fnam,out_fnam)
            print("Copied "+in_fnam+" to "+out_fnam)
    else:
        print("Model dir not found!")
        return

def save_matrix_with_labels(matrix,x_labels,y_labels,filename):

    import matplotlib
    import matplotlib.pyplot as plt

    fig, ax = plt.subplots()
    im = ax.imshow(matrix)

    ax.set_xticks(np.arange(len(x_labels)))
    ax.set_yticks(np.arange(len(y_labels)))
    ax.set_xticklabels(x_labels)
    ax.set_yticklabels(y_labels)

    plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
            rotation_mode="anchor")

    for i in range(len(y_labels)):
        for j in range(len(x_labels)):
            text = ax.text(j, i, matrix[i, j],
                        ha="center", va="center", color="w")

    fig.tight_layout()
    plt.show()
    plt.savefig(filename)

def plot_matrix(mat,show_value=False):
    fig = plt.figure(figsize=(16, 8))
    ax1 = fig.add_subplot(1,1,1)
    ax1.matshow(mat,cmap=plt.cm.Blues)
    ax1.set_xticks([],[])
    ax1.set_yticks([],[])
    if(show_value==True):
        for i in range(mat.shape[0]):
            for j in range(mat.shape[1]):
                ax1.text(j, i, str(int(mat[i,j])), va='center', ha='center')
    plt.show()

def path2fnam(fnam_path):
    i=-1
    while(fnam_path[i-1]!="/"):
        i-=1
    fnam_only = fnam_path[i:]
    return fnam_only

def get_ljobs(ini,size,njobs):
    ljobs = []
    njobs = min(njobs,size)
    div=max(1,(size-ini)/njobs)
    i1=ini
    i2=i1+div

    for k in range(njobs):
        if(k==njobs-1):
            i2=size
        ljobs.append([int(i1),int(i2),k+1])
        i1+=div
        i2+=div

    for i in range(len(ljobs)):
        ljobs[i].append(k+1)

    return ljobs

def convert_videos():
    videos_dir = "/srv/storage/datasets/Diognei/Video/MSHP/"
    vcache_dir = "/srv/storage/datasets/Diognei/Cache/video/"
    output_dir = "/srv/storage/datasets/Diognei/Cache/temp/"
    file_list = []
    for root, dirs, files in os.walk(videos_dir):
        for file in files:
            if file.endswith(".mp4"):
                vc_fnam = vcache_dir+file[:-4]+"/frames/"
                file_list.append(vc_fnam)

    for k in range(len(file_list)):

        vidf_dir = file_list[k]

        print("Current dir: "+vidf_dir+" ("+str(k+1)+"/"+str(len(file_list))+") ")

        i = -9
        while(vidf_dir[i]!="/"):
            i-=1

        avi_filename = output_dir+vidf_dir[i+1:-8]+".avi"
        mp4_filename = output_dir+vidf_dir[i+1:-8]+".mp4"

        if not(os.path.exists(mp4_filename)):
            images = []
            i = 0
            while(True):
                fnam = vidf_dir+str(i)+".jpg"
                if(os.path.exists(fnam)):
                    images.append(fnam)
                else:
                    break
                i+=1

            video = cv2.VideoWriter(avi_filename, 0, 30, (640,480))
            for i in range(len(images)):
                utils.show_progress("  Rendering video... ",i,len(images))
                video.write(cv2.imread(images[i]))

            cv2.destroyAllWindows()
            video.release()

            os.system("ffmpeg -i "+str(avi_filename)+" -strict -2 "+str(mp4_filename))

            os.remove(avi_filename)
        else:
            print("Output file already exists")

    print("All Done!")

def plt2array(fig):
    fig.canvas.draw()
    img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
    img = img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    return img

def compress_video(filename):
    filename_c = filename[:-4] +"_cmp.mp4"
    if(os.path.exists(filename_c)):
        os.remove(filename_c)
    os.system("ffmpeg -i " + filename +" -b 500k " + filename_c)
    return filename_c


import argparse
import csv
import pathlib
from typing import cast
from typing import Iterator
from typing import NamedTuple
import urllib.parse

import importlib_resources

class _MovieTorrentEntry(NamedTuple):
    imdb: str
    infohash: str
    file_index: int

def _iter_movie_torrent_entries() -> Iterator[_MovieTorrentEntry]:
    def _iter_lines() -> Iterator[str]:
        with importlib_resources.open_text("tvaf_pdmdb_jf", "movie-torrents.csv") as fp:
            for line in fp:
                if line.startswith("#"):
                    continue
                yield line

    reader = csv.reader(_iter_lines())
    for row in reader:
        imdb, infohash, file_index = row
        yield _MovieTorrentEntry(imdb.lower(), infohash.lower(), int(file_index))

def _generate_movies(args: argparse.Namespace) -> None:
    base_path = cast(pathlib.Path, args.path)
    base_url = cast(str, args.base_url)

    for entry in _iter_movie_torrent_entries():
        name = f"[imdbid={entry.imdb}]"
        version = f"{entry.infohash}.{entry.file_index}"
        path = base_path / name / f"{name} - {version}.strm"
        relative = f"tvaf/v1/1114{entry.infohash}/i/{entry.file_index}"
        url = urllib.parse.urljoin(base_url, relative)
        path.parent.mkdir(parents=True, exist_ok=True)
        path.write_text(f"{url}\n")

def generate() -> None:
    """Generates a Jellyfin library of public domain media streamed via tvaf."""
    parser = argparse.ArgumentParser(
        description="Generates a collection of *.strm files suitable for use as a "
        "Jellyfin library location. The *.strm files will point to tvaf URLs, using "
        "tvaf to stream media from bittorrent.",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument(
        "--base_url", default="http://127.0.0.1:8000", help="base tvaf url"
    )
    parser.add_argument(
        "--type", choices=["movies"], help="library type", required=True
    )
    parser.add_argument("path", type=pathlib.Path)

    args = parser.parse_args()
    if args.type == "movies":
        _generate_movies(args)


import sys
import time
import itertools

import pandas as pd
import multiprocessing
from typing import Callable, Tuple, Union
from multiprocessing.pool import Pool as sec_pool

class NoDaemonProcess(multiprocessing.Process):
    # make 'daemon' attribute always return False
    def _get_daemon(self):
        return False

    def _set_daemon(self, value):
        pass
    daemon = property(_get_daemon, _set_daemon)

# We sub-class multiprocessing.pool.Pool instead of multiprocessing.Pool
# because the latter is only a wrapper function, not a proper class.
class MyPool(multiprocessing.pool.Pool):
    Process = NoDaemonProcess

class AsyncMP:

    """
    This class is used to compute task in parallel using multiprocessing.
    """
    DEFAULT_NCPU = multiprocessing.cpu_count() - 1

    def __init__(self, **kwargs):

        self._num_cpu = kwargs.get('num_cpu', multiprocessing.cpu_count() - 1)

    def exec(self, params: list, func: Callable[[Tuple[str, pd.DataFrame]], Union[pd.DataFrame, pd.Series]],
             logger: Callable[[str], None] = sys.stdout) -> pd.DataFrame:

        start = time.time()
        # logger.flush()
        # logger.write("\nUsing {} CPUs in parallel...\n".format(self._num_cpu))

        with multiprocessing.Pool(self._num_cpu) as pool:
            result = pool.starmap_async(func, params)
            got = result.get()
        logger.write("\nTasks completed. Processed {} df in {:.1f}s\n".format(
            len(got), time.time() - start))

        return got

    def exec_function_in_parallel(self, tab_parameter: list,
                                  func,
                                  logger: Callable[[str], None] = sys.stdout
                                  ) -> pd.DataFrame:

        start = time.time()
        logger.flush()
        logger.write("\nUsing {} CPUs in parallel...\n".format(self._num_cpu))

        with MyPool(self._num_cpu) as pool:
            result = pool.starmap(func, tab_parameter, chunksize=1)

        return result

    def exec_function_with_pool_map(self, tab_parameter: list,
                                  func,
                                  logger: Callable[[str], None] = sys.stdout
                                  ) -> pd.DataFrame:
        start = time.time()
        logger.flush()
        logger.write("\nUsing {} CPUs in parallel...\n".format(self._num_cpu))
        pool = MyPool(self._num_cpu)
        results = pool.starmap(func, tab_parameter)
        pool.close()
        pool.join()

        return results

    @staticmethod
    def exec_function_in_slavequeue(queue, func, *args):

        queue.put(func(*args))
        queue.put(None)


# -*- coding: utf-8 -*-
"""
 Created on 2021/7/10 14:42
 Filename   : ex_plot_segmentation_toy.py
 Author     : Taosy.W
 Zhihu      : https://www.zhihu.com/people/1105936347
 Github     : https://github.com/AFei19911012/PythonSamples
 Description:
"""

# =======================================================
import numpy as np
import matplotlib.pyplot as plt

from sklearn.feature_extraction import image
from sklearn.cluster import spectral_clustering

def plot_segmentation_toy():
    l = 100
    x, y = np.indices((l, l))

    center1 = (28, 24)
    center2 = (40, 50)
    center3 = (67, 58)
    center4 = (24, 70)

    radius1, radius2, radius3, radius4 = 16, 14, 15, 14

    circle1 = (x - center1[0]) ** 2 + (y - center1[1]) ** 2 < radius1 ** 2
    circle2 = (x - center2[0]) ** 2 + (y - center2[1]) ** 2 < radius2 ** 2
    circle3 = (x - center3[0]) ** 2 + (y - center3[1]) ** 2 < radius3 ** 2
    circle4 = (x - center4[0]) ** 2 + (y - center4[1]) ** 2 < radius4 ** 2

    # #############################################################################
    # 4 circles
    img = circle1 + circle2 + circle3 + circle4

    # We use a mask that limits to the foreground: the problem that we are
    # interested in here is not separating the objects from the background,
    # but separating them one from the other.
    mask = img.astype(bool)

    img = img.astype(float)
    img += 1 + 0.2 * np.random.randn(*img.shape)

    # Convert the image into a graph with the value of the gradient on the
    # edges.
    graph = image.img_to_graph(img, mask=mask)

    # Take a decreasing function of the gradient: we take it weakly
    # dependent from the gradient the segmentation is close to a voronoi
    graph.data = np.exp(-graph.data / graph.data.std())

    # Force the solver to be arpack, since amg is numerically
    # unstable on this example
    labels = spectral_clustering(graph, n_clusters=4, eigen_solver='arpack')
    label_im = np.full(mask.shape, -1.)
    label_im[mask] = labels

    plt.matshow(img)
    plt.matshow(label_im)

    # #############################################################################
    # 2 circles
    img = circle1 + circle2
    mask = img.astype(bool)
    img = img.astype(float)

    img += 1 + 0.2 * np.random.randn(*img.shape)

    graph = image.img_to_graph(img, mask=mask)
    graph.data = np.exp(-graph.data / graph.data.std())

    labels = spectral_clustering(graph, n_clusters=2, eigen_solver='arpack')
    label_im = np.full(mask.shape, -1.)
    label_im[mask] = labels

    plt.matshow(img)
    plt.matshow(label_im)

    plt.show()

if __name__ == '__main__':
    plot_segmentation_toy()


import os

audio_path = "C:/Users/sures/Desktop/output/"
fileCount = 0
fileCount = len(os.listdir(audio_path))

i = 0
f = open(audio_path + str("fileList.txt"), "w")
while i < fileCount:
    f.write(str("file '"+str(i)+str(".wav'\n")))
    i += 1
f.close()
command = "cmd /c start /min /wait cmd.exe /K  \"cd " + audio_path + " && ffmpeg -f concat -safe 0 -i fileList.txt -c copy -ar 48000 -rf64 auto input.wav && exit\""
os.system(command)

command = "cmd /c start /min /wait cmd.exe /K \"cd "+audio_path+" && ffmpeg -i input.wav -codec:a libmp3lame -qscale:a 2 FinalOutput.mp3 && exit\""
os.system(command)


from flask import render_template, request, Blueprint
from flaskpitch.models import Post
import requests

main = Blueprint('main', __name__)

@main.route("/")
@main.route("/home")
def home():

    random_quote_url = 'http://quotes.stormconsultancy.co.uk/random.json'
    quote_response = requests.get(random_quote_url)
    quote_data = quote_response.json()

    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
    return render_template('home.html', posts=posts, quote_data=quote_data)

@main.route("/about")
def about():
    return render_template('about.html', title='About')


import pymongo
import argparse
import bz2
import json
import random
from tqdm import tqdm

"""This script adds metadata information to the articles of a Wikipedia dump and uploads it into MongoDB.
Said metadata is :
    * the corresponding WikiData ID
    * its NER class according to NECKAr
    * its list of WikiData aliases if it exists
"""

parser = argparse.ArgumentParser()

parser.add_argument("wikipedia_dump", help="Path to the Wikipedia dump to add the metadata to.")

args = parser.parse_args()

if __name__ == "__main__":
    client = pymongo.MongoClient()

    mappings_db = client.mappings
    title_to_id_coll = mappings_db.title_to_id

    wikidata_db = client.wikidata
    wikidata_items_coll = wikidata_db.wikidata_items

    neckar_db = client.neckar_results
    neckar_results_coll = neckar_db.neckar_results

    wikipedia_db = client.wikipedia
    dump_coll = wikipedia_db.dump_with_metadata
    dump_coll.delete_many({})

    wikipedia_dump = args.wikipedia_dump

    print("Counting number of articles in the Wikipedia dump...")

    articles_counter = 0
    with bz2.open(wikipedia_dump, "r") as dump_file:
        for article in dump_file:
            articles_counter += 1

    print("Processing...")
    progress_bar = tqdm(total=articles_counter, unit="items", leave=None, mininterval=0, miniters=0)

    with bz2.open(wikipedia_dump, "r") as dump_file:
        for article in dump_file:
            article_info = json.loads(article.decode("utf-8"))

            try:
                wikidata_id = title_to_id_coll.find_one({"title": article_info["title"]})["wikidata_id"]
            except TypeError:
                progress_bar.update(1)
                continue

            try:
                aliases = [alias["value"] for alias in wikidata_items_coll.find_one(
                    {"id": wikidata_id})["aliases"]["en"]]
            except KeyError:
                aliases = []

            try:
                ner_class = random.choice([result["neClass"] for result in neckar_results_coll.find(
                    {"id": wikidata_id})])
            except IndexError:
                ner_class = "MISC"

            article_info["wikidata_id"] = wikidata_id
            article_info["aliases"] = aliases
            article_info["ne_class"] = ner_class

            dump_coll.insert_one(article_info)
            progress_bar.update(1)

    dump_coll.create_index("title")

    print("Program completed.")


# Given a string s, find the first non-repeating character in it and return its index.
# If it does not exist, return -1.

class Solution(object):
    def firstUniqChar(self, string):
        index = 0
        if len(string) == 0:
            print("empty string!!!")

        for i in string:
            if string.count(i) == 1:
                return string.index(i)
            else:
                index += 1
        if index == len(string) :
            return -1


# Works with Python 3.6
import random
import asyncio
import discord
from discord.ext import commands  # Used to make commands

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

PREFIX = "ban"  # Sets the prefix of the bot

LOOP = asyncio.get_event_loop()  # Creates an event loop (used to make tasks with asyncio)
channel_loop = {}  # Channel loop dictionary used to store tasks under their channel id.

bot = commands.Bot(command_prefix=PREFIX,
    description='A Discord bot to interface with the Project Euler website and provide a random set of problems.',
    case_insensitive=True)

cogs = ['cogs.basic']

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@bot.event
async def on_ready():  # Once the bot is ready:

    print("Logged in as {} - {} The bot is ready!".format(bot.user.name, bot.user.id))
    for cog in cogs:
        bot.load_extension(cog)
    game = discord.Game(name="for {}help".format(PREFIX), type=3)
    await bot.change_presence(activity=game)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@bot.event
async def on_message(message):
    """
    On every single message that the bot sees, this will trigger.
    First, it makes sure that bot doesn't reply to itself.
    Then, it makes sure that the user executing commands isn't in a DM.
    Finally, it processes commands
    """

    # Ensures that the bot doesn't reply to itself
    if message.author == bot.user:
        return

    elif not message.content.startswith("{}about".format(PREFIX)):  # If the message is not the standard about command:
        if isinstance(message.channel, discord.abc.PrivateChannel):  # If it is a private channel:
            # Tell them they can't:
            await message.channel.send("You are not allowed to call commands other than `{}about` in DMs.".format(PREFIX))
            return

    await bot.process_commands(message)  # Process commands after this is triggered (bot commands won't work without this)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@bot.event
async def on_command_error(message, exception):
    #print("Exception: {}\n\nMessage: {}".format(exception, message))
    embed = discord.Embed(title="Error from command", description=str(exception), colour=discord.Colour.red())
    print("Error with message from{}: {}\n{}".format(message.author.id, message, exception))
    await message.channel.send(embed=embed)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@bot.command(pass_context=True)
async def roulette(message, chambers=6):
    """
    ROLL THE DICE.
    """

    not_so_lucky = ["Not so lucky this time.",
                    "The regret's setting in.",
                    "Git banned, nerd.",
                    "BANNED!"]
    lucky_duck = ["Lucky Duck!",
                  "Misfire!",
                  "Die another dayðŸ˜Ž",
                  "DEAD. just kidding ur fine"]

    await message.channel.send(f"Roll the dice...")

    time.wait(3)
    banned_on_one = random.randint(chambers)
    time.wait

    target_user = message.author

    if banned_on_one == 1:
        await message.channel.send(random.choice(not_so_lucky))
        time.wait(3)
        message.author.kick(reason="Unlucky Duck")

        # Sends the user an invite back to the server
        message.bot.fetch_invite()
    else:
        await message.channel.send(random.choice(lucky_duck))

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

@bot.command(pass_context=True)
async def me(message,):
    """
    Kicks the user off the discord. What do you want from me.
    """

    await message.channel.send("You got it.")
    time.wait(2)
    message.author.kick(reason="Asked for it")

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TOKEN = ""

bot.run(TOKEN, bot=True, reconnect=True)  # Runs the bot


#!/usr/bin/python
# _*_ coding: UTF-8 _*_
'''
Created on Jun 29, 2016

@author: shixingwen
发送电子邮件模块smtplib
'''
import smtplib
import string
HOST = "smtp.163.com" #定义smtp主机
SUBJECT = "Test email from Python" #定义邮件主题
TO = "shixingwe.n@163.com" #定义邮件收件人
FROM = "shixingwe.n@163.com" #定义邮件发件人
text = "Python rules them all!" #邮件内容
BODY = string.join((  #组装sendmail 方法的邮件主题内容，各段一"\r\n" 进行分隔
                      "From: %s" % FROM,
                      "To: %s" % TO,
                      "Subject: %s" % SUBJECT,
                      "",
                      text
                    ),"\r\n")
server = smtplib.SMTP()  #创建一个SMTP（）对象
server.connect(HOST, "25") #通过connect 方法连接smtp 主机
server.starttls() #启动安全传输模式
server.login("shixingwe.n@163.com", "A19860926a") #邮箱账户登录校验
server.sendmail(FROM,[TO], BODY) #邮件发送
server.quit() #断开smtp连接


from typing import List

from volatility.framework import renderers, constants, interfaces
from volatility.framework import exceptions, contexts
from volatility.framework.automagic import linux
from volatility.framework.configuration import requirements
from volatility.framework.interfaces import plugins
from volatility.framework.objects import utility
from volatility.framework.renderers import format_hints

class Lsmod(plugins.PluginInterface):
    """Lists loaded kernel modules"""

    @classmethod
    def get_requirements(cls) -> List[interfaces.configuration.RequirementInterface]:
        return [
            requirements.TranslationLayerRequirement(
                name = 'primary', description = 'Memory layer for the kernel', architectures = ["Intel32", "Intel64"]),
            requirements.SymbolTableRequirement(name = "vmlinux", description = "Linux kernel symbols")
        ]

    @classmethod
    def list_modules(cls, context: interfaces.context.ContextInterface, layer_name: str, vmlinux_symbols: str):
        """Lists all the modules in the primary layer"""
        linux.LinuxUtilities.aslr_mask_symbol_table(context, vmlinux_symbols, layer_name)

        vmlinux = contexts.Module(context, vmlinux_symbols, layer_name, 0, absolute_symbol_addresses = True)

        modules = vmlinux.object(symbol_name = "modules").cast("list_head")

        table_name = modules.vol.type_name.split(constants.BANG)[0]

        for module in modules.to_list(table_name + constants.BANG + "module", "list"):
            yield module

    def _generator(self):
        for module in self.list_modules(self.context, self.config['primary'], self.config['vmlinux']):

            mod_size = module.get_init_size() + module.get_core_size()

            mod_name = utility.array_to_string(module.name)

            yield 0, (format_hints.Hex(module.vol.offset), mod_name, mod_size)

    def run(self):
        return renderers.TreeGrid([("Offset", format_hints.Hex), ("Name", str), ("Size", int)], self._generator())


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

# ### **METADATA CREATION AND OBJECT CONCATENATION**
# #### Script takes the output files from STARsolo and gets them ready for the downstream analysis

import sys, os
import numpy as np
import pandas as pd
pd.options.mode.chained_assignment = None
import scipy as sc
from scipy.io import mmread, mminfo, mmwrite
from scipy import sparse
from scipy.sparse import vstack
from os import listdir
from os.path import isfile, join

#setting working directory
align_results_dir = './Results/STARsolo_Output/'

# #creation of metadata containing cell number, barcode, stage and sample
# stages = next(os.walk(align_results_dir))[1]
# for stage in stages:
#     samples = next(os.walk(align_results_dir + stage + '/'))[1]
#     for sample in samples:
#         lanes = next(os.walk(align_results_dir + stage + '/' + sample + '/'))[1]
#         for lane in lanes:
#             batches = next(os.walk(align_results_dir + stage + '/' + sample + '/' + lane + '/'))[1]
#             batches = [k for k in batches if 'Solo.out' in k]
#             for batch in batches:
#                 features = next(os.walk(align_results_dir + stage + '/' + sample + '/' + lane + '/' + batch))[1]
#                 for feature in features:
#                     if os.path.isfile(align_results_dir + stage + '/' + sample + '/' + lane + '/' + batch + '/' + feature + '/raw/barcodes.tsv'):
#                         bc = pd.read_csv(align_results_dir + stage + '/' + sample + '/' + lane + '/' + batch + '/' + feature + '/raw/barcodes.tsv', header=None)
#                         bc.rename(columns={0: 'barcode'}, inplace=True)
#                         df = bc.drop_duplicates(subset=['barcode'], inplace=False)
#                         df['cell'] = 'cell_' + df.reset_index().index.astype(str)
#                         df['sample'] = sample
#                         df['stage'] = stage
#                         df['stage'] = df['stage'].str.replace('_', '')
#                         df = pd.DataFrame(df)
#                         df.to_csv(align_results_dir + stage + '/' + sample + '/' + lane + '/' + batch + '/' + feature + '/raw/meta.tab', sep="\t", index=False)
#                     else:
#                         print(sample + '_' + lane + ' not found')
#                         break

# # deleting Gene Expression column from features.tsv and metadata concatenation of all samples
# # just on Genefull, as Velocyto has 3 different matrices
# genes = []
# metadata = []
# features = ['GeneFull']
# for stage in stages:
#     samples = next(os.walk(align_results_dir + stage + '/'))[1]
#     for sample in samples:
#         lanes = next(os.walk(align_results_dir + stage + '/' + sample + '/'))[1]
#         for lane in lanes:
#             batches = next(os.walk(align_results_dir + stage + '/' + sample + '/' + lane + '/'))[1]
#             batches = [k for k in batches if 'Solo.out' in k]
#             for batch in batches:
#                 for feature in features:
#                     if os.path.isfile(align_results_dir + stage + '/' + sample + '/' + lane + '/' + batch + '/' + feature + '/raw/barcodes.tsv'):
#                         #Load the data
#                         gene = pd.read_csv(align_results_dir + stage + '/' + sample + '/' + lane + '/' + batch + '/' + feature + '/raw/features.tsv',sep="\t",skiprows=0, header=None)
#                         meta = pd.read_csv(align_results_dir + stage + '/' + sample + '/' + lane + '/' + batch + '/' + feature + '/raw/meta.tab',sep="\t",skiprows=0)
#                         gene = gene.drop(gene.columns[2], axis=1)
#                         metadata.append(meta)
#                     else:
#                         print(sample + '_' + lane + ' file not found')

# df_metadata = pd.concat(metadata, ignore_index=True)

# gene.to_csv(align_results_dir + 'features.tsv', sep='\t', index=False)
# df_metadata.to_csv(align_results_dir + 'metadata_whole.tab', sep='\t', index=False)
# print('Gene Features and Metadata file saved')

# creation of whole matrix object concatenation all samples (high-resource process)
whole_matrix = []
features = ['GeneFull']
stages = next(os.walk(align_results_dir))[1]
for stage in stages:
    samples = next(os.walk(align_results_dir + stage + '/'))[1]
    for sample in samples:
        lanes = next(os.walk(align_results_dir + stage + '/' + sample + '/'))[1]
        for lane in lanes:
            batches = next(os.walk(align_results_dir + stage + '/' + sample + '/' + lane + '/'))[1]
            batches = [k for k in batches if 'Solo.out' in k]
            for batch in batches:
                for feature in features:
                    if os.path.isfile(align_results_dir + stage + '/' + sample + '/' + lane + '/' + batch + '/' + feature + '/raw/barcodes.tsv'):
                        #Load the data
                        mat = mmread(align_results_dir + stage + '/' + sample + '/' + lane + '/' + batch + '/' + feature + '/' + 'raw/matrix.mtx').tocsr().transpose()
                        whole_matrix.append(mat)
                        break
                    else:
                        print(sample + '_' + lane + ' file not found')

whole_matrix = sc.sparse.vstack((whole_matrix))
mmwrite(align_results_dir + 'matrix_whole', whole_matrix)

print('Whole Matrix file saved')


import numpy as np
from matplotlib import pyplot as plt
from labvaja1.vaja01 import loadImage, displayImage

## 1. NALOGA:
if __name__ == '__main__':
    I = loadImage('labvaja3\data\pumpkin-200x152-08bit.raw', [200,152], np.uint8)
    displayImage(I,'originalna slika')

## 2. NALOGA:
def interpolateImage(iImage, iSize, iOrder):
    iOrder = int(iOrder)

    # iImage - interpolacijska slika
    # oImage - interpolirana slika
    # iSize - vektor interpolirane slike

    oImage = np.zeros([iSize[1], iSize[0]])

    # korak interpolacije:  (dx, dy)
    step = [
        (iImage.shape[1]-1)/(iSize[0]-1),
        (iImage.shape[0]-1)/(iSize[1]-1)
    ]

    # interpolacija slike
    for y in range(iSize[1]):
        for x in range(iSize[0]):
            s = 0 # s je inteziteta

            # točka v koordinatnem sistemu vhodne slike
            pt = np.array([x, y])*np.array(step)

            if iOrder == 0:
                px = np.round(pt).astype(np.int64)
                s = iImage[px[1], px[0]]
            elif iOrder == 1:
                px = np.floor(pt).astype(np.int64)
                # oblika px: [xi, yi]

                a = abs((px[0]+1) - pt[0])*abs((px[1]+1) - pt[1]) # delimo z 1
                b = abs((px[0]+0) - pt[0])*abs((px[1]+1) - pt[1]) # delimo z 1
                c = abs((px[0]+1) - pt[0])*abs((px[1]+0) - pt[1]) # delimo z 1
                d = abs((px[0]+0) - pt[0])*abs((px[1]+0) - pt[1]) # delimo z 1

                # sa = sivinska vrednost povezana z a
                sa = iImage[px[1]+0, px[0]+0]
                sb = iImage[px[1]+0, min(px[0]+1, iImage.shape[1]-1)]
                sc = iImage[min(px[1]+1, iImage.shape[0]-1), px[0]+0]
                sd = iImage[min(px[1]+1, iImage.shape[0]-1), min(px[0]+1, iImage.shape[1]-1)]
                # z min funkcijo zagotovimo, da indeksi ne presežejo slike

                s = np.floor(sa*a + sb*b + sc*c + sd*d)

            oImage[y, x] = s
    return oImage

if __name__ == '__main__':
    intSize = [I.shape[1]*2, I.shape[0]*2]
    # rob je stopničast
    I0 = interpolateImage(I, intSize, 0)
    displayImage(I0, 'Interpolirana slika (red 0)')

    # rob je zglajen
    I1 = interpolateImage(I, intSize, 1)
    displayImage(I1, 'Interpolirana slika (red 1)')


import pandas as pd
import numpy as np
import scipy.sparse as sp
from scipy.sparse import csc_matrix
import sklearn.preprocessing as pp
from collections import OrderedDict

"""
dataroot = "../../data/"
def cosine_similarities(sp_mat):
    col_normed_mat = pp.normalize(sp_mat, axis=0)
    sim = col_normed_mat.T * col_normed_mat

    return sim.todense()

data  = pd.read_csv("../../data/KISA_TBC_VIEWS_UNIQ.csv")

user = data['USER_ID'].values
movie = data['MOVIE_ID'].values

target = [1] * len(user)
data = csc_matrix((target,(user, movie)))
print("csc matrix built")

sp.save_npz(dataroot+"csc_mat_for_cos.npz",data)
print("mat saved!")

cos = cosine_similarities(data)
np.save(dataroot+"cosine_sim.npy",cos)
"""

# # score(i, x) = (itemsSimilarity(x, record(i, 1)) + … + itemsSimilarity(x, record(i, M(i)))) /
# # (itemsSimilarity(x, item(1)) + … + itemsSimilarity(x, item(K)))
# # (단, 식 표현 편의를 위해 itemsSimilarity(x, x) = 0 으로 가정한 식입니다.)
cos = np.load("../../data/cosine_sim.npy")
# np.fill_diagonal(cos, 0)
# test = pd.read_csv("../../data/KISA_TBC_VIEWS_UNIQ_QUESTION.csv")
# print(test)
# #divisor = np.sum(cos[:,:],axis=1)
#
def calculate_sim(row):
    global cos
    user_id = row['USER_ID'].values[0]
    items = row['MOVIE_ID'].values

    out = np.copy(cos[items,:])
    out[:,items] = 0
    out[np.where(out <= 0.9)] = 0 # 확률이 0.9보다 큰 애들은 남겨두고 나머지 0으로 하기

    maxid = np.argmax(out,axis=1).astype(int) # max id, shape: (len(items),)
    print(len(maxid),len(items))
    assert len(maxid) == len(items)

    unorderdict = {}

    for i, watchedmax in enumerate(maxid):
        unorderdict[watchedmax] = out[i,watchedmax]

    outdict = OrderedDict(sorted(unorderdict.items(),key=lambda x:x[1]))

    out = list(set(out))
    print(out)
    #out = sorted(range(len(out)), key=lambda i: out[i],reverse=True)

    #out = np.sum(cos[:,items], axis=1)
    print(user_id,out)
    return out  #sorted(range(len(out)), key=lambda i: out[i], reverse=True)[:50]

grouped = test.groupby(by='USER_ID').apply(calculate_sim).tolist()
out = pd.DataFrame(grouped,dtype=np.uint32).fillna(9000).astype(np.uint32)
out.to_csv("../../data/similarity_score.csv")

a  = np.array([[1,2],[0,4]])
a[np.where(a>1)] = 5
print(a)


#!/usr/bin/env python3

from wsgiref.simple_server import make_server

import falcon

class ThingsResource:
    def on_get(self, req, resp):
        """Handles GET requests"""
        resp.status = falcon.HTTP_200
        resp.content_type = falcon.MEDIA_TEXT  # Default is JSON, so override
        resp.text = ('\nTwo things awe me most, the starry sky above me and the moral law within me.\n'
                     '    ~ Immanuel Kant\n')

# A CORS policy is required for <umarcor.github.io/hwstudio> to work with a backend served at <127.0.0.1>.
# - https://github.com/godotengine/godot/pull/40542
# - https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
# - https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowOrigin
# Enable a simple CORS policy for all responses.
# - https://falcon.readthedocs.io/en/3.0.1/api/cors.html
app = falcon.App(cors_enable=True)

app.add_route('/things', ThingsResource())

if __name__ == '__main__':
    with make_server('', 8000, app) as httpd:
        print('Serving on port 8000...')
        httpd.serve_forever()


from django.urls import path

from authors.apps.articles import views

app_name = "articles"

urlpatterns = [
    path('articles/', views.ArticleViewSet.as_view(
        {'get': 'list', "post": "create"}), name='articles-all'),
    path('articles/<pk>/', views.ArticleViewSet.as_view(
        {"get": "retrieve", "put": "update", "patch": "partial_update",
         "delete": "destroy"}), name='single-article'),
    path('rate/<slug>/', views.RatingAPIView.as_view(), name='rating'),
    path('articles/<pk>/like/', views.LikeViewSet.as_view(
        {"patch": "partial_update"}), name='like_article'),
    path('articles/<pk>/dislike/', views.DisLikeViewSet.as_view(
        {"patch": "partial_update"}), name='dislike_article'),
    path('articles/<pk>/comments/', views.CommentViewSet.as_view(
        {'get': 'list', "post": "create"}), name='comments-all'),
    path('articles/<pk>/comments/<id>/', views.CommentViewSet.as_view(
        {'get': 'retrieve', "put": "update",
         "delete": "destroy", "post": "create_reply"}), name='single-comment'),
    path('favorites/', views.FavoriteViewSet.as_view({"get": "list"}),
         name='favorites'),
    path('articles/<slug>/favorite',
         views.FavoriteViewSet.as_view({
             "put": "update", "delete": "destroy"}), name="favorite_article"),
]


# Create a single t2.micro instance using boto3 SDK. This uses Amazon Linux AMI 2016.03.1.
#
#

import boto3

ec2 = boto3.resource('ec2')

instance = ec2.create_instances(

    DryRun=False,
    ImageId='ami-f5f41398',
    MinCount=1,
    MaxCount=1,
    KeyName='test_key_pair', # Use your configured_key_pair
    SecurityGroupIds=[ 'sg-7fbf7e04'], # use your configured security group's Id.
    UserData='yum install httpd; chkconfig httpd on;/etc/init.d/httpd start',  # This will install httpd and start the httpd process
    InstanceType='t2.micro',
    BlockDeviceMappings=[
        {
	    'DeviceName': '/dev/xvda',
            'Ebs': {
                'SnapshotId': 'snap-a9b8c94e',
                'VolumeSize': 8,
                'DeleteOnTermination': True,
                'VolumeType': 'gp2'

            }

        }
    ],

    InstanceInitiatedShutdownBehavior='stop',
	IamInstanceProfile={
        'Arn': 'arn:aws:iam::590487556930:instance-profile/S3_Access'
    }

)