import os
from random import randint
from time import time
import subprocess
from datetime import datetime
from shutil import rmtree
from db.models import UserRegisteredContest,Contest,ContestProblem,Problem,Submission,Admin,User
from bson.objectid import ObjectId
from platform import system
from flask import current_app

ORIGINAL_DIR=os.getcwd()

if system()=='Linux':
    py_dir="/usr/local/bin/python"
    go_dir="/usr/bin/go"
    cplus_dir="/usr/bin/g++"
    c_dir="/usr/bin/gcc"
    import resource

compilers={
    "go":go_dir,
    "py":py_dir,
    "java":"/usr/bin/java",
    "c":c_dir,
    "cpp":cplus_dir,
    "python":py_dir,
    "python2":"/usr/bin/python2",
    "php":"/usr/bin/php",
    "js":"/usr/bin/node"
}

def runCommand(*popenargs,input=None, capture_output=False, timeout=None, check=False,memorylimit=300,**kwargs):
    #mocking the standard implementation of subprocess.run
    #so we can set memory limit
    lang=kwargs.get("lang")
    if lang:kwargs.pop("lang")
    if input is not None:
        if kwargs.get('stdin') is not None:
            raise ValueError('stdin and input arguments may not both be used.')
        kwargs['stdin'] = subprocess.PIPE

    if capture_output:
        if kwargs.get('stdout') is not None or kwargs.get('stderr') is not None:
            raise ValueError('stdout and stderr arguments may not be used '
                             'with capture_output.')
        kwargs['stdout'] = subprocess.PIPE
        kwargs['stderr'] = subprocess.PIPE

    with subprocess.Popen(*popenargs, **kwargs) as process:
        if system()=='Linux':
            memorylimit=memorylimit+128 #extra 128 mb for startup
            memorylimithard=memorylimit*1024**2+10024

            if lang!="js":
                #js is single threaded that means we would be setting stack limit
                #for the entire js interpreter since js relies on callback functions
                #for it operations which essentially uses stack
                resource.prlimit(process.pid,resource.RLIMIT_DATA,(memorylimit*1024**2,memorylimithard))
            resource.prlimit(process.pid,resource.RLIMIT_STACK,(memorylimit*1024**2,memorylimithard))

        try:
            stdout, stderr = process.communicate(input, timeout=timeout)
        except subprocess.TimeoutExpired as exc:
            process.kill()
            if subprocess._mswindows:
                # Windows accumulates the output in a single blocking
                # read() call run on child threads, with the timeout
                # being done in a join() on those threads.  communicate()
                # _after_ kill() is required to collect that and add it
                # to the exception.
                exc.stdout, exc.stderr = process.communicate()
            else:
                # POSIX _communicate already populated the output so
                # far into the TimeoutExpired exception.
                process.wait()
            raise
        except:  # Including KeyboardInterrupt, communicate handled that.
            process.kill()
            # We don't call process.wait() as .__exit__ does that for us.
            raise
        retcode = process.poll()
        if check and retcode:
            raise subprocess.CalledProcessError(retcode, process.args,
                                     output=stdout, stderr=stderr)
    return subprocess.CompletedProcess(process.args, retcode, stdout, stderr)

class Task:
    """
    Handles running of submitted code and updates the submission details in the database
    """

    @classmethod
    def taskObj(cls,d):
        if isinstance(d,dict):
            obj=object()
            for classvar in d:
                setattr(obj,classvar,d[classvar])
            setattr(obj,"run",)
            return obj

    PossibelTasksState=["initialize","running","finished"]
    def __init__(self,lang,content,userid,problem,id,stype,codefile,contestid=None,ctype=None):
        """
        :param stype: the type of submission. differentiates actual
                      test cases from sample cases.
        :param content: code content
        :param problem: an Instance of :class: `ProblemInstance`.
        """
        self.state=Task.PossibelTasksState[0]
        self.lang=lang
        self.stype = stype
        self.codefile = codefile
        if self.codefile:
            self.content = self.codefile.read().decode("utf-8")
        else:
            self.content = content # get submitted code content
        self.userid=userid
        user=User().getBy(_id=ObjectId(userid)) or Admin().getBy(_id=ObjectId(userid))
        self.username = user.get("username")
        self.id=id
        self.state=Task.PossibelTasksState[0]
        self.verdict = "Passed"
        self.contestid = contestid
        self.ctype = ctype
        self.problem = problem

        if self.stype == "test":
            self.cases=self.problem.getTestCases().split(",")
            self.answercase=self.problem.getAnswerForTestCases().split(",")
            self.result=[None]*int(self.problem.getSizeOfTestCases())
        elif self.stype == "sample":
            self.cases=self.problem.getSampleCases().split(",")
            self.answercase=self.problem.getAnswerForSampleCases().split(",")
            self.result=[None]*int(self.problem.getSizeOfSampleCases())

        self.cases=[i.strip() for i in self.cases]
        self.answercase=[i.strip() for i in self.answercase]

        self.timelimit=problem.getTimeLimit()
        self.memlimit=problem.getMemLimit()
        self.enter()

    def toJson(self):
        # Don't show expected output for contest submission
        if self.contestid:
            for each in self.result:
                if each:
                    each.pop('expectedoutput', None)
        return {"state":self.state,"lang":self.lang,"_id":self.id,"result":self.result}

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

    def __repr__(self):
        return str(self.__dict__)

    def free(self):
        #del self.contestid,self.ctype,self.problem,self.stype
        #del self.cases,self.answercase,self.timelimit,self.memlimit
        pass

    def __del___(self):
        #cleaning up
        try:
            os.remove(self.filepath)
            if self.lang.lower()=="java":
                rmtree(self.folder,ignore_errors=True)
        except FileNotFoundError:
            pass

    def __lt__(self,other):
        return ~self.PossibelTasksState.index(self.state)< ~other.PossibelTasksState.index(other.state)

    def enter(self):
        self.filename=self.randomFilename()
        if self.lang.lower()=="java":
            filename=os.path.join(*self.filename.split(".")[:-1])
            self.folder="/tmp/{}/{}/".format(self.lang,filename)
            os.makedirs(self.folder,exist_ok=True)
        else:
            self.folder="/tmp/{}/".format(self.lang)
        self.filepath=self.folder+self.filename
        with open(self.filepath,"w+") as f:
            f.write(self.content)
        self.timeofsubmission=str(datetime.now())

    def resolveFolder(self,lang):
        #python is py,java is java e.t.c.This function exist if need be resolve the name later

        if lang not in compilers:
            raise NotImplementedError("Not yet supported")
        if lang.lower()=="go":
            return [compilers[lang],"run"]
        return [compilers[lang]]

    def randomFilename(self):
        return self.userid+"{}{}.{}".format(hash(time()),hash(self),self.lang)

    def status(self):
        return self.state

    def formatRunOutput(self,string):
        if not string:return string
        #starting by replacing all file name with a generic name
        return string.replace(self.filepath,"submited."+self.lang)

    def runPerCase(self,n_cases,binargs,**kwargs):
        for cc in range(n_cases):
            errput = None
            ans = None
            try:
                ans=runCommand(binargs,input=self.cases[cc],**kwargs)
            except subprocess.TimeoutExpired:
                self.result[cc] ={"passed":False,
                                    "output":"Time Limit Exceeded",
                                    "errput":"",
                                    "expectedoutput":self.answercase[cc]
                                    }
                self.verdict = "Failed"
                errput = "TimeOut"
                break
            except MemoryError:
                self.result[cc] ={"passed":False,
                                    "output":"Memory Limit Exceeded",
                                    "errput":"",
                                    "expectedoutput":self.answercase[cc]
                                    }
                self.verdict = "Failed"
                errput = "OutOFMemory"
                break
            except Exception as e:
                errput="RuntimeError"
                if str(type(e))=="<class 'subprocess.TimeoutExpired'>":
                    errput="TimeOut"

            if ans:
                output=ans.stdout.strip()
                errput=errput or ans.stderr.strip()
                if not output and not errput:
                    errput="No result from Interpreter"
                if ans.returncode > 0:
                    errput="RuntimeError"

                self.result[cc] = {
                                    "passed":output==self.answercase[cc] and ans.returncode==0,
                                    "output":self.formatRunOutput(output),
                                    "errput":self.formatRunOutput(errput),
                                    "expectedoutput":self.answercase[cc]
                                    }
                if not self.answercase[cc] or ans.returncode>0 or output!=self.answercase[cc]:
                    self.verdict = "Failed"
            else:
                self.result[cc] = {
                                    "passed":False,
                                    "output":"",
                                    "errput":self.formatRunOutput(errput),
                                    "expectedoutput":self.answercase[cc]
                                    }
                self.verdict = "Failed"

    def runCompile(self,compiler_name,compile_options,run_options,n_cases,binary):

        compileans=runCommand([compiler_name]+compile_options+[self.filepath],
                                            capture_output=True,encoding="utf-8",memorylimit=self.memlimit)

        #if while trying to compile and there was an error
        l=len(self.result)
        if compileans.returncode >0 :
            for cc in range(l):
                self.result[cc] ={"passed":False,
                                    "output":self.formatRunOutput(compileans.stderr.strip()),
                                    "errput":"CompileError",
                                    "expectedoutput":self.answercase[cc]
                                    }
            return

        self.runPerCase(l,[binary]+run_options,capture_output=True,timeout= self.timelimit,\
                                encoding="utf-8",memorylimit=self.memlimit)

    def run(self,ClientConnection):
        setattr(self,"runtime",time())
        l=len(self.result)
        self.state=self.PossibelTasksState[1]
        #some languagues have to compile then run
        if self.lang == "java":
            options_compile=["-d",self.folder,"-s",self.folder,"-h",self.folder]
            options_run=["-classpath",self.folder,"Solution"]
            self.runCompile("javac",options_compile,options_run,l,"java")
        elif self.lang == "c":
            options_compile=["-o",self.filepath+".out"]
            options_run=[]
            self.runCompile("gcc",options_compile,options_run,l,self.filepath+".out")
        elif self.lang=="cpp":
            options_compile=["-o",self.filepath+".out"]
            options_run=[]
            self.runCompile("g++",options_compile,options_run,l,self.filepath+".out")
        else:
            # languages like python, js, php should be fine.
            self.memlimit= self.memlimit+100 # extra 100mb for interpreted languanges
            args=self.resolveFolder(self.lang)+[self.filepath]
            self.runPerCase(l,args,capture_output=True,timeout= self.timelimit,\
                            encoding="utf-8",memorylimit=self.memlimit,lang=self.lang)

        self.state=self.PossibelTasksState[2]
        #create a submission in the database
        submission_data = {'prblmid':self.problem.getprblmid(),'name':self.problem.getName(),'userid':self.userid,'contestid':self.contestid,'ctype':self.ctype,'codecontent':self.content,
        'lang':self.lang,'stype':self.stype,'result': self.result,'verdict': self.verdict,'timesubmitted':self.timeofsubmission}
        if self.stype == "test":
            if not self.contestid:
                submission_data.pop('userid', None)
                submission_data.pop('contestid', None)
                submission_data.pop('ctype', None)
                Submission(self.userid).addDoc(submission_data)
                if self.verdict == "Passed":
                    # problem solved, update this particular problem document
                    update = {"$addToSet": {"solvedby": self.userid}}
                    Problem().flexibleUpdate(update, _id=ObjectId(self.problem.getprblmid()))
            else:
                Submission(self.userid).addDoc(submission_data) #add this submission to the submission document
                self.gradeSubmission(submission_data)

        os.remove(self.filepath)
        if self.lang.lower()=="java":
            rmtree(self.folder,ignore_errors=True)
        self.free()
        ClientConnection.send(["DONE",self.id])

    def gradeSubmission(self, data):
        """
        This method is called in :class: `Task' in task.py to make necessary
        updates to the database when submission code to a problem in the contest has be run
        """
        userid = data.get('userid')
        contestid = data.get('contestid')
        ctype = data.get('ctype')
        prblmid = data.get('prblmid')
        verdict = data.get('verdict')
        contest_problem = ContestProblem(ctype, contestid).getBy(_id=ObjectId(prblmid))
        contest = Contest(ctype).getBy(_id=ObjectId(contestid))
        contest_start_time = contest.get('starttime')
        submission_time = datetime.now().timestamp() - contest_start_time

        prblmscorefield = 'problemscore.{}'.format(prblmid)
        if verdict != "Passed":
            score = 0
            penalty = -10
            # update the penalty field
            update = {'$inc': {'penalty': penalty}}
            pScore = contest_problem.get('prblmscore')
            argDict={"contestid":contestid, prblmscorefield:{'$ne': pScore}}
            UserRegisteredContest(userid).flexibleUpdate(update, **argDict)
        else:
            score = contest_problem.get('prblmscore')
            penalty = 0

        # update the user score for that problem and time penalty, if the new score is greater than the prev one
        update = {'$set': {'problemscore.{}'.format(prblmid): score}, '$inc': {'timepenalty': submission_time}}
        argDict={"contestid":contestid,prblmscorefield:{'$lte': score}}
        if UserRegisteredContest(userid).flexibleUpdate(update, **argDict):
            # calculate the total score
            reg_contest = UserRegisteredContest(userid).getBy(contestid=contestid)
            problemscore = reg_contest.get('problemscore')
            totalscore = reg_contest.get('penalty')
            timepenalty = reg_contest.get('timepenalty')
            for each in problemscore:
                totalscore += problemscore[each]
            # update the total score
            update = {"$set": {'totalscore': totalscore}}
            UserRegisteredContest(userid).flexibleUpdate(update, contestid=contestid)

            # update the contest document to reflect this participants current score.
            update = {"$set": {'participants.{}.currscore'.format(userid): totalscore,
            'participants.{}.timepenalty'.format(userid): timepenalty}}
            Contest(ctype).flexibleUpdate(update, _id=ObjectId(contestid))
            data['score'] = totalscore
            wsdata={"point":totalscore,"username":self.username,"attempted":len(problemscore)}
            current_app.socketio.emit('newscore', wsdata,namespace='/scoreboard/')


import numpy as np
import os.path
import math
import scipy.linalg as _la
from math import factorial
import itertools
import time
import os
from scipy.sparse import csc_matrix

#..................................counting number of one
POPCOUNT_TABLE16 = [0] * 2**16
for index in range(len(POPCOUNT_TABLE16)):
	POPCOUNT_TABLE16[index] = (index & 1) + POPCOUNT_TABLE16[index >> 1]

def one_count(v):
	return (POPCOUNT_TABLE16[ v        & 0xffff] +
			POPCOUNT_TABLE16[(v >> 16) & 0xffff])

#..................................Binomial
def comb(n, k):
	kk = factorial(n) / factorial(k) / factorial(n - k)
	uga= np.int64(kk)
	return uga

#..................................from configuration to bin number
def TO_bin(xx):
	return int(xx,2)

#..................................from bin number to configuration
def TO_con(x,L):
	x1=np.int64(x)
	L1=np.int64(L)
	return np.binary_repr(x1, width=L1)

#..................................base preparation
def Base_prep(n,k):
	result = []
	for bits in itertools.combinations(range(n), k):
		s = ['0'] * n
		for bit in bits:
			s[bit] = '1'
		result.append(''.join(s))
	return result

def BaseNumRes_creation(Dim,LL,B):
	A=np.zeros((Dim,LL), dtype=np.float)

	for i in range(Dim):
		k=0
		for j in list(B[i]):
			A[i,k] = float(j)-0.5
			k+=1
	return A

#..................................hop. preparation
def Hop_prep(L,BC):
	if BC == 1:
		Hop_dim=L-1
	else:
		Hop_dim=L
	return [TO_con(2**i+2**((i+1)%L),L) for i in range(Hop_dim)]

#..................................................Disorder creation
def Dis_Creation(LL,Dis_gen):

	dis = np.zeros(LL, dtype=np.float)
	for i in range(LL):
		if Dis_gen==0:
			dis[i] = 2*np.random.random()-1
		else:
			dis[i] = np.cos(2*math.pi*0.721*i/LL)
	return dis

#..................................creation Lin Tables
def LinTab_Creation(LL,Base,di):

	L = np.int64(LL)
	Dim=np.int64(di)

#..........................Table Creation
	MaxSizeLINVEC = sum([2**(i-1) for i in range(1,np.int64(L/2+1))])

	#....creates a table LinTab_L+LinTab_R
	#.....................[  ,  ]+[  ,  ]
	LinTab   = np.zeros((MaxSizeLINVEC+1,4),dtype=int)
	Jold     = JJ=j1=j2=0
	Conf_old = TO_con(0,np.int64(L/2))

#...........................Table Filling
	for i in range(Dim):
		Conf_lx = Base[i][0:np.int64(L/2)]
		Bin_lx  = TO_bin(Conf_lx)
		Conf_rx = Base[i][np.int64(L/2):L]
		Bin_rx  = TO_bin(Conf_rx)

		if Conf_lx==Conf_old:
			j1 = Jold
		else:
			j1 += j2

		Conf_old = Conf_lx

		if Jold != j1:
			JJ = Jold = 0

		j2	= JJ+1
		Jold = j1
		JJ  += 1

		#print Conf_lx, np.int64(Bin_lx), np.int64(j1), Conf_rx, np.int64(Bin_rx), np.int64(j2)

		LinTab[Bin_lx,0]= np.int64(Bin_lx)
		LinTab[Bin_lx,1]= np.int64(j1)
		LinTab[Bin_rx,2]= np.int64(Bin_rx)
		LinTab[Bin_rx,3]= np.int64(j2)

#	print LinTab
	return LinTab

#..................................Lin Look for complete state
def LinLook(vec,LL,arr):

	Vec  = TO_con(vec,LL)
	v1	 = Vec[0:np.int64(LL/2)]
	v2	 = Vec[np.int64(LL/2):LL]
	ind1 = TO_bin(v1)
	ind2 = TO_bin(v2)
	return arr[ind1,1]+arr[ind2,3]-1

#..................................Lin Look for RIGHT state
def LinLook_LL(vec,arr):
	ind=TO_bin(vec)
	return arr[ind+1,1]

#..................................Lin Look for RIGHT state
def LinLook_RR(vec,arr):
	ind=TO_bin(vec)
	return arr[ind+1,3]

#..................................................Hamiltonian Creation
def Ham_Dense_Creation(LL,NN,Dim,D,Dis_real,BC,Base_Bin,Base_Num,Hop_Bin,LinTab):

	t=1.
	# tutto in unita di t!!

	ham = np.zeros((Dim,Dim), dtype=np.float)

	if BC == 1:
		Hop_dim=LL-1
	else:
		Hop_dim=LL

	for i in range(Dim):
		n_int = 0.0
		n_dis = 0.0
		bra = LinLook(Base_Bin[i],LL,LinTab)

		for j in range(Hop_dim):
			xx  = Base_Bin[i]^Hop_Bin[j]
			ket = LinLook(xx,LL,LinTab)

			if one_count(xx) == NN:
				ham[bra,ket] = t/2
			uu = Base_Bin[i] & Hop_Bin[j]

			if one_count(uu) == 1:
				n_int -= 0.25
			else:
				n_int += 0.25

			n_ones = Base_Bin[i] & np.int64(2**(LL-j-1))
			if n_ones != 0:
				n_dis += 0.5*Dis_real[j]
			else:
				n_dis -= 0.5*Dis_real[j]

		ham[bra,bra] = t*(0*n_int + D*n_dis)

	return ham

#..................................................Hamiltonian Creation
def Ham_Sparse_Creation(LL,NN,Dim,D,Dis_real,BC,Base_Bin,Base_Num,Hop_Bin,LinTab):

	t=1.
	# tutto in unita di t!!

	ham_ind1 = []
	ham_ind2 = []
	ham_val  = []

	if BC == 1:
		Hop_dim=LL-1
	else:
		Hop_dim=LL

	for i in range(Dim):
		n_int = 0.0
		n_dis = 0.0
		bra = LinLook(Base_Bin[i],LL,LinTab)

		for j in range(Hop_dim):
			xx  = Base_Bin[i]^Hop_Bin[j]
			ket = LinLook(xx,LL,LinTab)

			if one_count(xx) == NN:

				ham_ind1.append( bra )
				ham_ind2.append( ket )
				ham_val.append(  t/2 )

			uu = Base_Bin[i] & Hop_Bin[j]

			if one_count(uu) == 1:
				n_int -= 0.25
			else:
				n_int += 0.25

			n_ones = Base_Bin[i] & np.int64(2**(LL-j-1))
			if n_ones != 0:
				n_dis += 0.5*Dis_real[j]
			else:
				n_dis -= 0.5*Dis_real[j]

		ham_ind1.append( bra )
		ham_ind2.append( bra )
		ham_val.append(  t*(0*n_int + D*n_dis) )

	ham = csc_matrix((ham_val, (ham_ind1,ham_ind2)), shape=(Dim,Dim), dtype=np.double)

	return ham

#..................................................Hamiltonian Dense Diagonalization
def eigval(A):
	E = _la.eigh(A)
	return E

#..................................................Hamiltonian Dense Diagonalization
def levstat(E,Dim):
	gap=E[1:]-E[:-1]
	B = np.zeros(Dim-2, dtype=np.float)
	for i in range(Dim-2):
		B[i]=np.minimum(gap[i+1],gap[i])/np.maximum(gap[i+1],gap[i])
	return B

#..................................................Hamiltonian Sparse Diagonalization
def eigsh(A,n):
	E = _la.sparse.linalg.eigsh(A, n)
	return E

#..................................................Initial state
def Psi_0(Dim):
	n = np.random.randnp.int64(0,Dim-1)
	#n = 0
	return n

def Proj_Psi0(a,V):
	return V[a]

#..................................................Traslations MEAN
def Trasl_Mean(A):
	a = A.shape
	B = np.zeros((a[1],a[1]), dtype=np.float)
	for i in range(a[1]):
		B[i] = np.roll(A[i],-i)
	return np.mean(B, axis=0)

#..................................................dens
def density(V,Base_NumRes):
	den   = np.dot(np.transpose(V**2),Base_NumRes)
	#equivalente a fare:
	#dens = np.einsum('jn,jn,ji -> ni', V, V, Base_NumRes)
	return den

def density_t(Pro,V,BDens):
	den = np.einsum('i,ij,jk-> k', Pro, V, BDens)

	#np.dot(np.transpose(V**2),Base_NumRes)
	return den

#..................................................NiNj
def OUTER_creation(L,Dim,A):
	B = np.zeros((Dim,L,L), dtype=np.float)
	for i in range(Dim):
		B[i] = np.outer(A[i],A[i])
	return B

def SzSz_con_P(A,B,C):
	SzSz=np.einsum('il,ijk -> ljk', A**2, B)
	uga =SzSz-C
	return uga

def SzSz_con_P_Psi0(A,B):
	uga=np.einsum('i,ijk -> jk', A, B)
	return uga

def SzSz_con_Huse(A):
	L    = A.shape[0]
	uga_mat= np.einsum('ijk -> jk', np.absolute(A))
	uga  = uga_mat/L
	return uga

def SzSz_con_Huse_t(A):
	L    = A.shape[0]
	Aabs = np.absolute(A)
	Alog = np.log(Aabs)
	uga_mat  = np.einsum('ijk -> jk', Alog)
	uga  = uga_mat/L
	return uga

def Mat_SzSz_DE(A,B,C):
	#NN -> A V, B Base_Corr, C Proj_Psi0
	corr_zero=np.einsum('l,il,ijk -> jk',C, A**2, B)
	return corr_zero

def Mat_Sz_DE(A,B):
	#NN -> A Dens B Proj_Psi0
	corr_zero=np.einsum('i,ij -> j',B, A)
	return corr_zero

def SzSz_con_DE(A,B,C):
	#A proiezioni, B SzSz, C Sz
	Sz2=np.outer(C,C)
	uga=B-Sz2
	return uga

def SPARSE_SzSz_con_DE(psi_t,Base_Corr,Base_NumRes):

	#mean SzSz
	SzSz = np.einsum('i, ijk -> jk', np.abs(psi_t)**2, Base_Corr)

	#mean Sz
	Dens = np.dot(np.transpose(np.abs(psi_t)**2),Base_NumRes)
	Sz2  = np.outer(Dens,Dens)

	#connected correlations
	SzSz_con = SzSz - Sz2

	return SzSz_con

#..................................................CdiCj

def prep_tab(L):
	Dim = comb(L, np.int64(L/2))

	Base_Num = Base_prep(L,np.int64(L/2))
	Base_Bin = [int(Base_Num [i],2) for i in range(Dim)]
	LinTab   = LinTab_Creation(L,Base_Num,Dim)

	CdC_Tab  = CdC_tabCreation (L,np.int64(L/2),Dim,Base_Num,Base_Bin,LinTab)

	return CdC_Tab

def CdC_tabCreation (LL,NN,Dim,Base_Num,Base_Bin,LinTab):
	dimCiCj =  comb(LL-2, NN-1)
	CdC_Tab  =  np.zeros((LL,LL,dimCiCj,2), dtype=int)

	for i in range(LL):
		for j in range(LL):

			xx = np.zeros((dimCiCj,2), dtype=int)
			x0 = 0

			for l in range(Dim):

				a = Base_Num[l][0:i]
				b = Base_Num[l][i+1:LL]
				c = ''.join([a,'1',b])
				a = c[0:j]
				b = c[j+1:LL]
				d = ''.join([a,'0',b])
				if (one_count(int(d,2)) == NN and int(d,2) != Base_Bin[l]):
					bra = LinLook(Base_Bin[l],LL,LinTab)
					ket = LinLook(int(d,2),LL,LinTab)

					xx[x0,0] = np.int64(bra)
					xx[x0,1] = np.int64(ket)
					x0 += 1

			CdC_Tab[i,j] = xx

	return CdC_Tab

def Mat_CdC_i(UU1,LL,V,l):

	CC = np.zeros((LL,LL),dtype=float)

	for i in range(LL):
		for j in range(i,LL):
			uu = UU1[i,j]
			CC[j,i] = CC[i,j] = np.inner(V[uu[:,0],l],V[uu[:,1],l])
	np.fill_diagonal(CC, 0.25)
	return CC

def Mat_CdC_Psi0(UU1,Proj_Psi0,Dim,LL,V):

	CC = np.empty((Dim,LL,LL),dtype=float)

	for l in range(Dim):
		for i in range(LL):
			for j in range(i,LL):
				uu = UU1[i,j]
				CC[l,j,i] = CC[l,i,j] = np.inner(V[uu[:,0],l],V[uu[:,1],l])
				#print Dim, l, i, j, np.inner(V[uu[:,0],l],V[uu[:,1],l])
		np.fill_diagonal(CC[l], 0.25)

		CC[l] *= Proj_Psi0[l]

	CC1 = np.einsum('ijk -> jk', CC)

	return CC1

def generate_filename(basename):
	local_time = str(np.int64(round(time.time() * 1000)))
	xx = basename + local_time + ".dat"
	if os.path.isfile(xx):
		time.sleep(1)
		return generate_filename(basename)
	return xx

#..................................................Entropy

#..................................................Time - Evolution

def Corr_Evolution(Proj_Psi0,E,V,t,Base_NumRes,Base_Corr):
	Pro_t0 = Proj_Psi0
	#c'era un V di troppo::: np.einsum('i,ki-> k', Proj_Psi0, V)

	Pro_t  = np.outer(Pro_t0*np.exp(1j*E*t),Pro_t0*np.exp(-1j*E*t))

	coef 	   = np.real(np.einsum('nm,jn,jm -> j', Pro_t, V, V))

	dens_t 	   = np.real(np.einsum('j,ji -> i',   coef, Base_NumRes))
	corr_t 	   = np.real(np.einsum('j,jli -> li', coef, Base_Corr))
	corr_con_t = np.real(corr_t - np.outer(dens_t,dens_t))

	corr_con_t_AVER = Trasl_Mean(corr_con_t)

	return dens_t,corr_t,corr_con_t_AVER

#..................................................Print_MATRIX
def print_matrix(H):

	#print('matrix to print')

	if isinstance(H, csc_matrix):
		print_h = csc_matrix.todense(H)
		print(print_h)
	else:
		print(H)

	return 0

def split(container, count):
	"""
	Simple function splitting a container into equal length chunks.
	Order is not preserved but this is potentially an advantage depending on
	the use case.
	"""
	return [container[_i::count] for _i in range(count)]


import pygame
from pygame.rect import *
from pygame.locals import *

BLACK = (0, 0, 0)
GRAY = (127, 127, 127)
WHITE = (255, 255, 255)

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)

SIZE = 600,600
imagePath = "Capitulo_4/python.png"
running = True
moving = False
background = GRAY

width, height = SIZE
screenCenter = width//2, height//2
pygame.init()
screen = pygame.display.set_mode(SIZE)
img0 = pygame.image.load(imagePath)
# Torna a imagem mais leve para o render
img0.convert()
scale = 1
angle = 0

rect0 = img0.get_rect()
pygame.draw.rect(img0, GREEN, rect0, 2)

img = img0
rect = img.get_rect()
rect.center = screenCenter

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == KEYDOWN:
            if event.key == K_r:
                if event.mod & KMOD_SHIFT:
                    angle -= 10
                else:
                    angle += 10
                img = pygame.transform.rotozoom(img0, angle, scale)

            elif event.key == K_s:
                if event.mod & KMOD_SHIFT:
                    scale /= 1.1
                else:
                    scale *= 1.1
                img = pygame.transform.rotozoom(img0, angle, scale)

            elif event.key == K_h:
                img = pygame.transform.flip(img, True, False)

            elif event.key == K_v:
                img = pygame.transform.flip(img, False, True)

            elif event.key == K_o:
                img = img0
                scale = 1
                angle = 0

            elif event.key == K_1 or event.key == K_KP1:
                img = pygame.transform.laplacian(img)

            elif event.key == K_2 or event.key == K_KP2:
                img = pygame.transform.scale2x(img)

            rect = img.get_rect()
            rect.center = screenCenter

    screen.fill(GRAY)
    screen.blit(img, rect)
    if moving:
        pygame.draw.rect(screen, RED, rect, 4)
    else:
        pygame.draw.rect(screen, GREEN, rect, 4)

    pygame.display.update()

pygame.quit()


# -*- coding: utf-8 -*-
# @Time    : 2022/9/14
# @Author  : youjiangyong
# @File    : test_hotSpotRank.py
import allure
import pytest
from Common.Base import base
from Config.path_config import PathMessage
import os,jsonpath,datetime,random

def get_timestamp():
    timestamp = datetime.datetime.now().replace(hour=9, minute=0, second=0, microsecond=0).timestamp()
    timestamp = int(timestamp)
    return timestamp
print(get_timestamp())

@allure.feature('官方热点榜')
@pytest.mark.flaky(reruns=5, reruns_delay=1)
class TestCase_Rank_hotSpotRank():
    timestamp = get_timestamp()
    @allure.story('验证官方热点榜遍历商品一级级分类返回的数据是否大于10条')
    @pytest.mark.parametrize('times', base.return_time_message_nowday())
    @pytest.mark.parametrize('timestamp', [timestamp])
    @allure.title("官方热点榜查看榜点：{timestamp}")
    def test_rank_promotionAweme_big_type(self, get_token, get_host, timestamp, times):
        para = f'day_type={times[0]}&date={times[1]}&page=1&size=50&type=1&timestamp={timestamp}'
        response = base().return_request(method="get", path=PathMessage.rank_hotSpotRank, data=para, tokens=get_token,hosts=get_host, )
        # print(response)
        assert len(response["response_body"]["data"]["list"]) > 10
        # aweme_id_list = jsonpath.jsonpath(response["response_body"], '$.data.list[*].aweme_id')
        # print(aweme_id_list)


from lib.cuckoo.common.abstracts import Signature

utilities = [
    "at ",
    "at.exe",
    "attrib",
    "chcp",
    "del ",
    "del.exe",
    "dir ",
    "dir.exe",
    "driverquery",
    "erase ",
    "erase.exe",
    "fsutil",
    "getmac",
    "ipconfig",
    "nbtstat",
    "net ",
    "net.exe",
    "netsh",
    "netstat",
    "nslookup",
    "pathping",
    "ping ",
    "ping.exe",
    "qwinsta",
    "reg ",
    "reg.exe",
    "regsrv32",
    "route",
    "runas",
    "rwinsta",
    "sc ",
    "sc.exe",
    "schtasks",
    "shutdown",
    "sigverif",
    "systeminfo",
    "tasklist",
    "taskkill",
    "telnet",
    "whoami",
    "wmic",
    "wusa",
]

risk_utilities = [
    "bitsadmin",
    "cacls",
    "csvde",
    "dsquery",
    "icacls",
    "nltest",
    "rexec",
    "sdbinst",
    "volumeid",
    "vssadmin",
    "wevtutil",
    "whois",
    "xcacls",
]

sysinternals = [
    "accesschk",
    "accessenum",
    "adexplorer",
    "adinsight",
    "adrestore",
    "autologon",
    "autoruns",
    "bginfo",
    "bluescreen",
    "clockres",
    "contig",
    "coreinfo",
    "ctrl2cap",
    "debugview",
    "desktops",
    "disk2vhd",
    "diskext",
    "diskmon",
    "du ",
    "du.exe",
    "efsdump",
    "findlinks",
    "handle ",
    "handle.exe",
    "hex2dec",
    "junction",
    "ldmdump",
    "listdlls",
    "livekd",
    "loadorder",
    "logonsessions",
    "movefile",
    "notmyfault",
    "ntfsinfo",
    "pendmoves",
    "pipelist",
    "portmon",
    "procdump",
    "psexec",
    "psfile",
    "bginfo",
    "psgetsid",
    "psinfo",
    "pskill",
    "pslist",
    "psloggedon",
    "psloglist",
    "pspasswd",
    "psping",
    "psservice",
    "psshutdown",
    "pssuspend",
    "pstools",
    "rammap",
    "regdelnull",
    "ru ",
    "ru.exe",
    "regjump",
    "sdelete",
    "shareenum",
    "shellrunas",
    "sigcheck",
    "streams ",
    "streams.exe",
    "strings ",
    "strings.exe",
    "sync ",
    "sync.exe",
    "sysmon",
    "tcpview",
    "vmmap",
    "volumeid",
    "whois",
    "winobj",
    "zoomit",
]

class UsesWindowsUtilities(Signature):
    name = "uses_windows_utilities"
    description = "Uses Windows utilities for basic Windows functionality"
    severity = 2
    categories = ["commands", "lateral"]
    authors = ["Cuckoo Technologies"]
    minimum = "2.0"
    ttp = ["T1053"]
    references = ["http://blog.jpcert.or.jp/2016/01/windows-commands-abused-by-attackers.html"]

    def on_complete(self):
        for cmdline in self.get_command_lines():
            for utility in utilities:
                if utility in cmdline.lower():
                    self.mark_ioc("cmdline", cmdline)

        return self.has_marks()

class SuspiciousCommandTools(Signature):
    name = "suspicious_command_tools"
    description = "Uses suspicious command line tools or Windows utilities"
    severity = 3
    categories = ["commands", "lateral"]
    authors = ["Kevin Ross"]
    minimum = "2.0"

    def on_complete(self):
        for cmdline in self.get_command_lines():
            for utility in risk_utilities:
                if utility in cmdline.lower():
                    self.mark_ioc("cmdline", cmdline)

        return self.has_marks()

class SysInternalsToolsUsage(Signature):
    name = "sysinternals_tools_usage"
    description = "Uses Sysinternals tools in order to add additional command line functionality"
    severity = 3
    categories = ["commands", "lateral"]
    authors = ["Kevin Ross"]
    minimum = "2.0"
    references = ["docs.microsoft.com/en-us/sysinternals/downloads/"]

    def on_complete(self):
        for cmdline in self.get_command_lines():
            for utility in sysinternals:
                if utility in cmdline.lower():
                    self.mark_ioc("cmdline", cmdline)

        return self.has_marks()

class AddsUser(Signature):
    name = "adds_user"
    description = "Uses windows command to add a user to the system"
    severity = 2
    categories = ["commands"]
    authors = ["Kevin"]
    minimum = "2.0"
    ttp = ["T1136"]

    def on_complete(self):
        for cmdline in self.get_command_lines():
                if cmdline.lower().startswith("net") and "user /add" in cmdline.lower():
                    self.mark_ioc("cmdline", cmdline)

        return self.has_marks()

class AddsUserAdmin(Signature):
    name = "adds_user_admin"
    description = "Uses windows command to add a user to the administrator group"
    severity = 3
    categories = ["commands"]
    authors = ["Kevin"]
    minimum = "2.0"
    ttp = ["T1098"]

    def on_complete(self):
        for cmdline in self.get_command_lines():
                if cmdline.lower().startswith("net") and "localgroup administrators" in cmdline.lower():
                    self.mark_ioc("cmdline", cmdline)

        return self.has_marks()


from django.urls import path, include
from estado.views import demonio_print
from estado.views import Estadoview,Estadoinsertar,Estadoeditar,Estadoeliminar,Partecuerpoview,Partecuerpoinsertar,Partecuerpoeditar,Partecuerpoeliminar,Detallecuerpoview,Detallecuerpoinsertar,Detallecuerpoeditar,Detallecuerpoeliminar,Demonioview,Demonioinsertar,Demonioeditar,Demonioeliminar,Batallaview,Batallainsertar,Batallaeditar,Batallaeliminar

urlpatterns = [
    path('Estado', Estadoview.as_view(), name='Estados'),
    path('Estado/new', Estadoinsertar.as_view(), name='AgregarE'),
    path('Estado/edit/<int:pk>', Estadoeditar.as_view(), name='EditarE'),
    path('Estado/delete/<int:pk>', Estadoeliminar.as_view(), name='EliminarE'),

    path('Partecuerpo', Partecuerpoview.as_view(), name='Partecuerpos'),
    path('Partecuerpo/new', Partecuerpoinsertar.as_view(), name='AgregarP'),
    path('Partecuerpo/edit/<int:pk>', Partecuerpoeditar.as_view(), name='EditarP'),
    path('Partecuerpo/delete/<int:pk>', Partecuerpoeliminar.as_view(), name='EliminarP'),

    path('Detallecuerpo', Detallecuerpoview.as_view(), name='Detallecuerpos'),
    path('Detallecuerpo/new', Detallecuerpoinsertar.as_view(), name='AgregarDC'),
    path('Detallecuerpo/edit/<int:pk>', Detallecuerpoeditar.as_view(), name='EditarDC'),
    path('Detallecuerpo/delete/<int:pk>', Detallecuerpoeliminar.as_view(), name='EliminarDC'),

    path('', Batallaview.as_view(), name='Batallas'),
    path('Batalla/new', Batallainsertar.as_view(), name='AgregarB'),
    path('Batalla/edit/<int:pk>', Batallaeditar.as_view(), name='EditarB'),
    path('Batalla/delete/<int:pk>', Batallaeliminar.as_view(), name='EliminarB'),

    path('Demonio', Demonioview.as_view(), name='Demonios'),
    path('Demonio/new', Demonioinsertar.as_view(), name='AgregarD'),
    path('Demonio/edit/<int:pk>', Demonioeditar.as_view(), name='EditarD'),
    path('Demonio/delete/<int:pk>', Demonioeliminar.as_view(), name='EliminarD'),
    path('Demonio/print',demonio_print,name='demonio_print'),
    path('Demonio/print/<int:pk>',demonio_print,name='demonio_print_one'),
  ]


import pygame,sys
from pygame.locals import *

class MySprite(pygame.sprite.Sprite):
    def __init__(self,target):
        pygame.sprite.Sprite.__init__(self)
        self.master_image = None
        self.frame =0
        self.old_frame=-1
        self.frame_width=1
        self.frame_heiget=1
        self.first_frame=0
        self.last_frame=0
        self.columns=1
        self.last_time=0

    def _getx(self):return self.rect.x
    def _setx(self,value):self.rect.x =value
    X = property(_getx,_setx)

    def _gety(self):return self.rect.y
    def _sety(self,value):self.rect.y =value
    Y = property(_gety,_sety)

    def _getpos(self): return self.rect.topleft
    def _setpos(self,pos):self.rect.topleft = pos
    position=property(_getpos,_setpos)

    def load(self,filename,width,height,columns):
        self.master_image=pygame.image.load("pic/dragon.png").convert_alpha()
        self.frame_width=width
        self.frame_heiget=height
        self.rect=Rect(0,0,width,height)
        self.columns =columns
        rect =self.master_image.get_rect()
        self.last_frame=(rect.width // width)*(rect.height // height -1)

    def update(self,current_time,rate=30):
        if current_time>self.last_time+rate:
            self.frame +=1
            if self.frame > self.last_frame:
                self.frame = self.first_frame
            self.last_time = current_time
        if self.frame != self.old_frame:
            frame_x = (self.frame % self.columns)*self.frame_width
            frame_y = (self.frame // self.columns) * self.frame_heiget
            rect = Rect(frame_x,frame_y,self.frame_width,self.frame_heiget)
            self.image = self.master_image.subsurface(rect)
            self.old_frame=self.frame
    def __str__(self):
        return str(self.frame)+","+str(self.first_frame)+\
               ","+str(self.last_frame)+","+str(self.frame_width)+\
               ","+str(self.frame_heiget)+","+str(self.columns)+\
               ","+str(self.rect)

def print_text(font,x,y,text,color=(255,255,255)):
    imgText = font.render(text,True,color)
    screen.blit(imgText,(x,y))

pygame.init()
screen = pygame.display.set_mode((800,600),0,32)
pygame.display.set_caption("Sprite Animation Demo")
font = pygame.font.Font(None,18)
framerate=pygame.time.Clock()

dragon = MySprite(screen)
dragon.load("pic/dragon.png",260,150,3)
group = pygame.sprite.Group()
group.add(dragon)

while True:
    framerate.tick(30)
    ticks=pygame.time.get_ticks()

    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    key=pygame.key.get_pressed()
    if key[pygame.K_ESCAPE]:
        sys.exit()

    screen.fill((0,0,100))
    group.update(ticks)
    group.draw(screen)
    print_text(font,0,0,"Sprite:"+str(dragon))
    pygame.display.update()


# Author: Yuchen Liu HID213, Wenxuan Han HID209, Junjie Lu HID214
# Data: 2017.12.01
# Reference: http://blog.csdn.net/tinkle181129/article/details/55261251

from datetime import datetime

import matplotlib.pyplot as plt
import pandas as pd
from numpy import *
from sklearn import svm
from sklearn import tree
from sklearn.cross_validation import cross_val_score
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB

# 1. read data from csv
def read_data():
    data_set = pd.read_csv("train.csv")
    data = data_set.values[0:, 1:]
    label = data_set.values[0:, 0]
    print("Data load completed.")
    return data, label

# plot 70 samples
def show_pic(data):
    print(shape(data))
    plt.figure(figsize=(7, 7))
    for digit_num in range(0, 70):
        plt.subplot(7, 10, digit_num + 1)
        grid_data = data[digit_num].reshape(28, 28)
        plt.imshow(grid_data, interpolation="none", cmap="afmhot")
        plt.xticks([])
        plt.yticks([])
    plt.tight_layout()
    plt.savefig("data_samples.png")

# 2. Data Cleaning
# The data is from 0-255 for each cell.
# Normalize data by set all value > 0 to 1
def data_clean(data):
    m, n = shape(data)
    new_data = zeros((m, n))
    for i in range(m):
        for j in range(n):
            if data[i, j] > 0:
                new_data[i, j] = 1
            else:
                new_data[i, j] = 0
    print("Data clean completed.")
    return new_data

# 3. Feature Selection by PCA
def feature_selection(data):
    # First, use explained_variance to get recommended number of component
    pca = PCA()
    # pca_parameter = pca.fit(data)
    pca.fit(data)
    ev = pca.explained_variance_
    ev_ratio = []
    for i in range(len(ev)):
        ev_ratio.append(ev[i] / ev[0])

    # select number of component which have a higher ratio
    # than 0.05 with the first components
    n = 0
    for i in range(len(ev_ratio)):
        if ev_ratio[i] < 0.05:
            n = i
            # print(n)
            break

    # Then, PCA the model by the number of components
    # pca = PCA(n_components=n, whiten=True)
    pca = PCA(n_components=n, whiten=True)
    print("Feature selection completed.")
    return pca.fit_transform(data)

# 4. Model Selection
def model_acc(data, label, model):
    start = datetime.now()
    acc = cross_val_score(model, data, label, cv=5, scoring="accuracy").mean()
    end = datetime.now()
    time_use = (end - start).seconds
    print("Time use: ", time_use)
    print("Accuracy by cross validation: ", acc)

def dt_classifier(data, label, data_type):
    dt_model = tree.DecisionTreeRegressor()
    dt_model.fit(data, label)
    print("Test " + data_type + " using DT: ")
    model_acc(data, label, dt_model)

def nb_classifier(data, label, data_type):
    nb_model = GaussianNB()
    nb_model.fit(data, label)
    print("Test " + data_type + " using NB: ")
    model_acc(data, label, nb_model)

def lr_classifier(data, label, data_type):
    lr_model = LogisticRegression()
    lr_model.fit(data, label)
    print("Test " + data_type + " using LR: ")
    model_acc(data, label, lr_model)

def rf_classifier(data, label, flag):
    rf_model = RandomForestClassifier(n_estimators=100)
    rf_model.fit(data, label)
    print("Test " + flag + " using RF: ")
    model_acc(data, label, rf_model)

def svm_classifier(data, label, flag):
    svm_model = svm.SVC(kernel="rbf", C=10)
    svm_model.fit(data, label)
    # svc_clf = NuSVC(nu=0.1, kernel='rbf', verbose=True)
    print("Test " + flag + " using SVM: ")
    model_acc(data, label, svm_model)

def main():
    data, label = read_data()
    # show_pic(data)
    clean_data = data_clean(data)

    test_type = 3
    for i in range(1, 3):
        print("In %d test" % i)

        if test_type == 0:
            input_data = data
            str = "raw data"
        elif test_type == 1:
            input_data = clean_data
            str = "clean data"
        elif test_type == 2:
            input_data = feature_selection(data)
            str = "pca data"
        elif test_type == 3:
            input_data = feature_selection(clean_data)
            str = "pca clean data"

        dt_classifier(input_data, label, str)
        nb_classifier(input_data, label, str)
        lr_classifier(input_data, label, str)
        rf_classifier(input_data, label, str)
        svm_classifier(input_data, label, str)

main()


import pytest

from qhub.render import render_default_template

@pytest.mark.parametrize('config_filename', [
    'tests/assets/config_aws.yaml',
    'tests/assets/config_gcp.yaml',
    'tests/assets/config_do.yaml',
])
def test_render(config_filename, tmp_path):
    output_directory = tmp_path / 'test'
    output_directory.mkdir()

    render_default_template(str(output_directory), config_filename)


from pymongo import MongoClient, InsertOne
import logging
from collections import defaultdict
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from config import TTL_BATCH_SIZE, DB_NAME

client = MongoClient()
db = client[DB_NAME]
coll_path = db.cname_real_ip
new_coll = db.ttl_real
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)

cnt = 0
requests = []
finished = set()
for doc in new_coll.find({}, {}):
    finished.add(doc["_id"])
logging.info("Finished %d" % len(finished))

for doc in coll_path.find():
    if doc["_id"] in finished:
        continue
    ttl = defaultdict(set)
    new_doc = dict()
    for rec in doc["A"]:
        # ip = rec[1][0][2][0]
        for line in rec[1]:
            ttl[str(rec[2])] |= set(line[2])
    for rec in doc["CNAMES"]:
        for line in rec[1]:
            ttl[str(rec[2])] |= set(line[2])
    for ttl_time in ttl:
        # find the min time with this domain in it
        ip_set = ttl[ttl_time]
        min_set = 1000000
        min_ip = None
        for ip in ip_set:
            tmp_doc = db.ip_domain.find_one({"_id": ip})
            tmp_an_set = set(tmp_doc["domain_set"])
            if doc["_id"] in tmp_an_set:
                tmp_size = tmp_doc["sz"]
                if tmp_size < min_set:
                    min_set = tmp_size
                    min_ip = ip
        if min_set < 1000000:
            ttl[ttl_time] = [min_set, min_ip]
        else:
            ttl[ttl_time] = "NA"
    new_doc["_id"] = doc["_id"]
    new_doc["qname"] = doc["qname"]
    new_doc["ttl"] = ttl
    cnt += 1
    requests.append(InsertOne(new_doc))
    if cnt % TTL_BATCH_SIZE == 0:
        new_coll.bulk_write(requests)
        requests = []
        logging.info(cnt)
new_coll.bulk_write(requests)


import time, math, config
from machine import Pin, ADC

mtr_enable = Pin('P20', mode=Pin.OUT)
snsr_enable = Pin('P19', mode=Pin.OUT)

adc = ADC()
adc.vref(config.level_vref)
sensor = adc.channel(pin='P13', attn=ADC.ATTN_11DB)

def purge(purge_for = 1):
    """ Purge air from the tube for given number of seconds """

    # turn on the pump for x seconds to purge the pipe
    mtr_enable.toggle()
    time.sleep(purge_for)
    mtr_enable.toggle()

def get():
    """ Take a reading(in mm) from the Level Sensor """

    # turn the sensor on and wait for it to boot up
    snsr_enable.toggle()
    time.sleep(1)

    zero_offset = config.level_zero_offset or 330.00    # Minimum sensor voltage reading (millivolts). In theory should be 330.00 but 286 gives us about 0 so...
    print('Offset = %s' % zero_offset)
    Vsupply = 3300.00       # Supply voltage to sensor (millivolts)
    pmax = 5.00             # Maximum sensor operating range (psi)
    psi_to_pa = 6894.76     # Conversion factor from psi to Pa
    g = 9.81                # Typical gravitational acceleration @ sea level (m/s^2)
    rho = 1000.00           # Density of water (kg/m^3)

    # take a bunch of readings and sum the result
    # this is to metigate the ESP32's noisy ADC
    reading = []
    print('start reading...')
    for i in range(100):
        # Calculate water pressure in Pa
        water_pressure = pmax * psi_to_pa * (sensor.voltage() - zero_offset) / (0.8 * Vsupply)

        # Calculate water depth (mm) from pressure (kPa)
        mm = round((1000 * water_pressure) / (rho * g))

        # append this reading to our list
        reading.append(mm)

        # print(water_pressure)
        print('{}mm'.format(mm))

        # wait a tick for next reading
        time.sleep(0.05) # 0.02 takes ~2 seconds to complete loop

    # turn the sensor off
    snsr_enable.toggle()
    print('end reading')

    rounded = math.ceil(sum(reading)/len(reading))
    reading = rounded if rounded >= 0 else 0

    return reading

def put(last_level):
    """ Store the given level reading """

    file = open(config.last_level_file, 'w')
    file.write('value = %s' % last_level)
    file.close()

def last():
    """ Get the previous level reading """
    output = {}
    try:
        execfile(config.last_level_file, None, output)
        return output['value']
    except:
        return 0


import pandas as pd
import numpy as np
# import seaborn as sb
import matplotlib.pyplot as plt

import statsmodels.formula.api as sm
from sklearn.model_selection import train_test_split # train and test
from sklearn import metrics
from sklearn.metrics import classification_report

#Importing Data
claimants1 = pd.read_csv("C:/Datasets_BA/Logistic regression/claimants.csv",sep=",")

#removing CASENUM
claimants1 = claimants1.drop('CASENUM', axis = 1)
claimants1.head(11)
claimants1.isna().sum()

# Imputating the missing values
########## Median Imputation ############
claimants1.fillna(claimants1.median(), inplace=True)
claimants1.isna().sum()

### Splitting the data into train and test data
# from sklearn.model_selection import train_test_split
train_data, test_data = train_test_split(claimants1, test_size = 0.3) # 30% test data

# Model building
# import statsmodels.formula.api as sm
logit_model = sm.logit('ATTORNEY ~ CLMAGE + LOSS + CLMINSUR + CLMSEX + SEATBELT', data = train_data).fit()

#summary
logit_model.summary2() # for AIC
logit_model.summary()

#prediction
train_pred = logit_model.predict(train_data.iloc[ :, 1: ])

# Creating new column
# filling all the cells with zeroes
train_data["train_pred"] = np.zeros(938)

# taking threshold value as 0.5 and above the prob value will be treated as correct value
train_data.loc[train_pred > 0.5, "train_pred"] = 1

# classification report
classification = classification_report(train_data["train_pred"], train_data["ATTORNEY"])
classification

# confusion matrix
confusion_matrx = pd.crosstab(train_data.train_pred, train_data['ATTORNEY'])
confusion_matrx

accuracy_train = (308 + 358)/(938)
print(accuracy_train)

#ROC CURVE AND AUC
fpr, tpr, threshold = metrics.roc_curve(train_data["ATTORNEY"], train_pred)

#PLOT OF ROC
plt.plot(fpr, tpr);plt.xlabel("False positive rate");plt.ylabel("True positive rate")

roc_auc = metrics.auc(fpr, tpr)  #AUC = 0.76

# Prediction on Test data set
test_pred = logit_model.predict(test_data)

# Creating new column for storing predicted class of Attorney
# filling all the cells with zeroes
test_data["test_pred"] = np.zeros(402)

# taking threshold value as 0.5 and above the prob value will be treated as correct value
test_data.loc[test_pred > 0.5, "test_pred"] = 1

# confusion matrix
confusion_matrix = pd.crosstab(test_data.test_pred, test_data['ATTORNEY'])

confusion_matrix
accuracy_test = (124 + 158)/(402)
accuracy_test

# Based on ROC curv we can say that cut-off value should be 0.60, We can select it and check the acccuracy again.


import setuptools
from setuptools import find_packages

_name = "data_streaming_pipeline"
_repo_name = "realtime_data_streaming_pipeline"
_license = 'Proprietary: Internal use only'
_description = "Realtime Data pipeline and Web application"
_github_username = "NourSamir"

setuptools.setup(
    name=_name,
    version='1.0.0',
    description=_description,
    license=_license,
    url=f'https://github.com/{_github_username}/{_repo_name}.git',
    author='Nour Samir',
    author_email='Noursamir96@gmail.com',
    python_requires='>=3.7',

    classifiers=[
        'Development Status :: Alpha',
        'Environment :: cli',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 3.7',
        'Topic :: Utilities',
    ],

    packages=find_packages(exclude=['tests']),

    install_requires=[
        "Flask",
        "Flask-Cors",
        "python-dotenv",
        "kafka-python",
        "redis",
    ],

    test_suite='unittest.TestCase',
    include_package_data=True,

    entry_points={
        'console_scripts': [
            "run_app_service = app_service.main:main",
            "run_metrics_calculation_flow = metrics_calculation_flow.main:main",
            "run_data_transformation_flow = data_transformation_flow.main:main",
            "run_producer_app = producer_app.main:main",
        ],
    },
)


import numpy as np
import pygame
from tile import Tile
import algorithme
from button import Button
from timer import Timer

class Board:
    def __init__(
        self,
        name: str,  # Nom de la grille
        window: pygame.Surface,  # Paramètre spécifiant la surface d'affichage
        matrix: np.ndarray,  # Matrice numpy contenant la grille de sudoku
        progress: np.ndarray,  # Matrice contenant les saisies du joueur
        notes: np.ndarray,  # Matrice contenant les notes prises par l'utilisateur
        noteMode,  # Paramètre vérifiant si le mode note est activé
        errors: int,  # Compteur d'erreurs
        solution: np.ndarray,  # Matrice contenant la solution de la grille
        elapsed_time: int = 0,  # Temps écoulé depuis le lancement du code
        play=True,  # Si la grille est faite pour jouer dessus ou alors pour regarder la solution
        font="Source Sans Pro"  # Police d'écriture
    ):
        self.active = True  # Si cet objet board est actif, il va être affiché à l'écran
        self.pause = False  # Mode pause pour arrêter le timer
        self.play = play  # Si la grille est faite pour jouer dessus ou alors pour regarder la solution
        self.name = name  # Nom de la grille
        self.init_board = np.array(matrix)  # Grille de départ
        self.size = self.init_board.shape[0]  # Taille de la grille
        self.bloc_size = int(self.size**0.5)  # Taille des sections de la grille
        self.cell_size = int(180 // self.bloc_size)  # Taille d'une case
        self.board_size = int(self.size * self.cell_size)  # Taille en pixels de la grille
        self.current_tile = None  # Case actuellement selectionnée
        self.offset = (
            int((1280 - self.board_size) / 2),
            int((720 - self.board_size) / 2),
        )  # Décalage permettant que la grille soit au milieu de l'écran
        self.error_count = errors  # Compteur d'erreurs
        self.solution = np.array(solution)  # Solution de la grille
        self.screen = window  # Surface de l'écran
        self.window = window.subsurface(
            pygame.Rect(*self.offset, self.board_size, self.board_size)
        )  # Surface d'affichage de la grille
        self.tiles = [
            [
                Tile(
                    i * self.cell_size,
                    j * self.cell_size,
                    progress[i][j],
                    self.init_board[i][j],
                    notes[i][j] if len(notes) > 0 else [],
                    self.solution[i][j] == progress[i][j],
                    self.window,
                    self.cell_size,
                    self.bloc_size,
                )
                for j in range(self.size)
            ]
            for i in range(self.size)
        ]  # Objet Tile qui contient toutes les cases de la grille
        self.history = []  # Tableau de l'historique du tableau
        self.history.append(self.tiles)
        self.noteMode = noteMode  # Paramètre vérifiant si le mode note est activé
        self.error_rect = window.subsurface(pygame.Rect(45, 150, 200, 30))  # Surface d'affichage du nombre d'erreurs
        self.back_menu = None  # Retour au menu
        self.new_game = None  # Nouvelle partie
        self.menus = None  # Tuple des menus de l'application
        self.font = font
        self.create_buttons(elapsed_time)
        self.pause_lock = False

    def is_running(self):  # Indique si une partie est en cours ou non
        return self.play and self.active and not self.pause

    def draw_board(self):  # Dessine le plateau de jeu
        for i in range(self.size):
            for j in range(self.size):
                self.tiles[i][j].draw()
                self.tiles[i][j].display()
        for i in range(1, int(self.bloc_size)):
            pygame.draw.line(
                self.window,
                (0, 0, 0),
                (i * self.cell_size * self.bloc_size, 0),
                (i * self.cell_size * self.bloc_size, self.board_size),
                3,
            )
            pygame.draw.line(
                self.window,
                (0, 0, 0),
                (0, i * self.cell_size * self.bloc_size),
                (self.board_size, i * self.cell_size * self.bloc_size),
                3,
            )

        font = pygame.font.SysFont("arial", 20)
        text = font.render(f"Erreurs : {self.error_count}/3", True, "black")
        self.error_rect.blit(
            text, text.get_rect(center=self.error_rect.get_rect().center)
        )
        pygame.mouse.set_cursor(pygame.SYSTEM_CURSOR_ARROW)
        self.note_button.process()
        self.back_button.process()
        self.back_home.process()
        self.timer_button.process()
        self.pause_button.process()
        self.big_play_button.process()

        if self.play:
            if self.error_count == 3:
                self.timer_button.stop = True
                self.popup("Vous avez fait trop d'erreurs")
                self.back_menu.process()
                self.new_game.process()

            compteur = 0
            for i in range(self.size):
                for j in range(self.size):
                    if self.tiles[i][j].value == self.solution[i][j]:
                        compteur += 1
            self.timer_button.stop = not self.is_running() or compteur == self.size * self.size
            if compteur == self.size * self.size:
                self.popup("Vous avez gagné")
                self.back_menu.process()
                self.new_game.process()

    def get_tile(self, x, y) -> Tile:  # Renvoie un objet Tile en fonction de ses coordonnées
        x -= self.offset[0]
        y -= self.offset[1]
        if 0 <= x < self.board_size and 0 <= y < self.board_size:
            return self.tiles[y // self.cell_size][x // self.cell_size]
        return None

    def select_tile(self, tile: Tile):  # Permet de sélectionner une case spécifique
        pos = tile.get_pos() if tile is not None else (-1, -1)
        for i in range(self.size):
            for j in range(self.size):
                self.tiles[i][j].selected = False
                self.tiles[i][j].background_color = "white"
                if tile is not None:
                    if (self.tiles[i][j].value != "0" or (i, j) == pos) and self.tiles[i][
                        j
                    ].value == tile.value:
                        self.tiles[i][j].background_color = "green"
                    elif (
                        i == pos[0]
                        or j == pos[1]
                        or (i // self.bloc_size, j // self.bloc_size)
                        == (pos[0] // self.bloc_size, pos[1] // self.bloc_size)
                    ):
                        self.tiles[i][j].background_color = "gray"
        if tile is not None:
            tile.selected = True
        self.current_tile = tile

    def move_tile(self, event):  # Permet de sélectionner des cases avec les flèches directionnelles
        if self.current_tile is None:
            return
        x, y = self.current_tile.get_pos()
        if event.key == pygame.K_UP and x > 0:
            x -= 1
        elif event.key == pygame.K_DOWN and x < self.size - 1:
            x += 1
        elif event.key == pygame.K_LEFT and y > 0:
            y -= 1
        elif event.key == pygame.K_RIGHT and y < self.size - 1:
            y += 1

        self.select_tile(self.tiles[x][y])

    def enter_char(self, char: str):  # Permet de saisir un caractère dans la case sélectionnée
        if self.is_running():
            if char not in algorithme.get_allowed_characters(self.size):
                return
            for i in range(self.size):
                for j in range(self.size):
                    if self.tiles[i][j].selected and self.init_board[i][j] == "0":
                        temp = [[t.clone() for t in self.tiles[i]] for i in range(len(self.tiles))]
                        self.tiles = temp
                        if self.noteMode:
                            if char != "0":
                                self.tiles[i][j].value = "0"
                                if char in self.tiles[i][j].notes:
                                    self.tiles[i][j].notes.remove(char)
                                else:
                                    self.tiles[i][j].notes.append(char)
                        elif not self.tiles[i][j].valid:
                            self.tiles[i][j].notes.clear()
                            self.tiles[i][j].value = char
                            if self.solution[i][j] == char:
                                self.tiles[i][j].valid = True
                            else:
                                self.tiles[i][j].valid = False
                                self.error_count += 1 if char != "0" else 0
                            self.select_tile(self.tiles[i][j])
                        else:
                            return
                        self.history.append(temp)
                        return

    def save(self):  # Permet de sauvegarder la partie en cours
        with open(f"./grids/{self.name}.sudoku", "w") as f:
            f.writelines(
                [",".join([e for e in line]) + "\n" for line in self.init_board]
            )
            f.write(f"PROGRESS:{self.error_count}\n")
            f.writelines(
                [",".join([tile.value for tile in line]) + "\n" for line in self.history[-1]]
            )
            f.write(f"NOTES:{self.noteMode}\n")
            f.writelines(
                [
                    ",".join(["|".join([note for note in tile.notes]) for tile in line])
                    + "\n"
                    for line in self.tiles
                ]
            )
            f.write(f"TIMER:{self.timer_button.elapsed_time}\n")

    def create_buttons(self, elapsed_time):  # Permet de créer les boutons sur le plateau de jeu
        def note_btn_f():
            if self.play:
                self.noteMode = not self.noteMode
                self.note_button.image = pygame.image.load("./resources/note-button-" + ("on" if self.noteMode else "off") + ".png")

        self.note_button: Button = Button(
            1070,
            90,
            self.screen,
            70,
            70,
            fontColor="#147DE6",
            fontSize=20,
            buttonText="Note",
            onclickFunction=note_btn_f,
            image="./resources/note-button-" + ("on" if self.noteMode else "off") + ".png",
            fillColors={
                "normal": "#ffffff",
                "hover": "#ffffff",
                "pressed": "#ffffff",
            },
            textOffset=(-10, 50)
        )

        def back_board_f():
            if self.play and len(self.history) > 1:
                self.history.pop(-1)
                self.tiles = self.history[-1]

        self.back_button: Button = Button(
            1170,
            90,
            self.screen,
            70,
            70,
            fontColor="#147DE6",
            fontSize=20,
            buttonText="Retour",
            onclickFunction=back_board_f,
            image="./resources/back.png",
            fillColors={
                "normal": "#ffffff",
                "hover": "#ffffff",
                "pressed": "#ffffff",
            },
            textOffset=(0, 51)
        )

        def back_menu_f():
            self.save()
            self.active = False

        self.back_home: Button = Button(
            1050,
            30,
            self.screen,
            200,
            30,
            "#147DE6",
            buttonText="Retour au menu principal",
            fontSize=20,
            onclickFunction=back_menu_f,
        )

        self.timer_button: Timer = Timer(
            100,
            50,
            self.screen,
            100,
            50,
            "black",
            buttonText="",
            fontSize=30,
            start_time=pygame.time.get_ticks(),
            elapsed_time=elapsed_time,
            stop=not self.is_running()
        )

        def pause_f():
            if self.play:
                if self.pause:
                    self.pause_lock = True
                self.pause = not self.pause
                self.pause_button.image = pygame.image.load("./resources/" + ("play" if self.pause else "pause") + "-button.png")
                self.big_play_button.visible = self.pause
                self.select_tile(None)

        self.pause_button: Button = Button(
            130,
            100,
            self.screen,
            32,
            32,
            "black",
            buttonText=None,
            image="./resources/pause-button.png",
            onclickFunction=pause_f
        )

        self.big_play_button: Button = Button(
            self.offset[0] + self.board_size // 2 - 64,
            self.offset[1] + self.board_size // 2 - 64,
            self.screen,
            128,
            128,
            "black",
            buttonText=None,
            image="./resources/play.png",
            fillColors={
                "normal": None,
                "hover": None,
                "pressed": None
            },
            onclickFunction=pause_f,
            visible=False
        )

    def popup(self, message):  # Affiche des messages contextuels
        self.pause = True

        gray_surface = pygame.Surface(
            (self.screen.get_width(), self.screen.get_height())
        )
        gray_surface.set_alpha(200)
        gray_surface.fill((0, 0, 0))
        self.screen.blit(gray_surface, (0, 0))

        message_rect = pygame.Rect(
            self.screen.get_width() // 2 - 200,
            self.screen.get_height() // 2 - 100,
            400,
            200,
        )
        pygame.draw.rect(self.screen, (250, 250, 250), message_rect, border_radius=20)

        font = pygame.font.SysFont("arial", 30)
        font2 = pygame.font.SysFont("arial", 20)
        text = font.render("Partie terminée", True, (0, 0, 0))
        text2 = font2.render(message, True, (186, 186, 186))

        text_rect = text.get_rect(center=message_rect.center)
        text_rect = text_rect.move(0, -35)
        self.screen.blit(text, text_rect)
        text_rect = text.get_rect(center=message_rect.center)
        text_rect = text_rect.move(-10, 5)
        self.screen.blit(text2, text_rect)

        def back_menu_f():
            self.save()
            self.active = False

        self.back_menu: Button = (
            self.back_menu
            if self.back_menu is not None
            else Button(
                message_rect.left + 45,
                message_rect.bottom - 80,
                self.screen,
                155,
                40,
                "white",
                buttonText="Retour à l'accueil",
                fontSize=20,
                onclickFunction=back_menu_f,
                fillColors={
                    "normal": "#0048f9",
                    "hover": "#666666",
                    "pressed": "#0093f9",
                },
                borderRadius=10,
            )
        )

        def generate_menu_f():
            self.save()
            self.active = False
            self.menus[0]._open(self.menus[2])

        self.new_game: Button = (
            self.new_game
            if self.new_game is not None
            else Button(
                message_rect.right - 195,
                message_rect.bottom - 80,
                self.screen,
                145,
                40,
                "black",
                buttonText="Nouvelle partie",
                fontSize=20,
                onclickFunction=generate_menu_f,
                fillColors={
                    "normal": "#ffffff",
                    "hover": "#dadada",
                    "pressed": "#ffffff",
                },
                borderRadius=10,
            )
        )


import discord

from redbot.core import commands, checks
from redbot.core.utils.menus import menu, DEFAULT_CONTROLS

from .core import Core

class Space(Core):
    """Show pics of space."""

    @commands.group()
    @checks.mod_or_permissions(manage_channels=True)
    async def spaceset(self, ctx: commands.Context):
        """Group commands for Space cog settings."""

    @spaceset.command()
    async def autoapod(self, ctx: commands.Context, channel: discord.TextChannel = None):
        """
        Choose if you want to automatically receive \"Astronomy Picture of the Day\" every day.

        Set to actual channel by default. You can also use `[p]spaceset autoapod <channel_name>` if you want to receive APOD in others channels.
        Use the same command to disable it.
        """
        channel = ctx.channel if not channel else channel
        auto_apod = await self.config.channel(channel).auto_apod()
        await self.config.channel(channel).auto_apod.set(not auto_apod)
        msg = (
            "I will now automatically send Astronomy Picture of the Day every day in this channel."
            if not auto_apod
            else "No longer sending Astronomy Picture of the Day every day in this channel."
        )
        await channel.send(msg)
        if not auto_apod:
            data = await self.get_data("https://api.martinebot.com/images/apod")
            apod_text = await self.apod_text(data, channel)
            await self.maybe_send_embed(channel, apod_text)
            await self.config.channel(channel).last_apod_sent.set(data["date"])
        await ctx.tick()

    @commands.command()
    @commands.bot_has_permissions(embed_links=True)
    async def apod(self, ctx: commands.Context):
        """Astronomy Picture of the Day."""
        async with ctx.typing():
            apod_text = await self.apod_text(
                await self.get_data("https://api.martinebot.com/images/apod"),
                ctx,
            )
        return await self.maybe_send_embed(ctx, apod_text)

    @commands.command()
    @commands.bot_has_permissions(embed_links=True)
    async def spacepic(self, ctx: commands.Context, *, query: str):
        """
        Lookup pictures from space!
        Note - Some pictures are from presentations and other educational talks
        """
        async with ctx.typing():
            query = self.escape_query("".join(query))
            space_data = await self.get_space_pic_data(ctx, query)
            if space_data is None:
                await ctx.send(f"Looks like you got lost in space looking for `{query}`")
                return
            if space_data is False:
                return
            if query.lower() == "star wars":
                await ctx.send(self.star_wars_gifs())
                return

            pages = []
            total_pages = len(space_data)  # Get total page count
            for c, i in enumerate(space_data, 1):  # Done this so I could get page count `c`
                space_data_clean = i.replace(" ", "%20")
                embed = discord.Embed(
                    title="Results from space",
                    description=f"Query was `{query}`",
                    color=await ctx.embed_color(),
                )
                embed.set_image(url=space_data_clean)
                embed.set_footer(text=f"Page {c}/{total_pages}")
                # Set a footer to let the user
                # know what page they are in
                pages.append(embed)
                # Added this embed to embed list that the menu will use
        return await menu(ctx, pages, DEFAULT_CONTROLS)

    @commands.command()
    @commands.bot_has_permissions(embed_links=True)
    async def isslocation(self, ctx: commands.Context):
        """Show the Current location of the ISS."""
        async with ctx.typing():
            data = await self.get_data("http://api.open-notify.org/iss-now.json")
            if not data:
                await ctx.send("I can't get the data from the API. Try again later.")
                return

            embed = discord.Embed(
                title="Current location of the ISS",
                description="Latitude and longitude of the ISS",
                color=await ctx.embed_color(),
            )
            embed.add_field(name="Latitude", value=data["iss_position"]["latitude"], inline=True)
            embed.add_field(name="Longitude", value=data["iss_position"]["longitude"], inline=True)
            embed.set_thumbnail(url="https://photos.kstj.us/GrumpyMeanThrasher.jpg")
        return await ctx.send(embed=embed)

    @commands.command()
    @commands.bot_has_permissions(embed_links=True)
    async def astronauts(self, ctx: commands.Context):
        """Show who is currently in space."""
        async with ctx.typing():
            data = await self.get_data("http://api.open-notify.org/astros.json")
            if not data:
                await ctx.send("I can't get the data from the API. Try again later.")
                return

            astrosnauts = []
            for astros in data["people"]:
                astrosnauts.append(astros["name"])

            embed = discord.Embed(title="Who's in space?", color=await ctx.embed_color())
            embed.add_field(name="Current Astronauts in space", value="\n".join(astrosnauts))
        return await ctx.send(embed=embed)


import tensorflow as tf
import tensorlayer as tl
import numpy as np
import scipy
import time
import math
import argparse
import random
import sys
import os

from termcolor import colored, cprint

from model_dual import model_dual as model_net
import dataLoad_dual as dataLoad

from time import gmtime, strftime

import progressbar

from tensorflow.python import debug as tf_debug

parser = argparse.ArgumentParser(description="Run the NN for particle simulation")

parser.add_argument('datapath')
parser.add_argument('outpath')
parser.add_argument('-gpu', '--cuda-gpus')
parser.add_argument('-bs', '--batch-size', type = int, default = 64)
parser.add_argument('-ds', '--delta-step', type = int, default = 1, help = "How many steps ds will the network predict, step i -> step (i+ds), 0 for identity test")
parser.add_argument('-ls', '--latent-step', type = int, default = 1, help = "How many ds will the network simulate in latent space before predicting final result (decoding)")
parser.add_argument('-ss', '--start-step', type = int, default = 0, help = "Which step to start")
parser.add_argument('-conly', '--card-only', dest = 'card_only', action = 'store_const', default = False, const = True, help = "Only predicting card (use density map visualizer instead)")
parser.add_argument('-simit', '--sim-iter', type = int, default = 130, help = "How many steps (= latent steps) to simulate ( total steps = simit * ls * ds )")
parser.add_argument('-vm', '--velocity-multiplier', type = float, default = 1.0, help = "Multiplies the velocity by this factor")
parser.add_argument('-zdim', '--latent-dim', type = int, default = 256, help = "Length of the latent vector")
parser.add_argument('-res', '--res-size', type = int, default = 4, help = "Length of res layers (res block stacks)")
parser.add_argument('-maxpool', '--maxpool', dest = 'combine_method', action='store_const', default = tf.reduce_mean, const = tf.reduce_max, help = "use Max pooling instead of sum up for permutation invariance")
parser.add_argument('-size', '--size', type = int, default = 2560, help = "Total amount of particles we are going to deal with")
parser.add_argument('-vSize', '--voxel-size', type = int, default = 32, help = "Max amount of particles in a voxel")
parser.add_argument('-load', '--load', type = str, default = "None", help = "File to load to continue training")
parser.add_argument('-debug', '--debug', dest = "enable_debug", action = 'store_const', default = False, const = True, help = "Enable debugging")

args = parser.parse_args()

dataLoad.particleCount = args.size
dataLoad.maxParticlePerGrid = args.voxel_size

os.environ["CUDA_VISIBLE_DEVICES"] = args.cuda_gpus

# Create the model
optimizer = tf.train.AdamOptimizer()

# model = model_net(16, args.latent_dim, args.batch_size, optimizer)
model = model_net(args.size, args.voxel_size, args.latent_dim, args.batch_size, optimizer)
model.resSize = args.res_size
model.combine_method = args.combine_method

# Headers
headers = dataLoad.read_file_header(args.datapath)
model.total_world_size = 96.0
model.initial_grid_size = model.total_world_size / 16

model.latent_simulate_steps = args.latent_step
# model.initial_grid_size = model.total_world_size / 4

# model.build_model()

# Build the model
if args.card_only:
    card_prediction, _loss = model.card_only_prediction(args.sim_iter)
else:
    model.build_prediction()

# Create session
sess = tf.Session()

if args.enable_debug:
    sess = tf_debug.LocalCLIDebugWrapperSession(sess)
    sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)

sess.run(tf.local_variables_initializer())
tl.layers.initialize_global_variables(sess)

# Save & Load
saver = tf.train.Saver()

if args.load != "None":
    saver.restore(sess, args.load)
    print("Model restored.")

# variables_names = [v.name for v in tl.layers.get_variables_with_name('', True)]
# values = sess.run(variables_names)
# cprint('Trainable vars', 'red')
# for k, v in zip(variables_names, values):
#     cprint("Variable: " + str(k), 'yellow')
#     cprint("Shape: " + str(v.shape), 'green')
#     #print(v)

batch_idx = 0
current_step = 0

# Prediction
groundTruth_content = dataLoad.read_file(args.datapath, args.delta_step * args.velocity_multiplier)
batch_X = np.zeros((1, args.size, 7))

if args.card_only:
    # 1st batch
    batch_X[0] = groundTruth_content['data'][args.start_step, :, 0:7]
    # batch_X[1] = groundTruth_content['data'][args.start_step, :, 0:7]

    results, loss = sess.run([card_prediction, _loss], feed_dict = {model.ph_X: batch_X})
    # results, loss, vloss, tloss = sess.run([card_prediction, _loss, _valloss, _trainloss], feed_dict = {model.ph_X: batch_X, model.ph_Y: batch_X})
    print("Loss = %f" % (loss))
    np.save(args.outpath, results)
    print('Please run \'python convertNpyToRBin.py %s %s\'' % (args.outpath, args.outpath.split('.')[0] + '.rbin'))
else:
    # Create result file
    content = {}

    content['gridCountX'] = 16
    content['gridCountY'] = 16
    content['gridCountZ'] = 16
    content['gridCount'] = 4096
    content['gridSize'] = 6

    content['worldLength'] = 96
    content['gravity'] = False
    content['boundary'] = False

    content['stepCount'] = args.sim_iter + 1
    content['particleCount'] = np.zeros((content['stepCount'], content['gridCount']), dtype = np.int32)
    content['data'] = np.zeros((content['stepCount'], content['gridCount'], args.voxel_size, 6))

    # 1st batch
    batch_X[0] = groundTruth_content['data'][args.start_step, :, 0:7]

    # Write 1st batch to result
    for p in range(batch_X.shape[1]):
        gridX = batch_X[0, p, 0] // content['gridSize'] + content['gridCountX'] // 2
        gridY = batch_X[0, p, 1] // content['gridSize'] + content['gridCountY'] // 2
        gridZ = batch_X[0, p, 2] // content['gridSize'] + content['gridCountZ'] // 2

        gridHash = int(gridX * content['gridCountY'] * content['gridCountZ'] + gridY * content['gridCountZ'] + gridZ)

        # Translate particles from world space to voxel space
        content['data'][current_step, gridHash, content['particleCount'][current_step, gridHash]] = np.asarray([\
            batch_X[0, p, 0] - (gridX - content['gridCountX'] // 2) * content['gridSize'] - content['gridSize'] / 2,\
            batch_X[0, p, 1] - (gridY - content['gridCountY'] // 2) * content['gridSize'] - content['gridSize'] / 2,\
            batch_X[0, p, 2] - (gridZ - content['gridCountZ'] // 2) * content['gridSize'] - content['gridSize'] / 2,\
            batch_X[0, p, 3],\
            batch_X[0, p, 4],\
            batch_X[0, p, 5]])
        content['particleCount'][current_step, gridHash] += 1

    current_step += 1

    # Simulation loop
    while current_step <= args.sim_iter:

        print("Step %d" % current_step)

        # Feed the data and run the net
        card, data = sess.run([model.predict_cardinality, model.predict_outputs], feed_dict = {model.ph_X: batch_X})

        # Write the data to result
        # The velocity data here was multiplied by ds*vm, and it will be divided back when writing data back to files.
        content['data'][current_step, :, :, :] = data[:, :, 0:6]
        content['particleCount'][current_step, :] = card[:]

        # Prepare next batch
        batch_X = np.zeros((1, args.size, 7))
        particle_idx = 0
        for g in range(content['gridCount']):

            gridPosX = g // (content['gridCountZ'] * content['gridCountX']) * content['gridSize'] - (content['gridCountX'] // 2 * content['gridSize'])
            gridPosY = g % (content['gridCountZ'] * content['gridCountX']) // content['gridCountZ'] * content['gridSize'] - (content['gridCountY'] // 2 * content['gridSize'])
            gridPosZ = g % content['gridCountZ'] * content['gridSize'] - (content['gridCountZ'] // 2 * content['gridSize'])

            for p in range(content['particleCount'][current_step, g]):

                # Translate particles from voxel space to world space
                batch_X[0, particle_idx, 0] = content['data'][current_step, g, p, 0] + gridPosX + (content['gridSize'] / 2) # x
                batch_X[0, particle_idx, 1] = content['data'][current_step, g, p, 1] + gridPosY + (content['gridSize'] / 2) # y
                batch_X[0, particle_idx, 2] = content['data'][current_step, g, p, 2] + gridPosZ + (content['gridSize'] / 2) # z

                batch_X[0, particle_idx, 3:6] = content['data'][current_step, g, p, 3:6]
                batch_X[0, particle_idx, 6] = 1

                particle_idx += 1

        current_step += 1

    # Save the results
    dataLoad.save_file(content, args.outpath, args.delta_step * args.velocity_multiplier)


import urllib.parse

from flask import request

from sqlalchemy import select

from FashionCampus.database import session
from FashionCampus.common import get_image_url
from FashionCampus.model import Product, ProductImage

from FashionCampus.api.blueprints import home

@home.route('/home/banner', methods = ['GET'])
def home_get_banner():
    db = session()

    products = db.execute(select(
        Product.id,
        Product.name,
        ProductImage.path.label('image_path')
    ).join(Product.images.and_(ProductImage.order == 1), isouter = True).where(Product.is_deleted == False).order_by(Product.created_at.desc()).limit(5)).fetchall()

    return {
        'data': [
            {
                'id': str(p.id),
                'image': get_image_url(p.image_path),
                'title': p.name
            } for p in products
        ]
    }, 200


# coding: utf-8

# In[1]:

import numpy as np
import tensorflow as tf

dataPhase = np.load("trainLenetdataPhase.npy")
dataMag = np.load("trainLenetdataMag.npy")
dataY = np.load("trainLenetdataY.npy")

# In[2]:

from keras.models import Sequential
from keras.layers import Convolution2D
model = Sequential()
model.add(Convolution2D(filters=6,kernel_size=(5,5),strides=1,activation='relu',input_shape=(32,32,2)))

# In[3]:

from keras.layers import MaxPooling2D
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
model.add(Convolution2D(filters=16,kernel_size=(5,5),strides=1,activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))

# In[4]:

from keras.layers import Flatten
model.add(Flatten())

# In[5]:

from keras.layers import Dense
model.add(Dense(120,input_shape=(400,),activation='relu'))
model.add(Dense(84,activation='relu'))
model.add(Dense(20,activation='softmax'))

# In[6]:

from keras.optimizers import Adam
optimizer = Adam(lr=0.0001)
model.compile(loss='categorical_crossentropy',optimizer=optimizer,metrics=['accuracy'])

# In[7]:

print(dataMag.shape,dataPhase.shape)
dataIn = np.stack((dataMag,dataPhase),axis=3)
dataIn = np.reshape(dataIn,(dataIn.shape[0],dataIn.shape[1],dataIn.shape[2],dataIn.shape[3]))
print(dataIn.shape)

# In[8]:

model.summary()

# In[9]:

model.fit(x=dataIn,y=dataY,epochs=20,batch_size=32)

# In[10]:

model.save('Lenet_model_with_MagAndPhase.h5')


# Largely stolen from https://github.com/jmoiron/humanize (MIT)

from datetime import datetime, timedelta

def _ngettext(message, plural, num):
    return message if num == 1 else plural

def _now():
    return datetime.now()

def abs_timedelta(delta):
    """Returns an "absolute" value for a timedelta, always representing a
    time distance."""
    if delta.days < 0:
        now = _now()
        return now - (now + delta)
    return delta

def date_and_delta(value):
    """Turn a value into a date and a timedelta which represents how long ago
    it was.  If that's not possible, return (None, value)."""
    now = _now()
    if isinstance(value, datetime):
        date = value
        delta = now - value
    elif isinstance(value, timedelta):
        date = now - value
        delta = value
    else:
        try:
            value = int(value)
            delta = timedelta(seconds=value)
            date = now - delta
        except (ValueError, TypeError):
            return (None, value)
    return date, abs_timedelta(delta)

def naturaldelta(value, months=True):
    """Given a timedelta or a number of seconds, return a natural
    representation of the amount of time elapsed.  This is similar to
    ``naturaltime``, but does not add tense to the result.  If ``months``
    is True, then a number of months (based on 30.5 days) will be used
    for fuzziness between years."""
    date, delta = date_and_delta(value)
    if date is None:
        return value

    use_months = months

    seconds = abs(delta.seconds)
    days = abs(delta.days)
    years = days // 365
    days = days % 365
    months = int(days // 30.5)

    if not years and days < 1:
        if seconds < 60:
            return "a moment"
        elif 60 <= seconds < 120:
            return "a minute"
        elif 120 <= seconds < 3600:
            minutes = seconds // 60
            return _ngettext("%d minute", "%d minutes", minutes) % minutes
        elif 3600 <= seconds < 3600 * 2:
            return "an hour"
        elif 3600 < seconds:
            hours = seconds // 3600
            return _ngettext("%d hour", "%d hours", hours) % hours
    elif years == 0:
        if days == 1:
            return "a day"
        if not use_months:
            return _ngettext("%d day", "%d days", days) % days
        else:
            if not months:
                return _ngettext("%d day", "%d days", days) % days
            elif months == 1:
                return "a month"
            else:
                return _ngettext("%d month", "%d months", months) % months
    elif years == 1:
        if not months and not days:
            return "a year"
        elif not months:
            return _ngettext("1 year, %d day", "1 year, %d days", days) % days
        elif use_months:
            if months == 1:
                return "1 year, 1 month"
            else:
                return (
                    _ngettext("1 year, %d month", "1 year, %d months", months)
                    % months
                )
        else:
            return _ngettext("1 year, %d day", "1 year, %d days", days) % days
    else:
        return _ngettext("%d year", "%d years", years) % years

def naturaltime(value, future=False, months=True):
    """Given a datetime or a number of seconds, return a natural representation
    of that time in a resolution that makes sense.  This is more or less
    compatible with Django's ``naturaltime`` filter.  ``future`` is ignored for
    datetimes, where the tense is always figured out based on the current time.
    If an integer is passed, the return value will be past tense by default,
    unless ``future`` is set to True."""
    now = _now()
    date, delta = date_and_delta(value)
    if date is None:
        return value
    # determine tense by value only if datetime/timedelta were passed
    if isinstance(value, (datetime, timedelta)):
        future = date > now

    ago = "%s from now" if future else "%s ago"
    delta = naturaldelta(delta, months)

    if delta == "a moment":
        return "now"

    return ago % delta

suffixes = ("kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")

def naturalsize(value, format="%.1f"):
    """Format a number of byteslike a human readable filesize (eg. 10 kB) """

    base = 1024
    bytes = float(value)

    if bytes == 1:
        return "1 Byte"
    elif bytes < base:
        return "%d Bytes" % bytes

    for i, s in enumerate(suffixes):
        unit = base ** (i + 2)
        if bytes < unit:
            return (format + " %s") % ((base * bytes / unit), s)
    return (format + " %s") % ((base * bytes / unit), s)


from indexes.single_indexes.hnsw_hnswlib import HnswHnswlib
from indexes.testers.TimeStats import BuildTimeStats, QueryTimeStats
from indexes.utils.dataset import BasicDataset
from indexes.utils.distance_function import l2distance
import Index

from sklearn.cluster import KMeans
from overrides import overrides
from typing import List, Tuple

from time import time

class KMeansIndex(Index.Index):
    def __init__(self, name, max_elements, dimensions, metric='l2', distance_function=None, num_threads=-1,
                 num_partitions=4, num_partitions_to_search=2, ef_construction=1000, ef=1000, m=64):
        super().__init__(name, max_elements, dimensions, metric, distance_function, num_threads)
        self.num_partitions = num_partitions
        self.num_partitions_to_search = num_partitions_to_search
        self.ef_construction = ef_construction
        self.ef = ef
        self.m = m
        self.single_partitions_size = 0
        self.mapping = None
        self.centers = None

    def partition(self, dataset):
        data = dataset.get_data()
        k_means = KMeans(n_clusters=self.num_partitions, random_state=0)
        k_means.fit(data)
        labels = k_means.labels_
        self.centers = k_means.cluster_centers_
        self.mapping = dict([(i, []) for i in range(self.num_partitions)])
        p_indexes = [[] for _ in range(self.num_partitions)]
        for i in range(labels.shape[0]):
            p_indexes[labels[i]].append(i)
            self.mapping[labels[i]].append(i)

        partitions = [data[p_indexes[idx]] for idx in range(self.num_partitions)]

        return partitions

    @overrides
    def build(self, dataset) -> BuildTimeStats:
        partition_start_time = time()
        partitions = self.partition(dataset)
        elapsed_partition_time = time() - partition_start_time

        build_start_time = time()
        datasets = [BasicDataset(f'partition{idx}', '') for idx in range(self.num_partitions)]
        for idx, dataset in enumerate(datasets):
            dataset.load_dataset_from_numpy(partitions[idx])
        self.index = [HnswHnswlib('hnsw_hnswlib', datasets[idx].get_size(), datasets[idx].get_dimensions(), self.metric,
                                  self.distance_function, self.num_threads, self.ef_construction, self.ef, self.m) for
                      idx in range(self.num_partitions)]
        for idx, index in enumerate(self.index):
            index.build(datasets[idx])
        elapsed_build_time = time() - build_start_time
        return BuildTimeStats(elapsed_partition_time, elapsed_build_time)

    @overrides
    def search(self, query, k=5) -> Tuple[List[List[Tuple[float, int]]], QueryTimeStats, float]:
        avg_ind = self.num_partitions_to_search
        query_start_time = time()
        index_list = [
            list(sorted([(l2distance(q, self.centers[i]), i) for i in range(len(self.index))], key=lambda x: x[0]))[
            :self.num_partitions_to_search] for q in query]
        results = [[self.index[idx].search([query[q_id]], k)[0] for _, idx in id_list] for q_id, id_list in
                   enumerate(index_list)]
        elapsed_query_time = time() - query_start_time

        merge_start_time = time()
        # Fix indexes

        results = [[[(t[0], self.mapping[index_list[q_id][ind_id][1]][t[1]]) for t in index[0]] for ind_id, index in enumerate(res)]
                   for q_id, res in enumerate(results)]

        result = [list(sorted([t for index in res for t in index], key=lambda x: x[0]))[:k] for res in results]

        elapsed_merge_time = time() - merge_start_time
        return result, QueryTimeStats(elapsed_query_time, elapsed_merge_time, len(query)), avg_ind


from typing import List

class Contact():

    all_contacts: List["Contact"] = []

    def __init__(self,nombre: str,email: str):

        self.nombre = nombre
        self.email = email

        Contact.all_contacts.append(self)

    def __repr__(self)->str:
             return (f"ClassName: {self.__class__.__name__}  Nombre:{self.nombre}  Email:{self.email}")

class Supplier(Contact):

    def ventas(self, venta:"Order")->None:

        print(f'La venta fue pendida al contacto supplier {self.nombre}')

c_1 = Contact("Dusty", "dusty@example.com")
c_2 = Contact("Steve", "steve@itmaybeahack.com")
c = Contact("Some Body", "somebody@example.net")
s = Supplier("Sup Plier", "supplier@example.net")
print(Contact.all_contacts)

print(s.ventas('Holaperro'))


#!/usr/bin/env python3

from pynput import keyboard as kb
import rospy
from std_msgs.msg import String

rospy.init_node("teclas")
pub = rospy.Publisher("/voice_ui", String, queue_size=10)

def callback(tecla):
    s = String()

    print("Se ha pulsado la tecla ")

    if(str(tecla) == "'r'"):
        print("R")
        s.data = "sal"

    elif(str(tecla) == "'g'"):
        print("G")
        s.data = "pimienta"

    elif(str(tecla) == "'b'"):
        print("B")
        s.data = "azúcar"

    pub.publish(s)

# Main
if __name__ == '__main__':

    print("------- Start --------")
    kb.Listener(callback).run()