"""
A class for managing a list of image pairs stored on disk which also handles keeping track of processed and raw versions on each image
"""

from base import *
from .common import *

from pysideline import *

from PySide import QtGui, QtCore
from PySide.QtCore import Qt

import uuid
import os
import re

from . import processing

class ThumbnailJob(processing.ProcessingJob):

	priority = 100

	def __init__(self,app,image,cameraIndex,size,pm=None,withPreview=False):
		self.app = app
		self.image = image
		self.cameraIndex = cameraIndex
		self.size = size
		self.withPreview = withPreview
		self.pm = pm

	def execute(self):

		if self.image[self.cameraIndex].thumbnail.exists():
			self.scaledPM = QtGui.QPixmap()
			self.scaledPM.load(self.image[self.cameraIndex].thumbnail.getFilePath())
			return

		if not self.pm:
			self.pm = QtGui.QPixmap(self.image[self.cameraIndex].raw.getFilePath())
		s = self.pm.size()
		s.scale(self.size[0], self.size[1],Qt.KeepAspectRatio)
		self.scaledPM = self.pm.scaled(s,transformMode=Qt.SmoothTransformation)
		self.scaledPM.save(self.image[self.cameraIndex].thumbnail.getFilePath())

	def oncompletion(self):
		self.image.thumbnail.updateImage(self.scaledPM,cameraIndex=self.cameraIndex)
		if self.withPreview:
			preview = self.app.previews[self.cameraIndex]
			preview.loadFromData('raw',self.pm)

class CapturedImageFile(object):
	"""
	A basic image file object supporting the simple manipulations required for files in the image list

	@ivar path: the path to the directory in which the image is stored
	@ivar filename: the currently configured filename for the image (excluding path)
	"""

	def __init__(self,path,filename):
		self.path = path
		self.filename = filename
		self.ext = os.path.splitext(filename)[-1]

	def write(self,data):
		f = open(self.getFilePath(),'wb')
		f.write(data)
		f.close()

	def delete(self):
		if self.exists():
			os.remove(self.getFilePath())

	def rename(self,filename):
		filename = os.path.splitext(filename)[0] + self.ext
		exists = self.exists()
		oldFilePath = self.getFilePath()
		self.filename = filename
		newFilePath = self.getFilePath()
		if newFilePath == oldFilePath:
			return
		if exists:
			os.rename(oldFilePath,newFilePath)

	def exists(self):
		return os.path.exists(self.getFilePath())

	def getFilePath(self):
		return os.path.join(self.path,self.filename)

class CapturedImage(object):
	"""
	An image that can (optionally) have raw and processed versions stored on disk

	@ivar raw: a L{CapturedImageFile} object for the raw image
	@ivar processed: a L{CapturedImageFile} object for the raw image (the object if present even if no such file is ever created)
	"""

	def __init__(self,path,filename):
		self.raw = CapturedImageFile(path,filename)
		self.processed = CapturedImageFile(self.calcProcessedPath(path),filename)
		self.thumbnail = CapturedImageFile(self.calcThumbnailPath(path),filename)
		self.auxiliary = []

	@property
	def files(self):
		return [self.raw,self.processed,self.thumbnail] + self.auxiliary

	def delete(self):
		for f in self.files:
			f.delete()

	def rename(self,filename):
		for f in self.files:
			f.rename(filename)

	def addAuxFromFile(self,old):
		ext = os.path.splitext(old)[-1]
		rawBase = os.path.splitext(self.raw.getFilePath())[0]
		new = rawBase + ext
		path,filename = os.path.split(new)
		if os.path.exists(new):
			raise Exception('Cannot add new auxiliary file %s from %s because the new filename already exists'%(new,old))
		os.rename(old,new)
		self.auxiliary.append(CapturedImageFile(path,filename))

	def calcProcessedPath(self,path):
		return os.path.join(path,'processed')

	def calcThumbnailPath(self,path):
		return os.path.join(path,'thumbnails')

class CapturedImageBase(object):
	"""
	Common base for both kinds of image pair (the one that's actually a pair and the one that only looks like a pair!)
	"""

class CapturedImagePair(CapturedImageBase):
	"""
	An PAIR of images (left and right) that can (optionally) also have raw and processed versions stored on disk

	@ivar left: a L{CapturedImageFile} object for the left image
	@ivar right: a L{CapturedImageFile} object for the right image (the object if present even if no such file is ever created)
	"""

	def __init__(self,path,filename):
		self.uid = uuid.uuid1().hex
		base,ext = os.path.splitext(filename)
		self.left = CapturedImage(path,'%sL%s'%(base,ext))
		self.right = CapturedImage(path,'%sR%s'%(base,ext))

	def __getitem__(self,k):
		if k == 1:
			return self.left
		elif k == 2:
			return self.right
		else:
			raise KeyError(k)

	def delete(self):
		self.left.delete()
		self.right.delete()

	def rename(self,filename):
		base,ext = os.path.splitext(filename)
		self.left.rename('%sL%s'%(base,ext))
		self.right.rename('%sR%s'%(base,ext))

class CapturedImageSingle(CapturedImageBase):
	"""
	An SINGLE image to use in place of pairs where pairs are not wanted

	@ivar image: a L{CapturedImageFile} object for the underlying images
	"""

	def __init__(self,path,filename):
		self.uid = uuid.uuid1().hex
		self.image = CapturedImage(path,filename)

	def __getitem__(self,k):
		if k == 1:
			return self.image
		else:
			raise KeyError(k)

	def delete(self):
		self.image.delete()

	def rename(self,filename):
		self.image.rename(filename)

class CapturedImageManager(object):

	PAT_FILE_DOUBLE = re.compile(r'(page[0-9]+)([lr])\.(jpg|jpeg)',re.I)
	PAT_FILE_SINGLE = re.compile(r'(page[0-9]+)()\.(jpg|jpeg)',re.I)

	sideToCameraIndex = {'l':1,'L':1,'R':2,'r':2,'':1,None:1}

	def __init__(self,path,view,solo=False):
		self.path = path
		self._images = []
		self.view = view
		self.solo = solo
		self.selected = None

		if self.solo:
			self.cameraIndices = [1]
		else:
			self.cameraIndices = [1,2]

		pPath = os.path.join(self.path,'processed')
		if not os.path.exists(pPath):
			os.mkdir(pPath)

		tPath = os.path.join(self.path,'thumbnails')
		if not os.path.exists(tPath):
			os.mkdir(tPath)

	def fillFromDirectory(self,progressCallback=None):

		names = os.listdir(self.path)
		names.sort()

		if self.solo:
			pat = self.PAT_FILE_SINGLE
		else:
			pat = self.PAT_FILE_DOUBLE

		names = [name for name in names if pat.match(name)]
		total = len(names)
		done = 0
		for name in names:
			m = pat.match(name)
			side = m.group(2)
			baseName = '%s.%s'%(m.group(1),m.group(3))
			self.addFromExistingLocalFile(filename=name,baseFilename=baseName,cameraIndex=self.sideToCameraIndex[side])
			done += 1
			progressCallback(total,done)
		self.renameChanged()

	def new(self,filename):
		if self.solo:
			image = CapturedImageSingle(path=self.path,filename=filename)
		else:
			image = CapturedImagePair(path=self.path,filename=filename)
		self._images.append(image)
		image.thumbnail = self.view.new(uid=image.uid)
		image.thumbnail.image = image
		image.thumbnail.relabel()
		return image

	def addFromData(self,data=None,pm=None,cameraIndex=1,withPreview=False):

		if pm is None:
			pm = QtGui.QPixmap()
			pm.loadFromData(data)

		ndx = 0
		for ndx,image in enumerate(self._images):
			if not image[cameraIndex].raw.exists():
				break
		else:
			filename = self.calcBaseFilename(len(self._images))
			image = self.new(filename)

		pm.save(image[cameraIndex].raw.getFilePath())

		self.view.app.processingQueue.put(
			ThumbnailJob(
				app = self.view.app,
				image = image,
				cameraIndex = cameraIndex,
				size = (self.view.thumbnailWidth,self.view.thumbnailHeight),
				pm = pm,
				withPreview = withPreview,
			)
		)

		return image

	def addFromExistingLocalFile(self,filename,baseFilename,cameraIndex=1,withPreview=False):

		filepath = os.path.join(self.path,filename)

		for image in self._images:
			if not image[cameraIndex].raw.exists():
				break
			if image[cameraIndex].raw.filename == filename:
				break
		else:
			image = self.new(baseFilename)

		self.view.app.processingQueue.put(
			ThumbnailJob(
				app = self.view.app,
				image = image,
				cameraIndex = cameraIndex,
				size = (self.view.thumbnailWidth,self.view.thumbnailHeight),
				withPreview = withPreview
			)
		)

		return image

	def normalise(self,v):
		if isinstance(v,CapturedImageBase):
			image = v
			index = self._images.index(v)
		else:
			index = v
			image = self._images[v]
		return index,image

	def move(self,fromLocation,toLocation):
		fromIndex,fromImage = self.normalise(fromLocation)
		toIndex,toImage = self.normalise(toLocation)
		del(self._images[fromIndex])
		self._images.insert(toIndex,fromImage)
		self.view.move(fromImage.thumbnail,toIndex)
		self.renameChanged()

	def renameChanged(self):
		changed = []
		for ndx,image in enumerate(self._images):
			for cameraIndex in self.cameraIndices:
				if image[cameraIndex].raw.filename and image[cameraIndex].raw.filename != self.calcFilename(ndx,cameraIndex=cameraIndex):
					changed.append(image)
					break
		for ndx,image in enumerate(changed):
			image.rename('temp%04d.jpg'%(ndx+1))
		for ndx,image in enumerate(changed):
			image.thumbnail.relabel()
			for cameraIndex in self.cameraIndices:
				image[cameraIndex].rename(self.calcFilename(self._images.index(image),cameraIndex=cameraIndex))

	def delete(self,fromLocation):
		index,image = self.normalise(fromLocation)
		del(self._images[index])
		image.delete()
		self.view.remove(image.thumbnail)
		for ndx,i in enumerate(self._images[index:]):
			i.rename(self.calcBaseFilename(ndx+index))
			i.thumbnail.relabel()

	def select(self,image):
		self.selected = image
		if image is None:
			for cameraIndex in self.view.app.cameraIndices:
				self.view.app.previews[cameraIndex].clear()
		else:
			self.view.app.processingQueue.put(processing.ImageLoadJob(self.view.app,image))

	def calcFilename(self,ndx,cameraIndex=1):
		if self.solo:
			assert cameraIndex == 1
			return 'page%04d.jpg'%(ndx+1)
		else:
			assert cameraIndex in (1,2)
			if cameraIndex == 1:
				return 'page%04dL.jpg'%(ndx+1)
			else:
				return 'page%04dR.jpg'%(ndx+1)

	def calcBaseFilename(self,ndx):
		return 'page%04d.jpg'%(ndx+1)

	def getLabel(self,location):
		index,image = self.normalise(location)
		return 'page %d'%(index + 1)


#https://leetcode.com/problems/frequency-of-the-most-frequent-element/

class Solution:
    def maxFrequency(self, nums, k):
        nums.sort(reverse=True)
        n = len(nums)
        l, r = 0, 0
        s = 0
        while r < n:
            s += nums[l] - nums[r]
            if s <= k:
                r += 1
            else:
                s -= (nums[l] - nums[l + 1]) * (r - l)
                l += 1
                r += 1

        return r - l

a = Solution()
print(a.maxFrequency([1,2,4], 5))


list = [1,2,3,4,6,7,9,9,9,8,8,8]
i = 0
while i< len(list)-1:
    j = i+1
    if list[i]%2 != 0 and list[j]%2 == 0:
        list[i],list[j] = list[j],list[i]
        i = j+1

    elif list[i]%2 == 0 and list[j]%2 != 0:
        i = j + 1

    elif list[i]%2 == 0 and list[j]%2 == 0:
        for k in range(j+1,len(list)):
            if list[k]%2 != 0:
                list[j],list[k] = list[k],list[j]
                i = j+1
                break
    else:
        for k in range(j + 1, len(list)):
            if list[k]%2 == 0:
                list[i],list[k] = list[k],list[i]
                i = j+1
                break

print(list)


# -*- coding: utf-8 -*-
# @Time    : 2022/9/1
# @Author  : rickHan
# @Software: PyCharm
# @File    : total.py
import os
def getfiles():
    filenames=os.listdir(r'./txtLabel1/test')
    print(filenames)
    total = 0
    for filename in filenames:
        with open('./txtLabel/test.txt','a+') as f:
            filename = filename[:-4]
            f.write(filename + '\n')
            total = total + 1
            print(total)
            f.close()

if __name__ == '__main__':
    getfiles()


budget = float(input())
price_for_kilo = float(input())
pack_eggs = price_for_kilo * 0.75
litre_milk = (price_for_kilo * 1.25)/4
one_brad = price_for_kilo + pack_eggs + litre_milk
sum_sum = 0
bread = 0
color_eggs = 0
while sum_sum <= budget and (one_brad + sum_sum) <= budget:
    color_eggs += 3
    bread += 1
    if bread % 3 == 0:
        color_eggs -= (bread - 2)
    sum_sum += price_for_kilo + pack_eggs + litre_milk
print(f'You made {bread} loaves of Easter bread! Now you have '
      f'{color_eggs} eggs and {(budget - sum_sum):.2f}BGN left.')

SoftUni/Python fundamentals/Basic Syntax, Conditional Statements and Loops - Exercise/09. Easter Bread.py


import numpy as np
import gymnasium
from gymnasium import spaces
import math

class GridWorldEnv(gymnasium.Env):

    def __init__(self):
        self.observation_space  = spaces.Box(low=np.array([0]), high = np.array([100]), dtype=np.float64)
        self.action_space = spaces.Box(low = np.array([0]), high=np.array([2]), dtype=np.int16)

       # Initial conditions
        self.X0 = 0.3e5  # cell/ml
        self.S0 = 0.2 # g/L
        self.E0 = 0.0 # U/L
        # Process conditions
        self.T = np.random.randint(low=32, high=35) #'C
        self.A = 250 # RPM

        # Substrate addition : add 0.05 evert 24 hours
        self.sadd = 0.05
        self.sadd_t = 5 # add every 5 hours

        #model parameters
        self.Ks = 0.1
        self.C = 0.000001

        self.MuE = .00000001

        self.MuD = 0

        self.del_t = 0.1
        self.sadd_s = self.sadd_t/self.del_t
        self.t_end = 24*7
        self.tvec = np.arange(0, self.t_end, self.del_t)
        self.ns = len(self.tvec)

        # X S E delE delX
        self.D = np.zeros((self.ns+1, 5))

        self.D[0][0] = self.X0
        self.D[0][1] = self.S0
        self.D[0][2] = self.E0
        self.i = 0

        self.media = 10 # Liters
        self.max_capacity = 50 # Liters
        self.media_added_in_liters = self.sadd/self.S0

    def reset(self, seed=None, options=None):
        # We need the following line to seed self.np_random
        super().reset(seed=seed)
       # Initial conditions
        self.X0 = 0.3e5  # cell/ml
        self.S0 = 0.2 # g/L
        self.E0 = 0.0 # U/L
        # Process conditions
        self.T = np.random.randint(low=32, high=35) #'C
        self.A = 250 # RPM

        # Substrate addition : add 0.05 evert 24 hours
        self.sadd = 0.05
        self.sadd_t = 5 # add every 5 hours

        #model parameters
        self.Ks = 0.1
        self.C = 0.000001

        self.MuE = .00000001

        self.MuD = 0

        self.del_t = 0.1
        self.sadd_s = self.sadd_t/self.del_t
        self.t_end = 24*7
        self.tvec = np.arange(0, self.t_end, self.del_t)
        self.ns = len(self.tvec)

        # X S E delE delX
        self.D = np.zeros((self.ns+1, 5))

        self.D[0][0] = self.X0
        self.D[0][1] = self.S0
        self.D[0][2] = self.E0

        self.media = 10 # Liters

        # Reset makes    the enzyme concetration 0
        observation = np.array([self.E0],dtype=float)
        info = {}
        self.i = 0
        with open("plot.txt",'w') as f:
            f.truncate(0)
        return observation,info

    def step(self, action):
        if self.i == int(self.t_end/self.del_t):
            terminate = True
            return np.array([self.D[self.i][2]]), 0, terminate, False, {}
        elif self.media >= self.max_capacity:
            terminate = True
            return np.array([self.D[self.i][2]]), 0, terminate, False, {}
        else:
            terminate = False
            action = math.ceil(action[0])
            initial_cordinates = [self.i, self.D[self.i][2]]
            if action == 2:
                self.T -= 0.1
            else:
                self.T += action/10

            MuX =  0.1*(math.exp(-((self.T - 37)**2)/5**2))

            dXdt = (MuX *self.D[self.i][1]) / (self.Ks + self.D[self.i][1]) * self.D[self.i][0]

            dSdt = ( -self.C * MuX*self.D[self.i][1]/(self.Ks+self.D[self.i][1]) * self.D[self.i][0] )

            delX = dXdt * self.del_t
            delS = dSdt * self.del_t

            # Change in cells
            self.D[self.i+1][4] = delX

            # time of cell death
            tcd = 48
            tcd_s = math.floor(tcd/self.del_t)
            cellsub = 0

            if self.i+1 == tcd_s:
                cellsub = self.X0 # seeded cells die on this step

            elif self.i+1 > tcd_s :
                cellsub = self.D[(self.i+1)-tcd][4]

            # Update cells
            self.D[self.i+1][0] = self.D[self.i][0] + delX - cellsub

            # Update substrate
            if self.i % self.sadd_s == 0:
                delS = delS + self.sadd
                self.media += self.media_added_in_liters

            self.D[self.i+1][1] = self.D[self.i][1] + delS

            # Enzyme determination

            delE = self.MuE * dXdt*self.del_t # new enzyme from fresh cells

            step = self.i
            nts = 1

            t_ev = 20 # hours cells produce enzyme with linear depletion
            s_ev = 20/self.del_t # Timesteps cells can make enzyme

            for j in range(0,int(s_ev)):
                ts = self.i - j
                if ts < 1 :
                    break
                else:
                    delE = delE + (- (self.MuE/s_ev)*j+self.MuE) * self.D[ts][4]

            # Change in enzyme
            self.D[self.i+1][3] = delE

            #Update enzyme variable
            self.D[self.i+1][2] = self.D[self.i][2] + delE

            # Degradation of enzymes
            EDT = 72

            if self.i+1>(EDT/self.del_t+1):
                self.D[self.i+1][2] = self.D[self.i+1][2] - self.D[self.i+1 - int(EDT/self.del_t)][3]

            final_cordinates = [self.i+1, self.D[self.i+1][2]]

            slope = (final_cordinates[1] - initial_cordinates[1]) / (final_cordinates[0] - initial_cordinates[0])

            if slope < 0:
                reward = 10 * slope
                terminate = True
            else:
                slope = 100 * slope
                reward = 10 + slope

            with open('plot.txt','a') as plotting_file:
                plotting_file.write(f"{self.tvec[self.i]},{self.D[self.i][0]/1e6},{self.D[self.i][1]},{self.D[self.i][2]}\n")
            self.i += 1

            return np.array([self.D[self.i][2]]), reward, terminate, False, {}


# -*- coding: utf-8 -*-
"""
Created on Thu Dec 15 23:53:25 2022

@author: admin
"""

x=(input("enter the name: "))
y=len(x)
for i in range(y-1,-1,-1):
    print(x[i],end="")


"""
label encoder
"""
import pandas as pd
from sklearn.preprocessing import LabelEncoder

base = pd.read_csv('../data/kaggle_car/autos_tratado_sem_valores_null.csv', encoding='ISO-8859-1') # esse encod é pq tem acentos

"""
 0   price                359291 non-null  int64
 1   abtest               359291 non-null  object 0
 2   vehicleType          359291 non-null  object 1
 3   yearOfRegistration   359291 non-null  int64  2
 4   gearbox              359291 non-null  object 3
 5   powerPS              359291 non-null  int64  4
 6   model                359291 non-null  object 5
 7   kilometer            359291 non-null  int64  6
 8   monthOfRegistration  359291 non-null  int64  7
 9   fuelType             359291 non-null  object 8
 10  brand                359291 non-null  object 9
 11  notRepairedDamage    359291 non-null  object 10
"""

# a coluna 0 é o preço então vamos usar da coluna 1 a 11

previsores = base.iloc[:, 1:13].values # caracteristicas
preco_real = base.iloc[:, 0].values # respostas

"""
o LabelEncoder será usado para transformar texto em numeros para nossa classificação
"""

labelencoder_previsores = LabelEncoder()

# todas as colunas como object listadas do base.info()
print('ANTES\n',previsores[0])
previsores[:,0] = labelencoder_previsores.fit_transform(previsores[:,0])
previsores[:,1] = labelencoder_previsores.fit_transform(previsores[:,1])
previsores[:,3] = labelencoder_previsores.fit_transform(previsores[:,3])
previsores[:,5] = labelencoder_previsores.fit_transform(previsores[:,5])
previsores[:,8] = labelencoder_previsores.fit_transform(previsores[:,8])
previsores[:,9] = labelencoder_previsores.fit_transform(previsores[:,9])
previsores[:,10] = labelencoder_previsores.fit_transform(previsores[:,10])

print('DEPOIS\n',previsores[0])


class WebDevelpoment:
 course="Web Developement"
 def __init__(self):
  WebDevelpoment.gravity=9.8

 def static_variable(self):
  WebDevelpoment.programming="Python"

#Creating a Constructor
s1=WebDevelpoment()

#Creating Static variables outside the class
WebDevelpoment.pi=3.14

#Accessing the static variables outside the class
print(WebDevelpoment.__dict__)

#Fetching the instance Variables:
print(s1.__dict__)

#calling the instance Method:
s1.static_variable()
print(WebDevelpoment.__dict__)

Week 10/30-11-21/creating a static variable inside the constructor.py


#encoding: utf-8
'''----------------------------------------------------------------------
| Tarefa 4 - Implemente o Jogo da velha utilizando o algoritmo minimax	|
| 																		|
| Renan Mateus Bernardo Nascimento										|
| Vinícius Magalhães D'Assunção											|
----------------------------------------------------------------------'''

class Tabuleiro:
	# Atributos estáticos
	BOLA 	= 'o'
	XIS 	= 'x'
	VAZIO 	= ' '
	EMPATE 	= '='

	def __init__(self, tamanho):
		# Tamanho do tabuleiro
		self.tamanho 	= tamanho
		# Possui a sequência que faz bola ser vencedor
		self.array_bola = self.tamanho * [self.BOLA]
		# Possui a sequência que faz xis ser vencedor
		self.array_xis 	= self.tamanho * [self.XIS]
		# Array 3x3 para representar o tabuleiro
		self.tabuleiro = [ self.tamanho * [self.VAZIO] for i in range(0, self.tamanho) ]

	# Colocar vazio em todas as posições do tabuleiro
	def limpaTabuleiro(self):
		self.tabuleiro = [ self.tamanho * [self.VAZIO] for i in range(0, self.tamanho) ]

	def marcaBola(self, x, y):
		self.tabuleiro[x][y] = self.BOLA

	def marcaXis(self, x, y):
		self.tabuleiro[x][y] = self.XIS

	def verificaLinha(self):
		for i in range(0, self.tamanho):
			if self.tabuleiro[i] == self.array_bola:
				return self.BOLA
			if self.tabuleiro[i] == self.array_xis:
				return self.XIS
		return False

	def verificaColuna(self):
		for j in range(0, self.tamanho):
			if [ self.tabuleiro[i][j] for i in range(0, self.tamanho) ] == self.array_bola:
				return self.BOLA
			if [ self.tabuleiro[i][j] for i in range(0, self.tamanho) ] == self.array_xis:
				return self.XIS
		return False

	def verificaDiagonal(self):
		# Verifica diagonal principal
		if [ self.tabuleiro[i][i] for i in range(0, self.tamanho) ] == self.array_bola:
			return self.BOLA
		if [ self.tabuleiro[i][i] for i in range(0, self.tamanho) ] == self.array_xis:
			return self.XIS

		# Verifica diagonal secundária
		t = self.tamanho - 1
		if [ self.tabuleiro[i][t - i] for i in range(0, self.tamanho) ] == self.array_bola:
			return self.BOLA
		if [ self.tabuleiro[i][t - i] for i in range(0, self.tamanho) ] == self.array_xis:
			return self.XIS

		return False

	def verificaEmpate(self):
		for i in range(0, self.tamanho):
			for j in range(0, self.tamanho):
				if self.tabuleiro[i][j] == self.VAZIO:
					return False
		return self.EMPATE

	'''
		Retorna o jogador vencedor ou False se não houve ganhador
	'''
	def ganhou(self):
		ganhouLinha = self.verificaLinha()
		if ganhouLinha:
			return ganhouLinha
		ganhouColuna = self.verificaColuna()
		if ganhouColuna:
			return ganhouColuna
		ganhouDiagonal = self.verificaDiagonal()
		if ganhouDiagonal:
			return ganhouDiagonal
		empate = self.verificaEmpate()
		return empate

	def imprimirTabuleiro(self):
		print()
		for i in range(0, self.tamanho):
			print(' ', end='')
			for j in range(0, self.tamanho):
				print(self.tabuleiro[i][j], end='')
				if j < self.tamanho - 1:
					print(' | ', end='')
			if i < self.tamanho - 1:
				print('\n' + (3 * self.tamanho + self.tamanho - 1) * '-')
		print(' ')


import os
from os import path
from PIL import Image
import numpy as np
import json
from torchvision.transforms import ToTensor
from torch.utils.data import Dataset
from tqdm import tqdm
import pickle
import torch

torch.set_default_dtype(torch.float64)

class PlanarDatasetN(Dataset):
    width = 40
    height = 40
    action_dim = 2

    def __init__(self, dir):
        self.dir = dir
        self.n_steps = None
        with open(path.join(dir, 'data.json')) as f:
            self._data = json.load(f)
        self._process()

    def __len__(self):
        return len(self._data['samples'])

    def __getitem__(self, index):
        return self._processed[index]

    @staticmethod
    def _process_image(img):
        return ToTensor()((img.convert('L').
                           resize((PlanarDataset.width,
                                   PlanarDataset.height))))

    def _process(self):
        preprocessed_file = os.path.join(self.dir, 'processed.pkl')
        if True or not os.path.exists(preprocessed_file):
            processed = []
            self.n_steps = None
            for sample in tqdm(self._data['samples'], desc='processing data'):
                obs0 = Image.open(os.path.join(self.dir, sample['observation_files'][0]))

                processed_sample = [self._process_image(obs0)]
                n_steps = 0
                for obs_file, control in zip(sample['observation_files'][1:], sample['controls']):
                    obs = Image.open(os.path.join(self.dir, obs_file))
                    processed_sample += [np.asarray(control), self._process_image(obs)]
                    n_steps += 1

                if self.n_steps is None:
                    self.n_steps = n_steps
                assert self.n_steps == n_steps
                processed.append(tuple(processed_sample))

            with open(preprocessed_file, 'wb') as f:
                pickle.dump(processed, f)
            self._processed = processed
        else:
            with open(preprocessed_file, 'rb') as f:
                self._processed = pickle.load(f)


from sklearn.feature_extraction.text import TfidfVectorizer
import nltk
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('wordnet')
nltk.download('omw-1.4')
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.preprocessing import MinMaxScaler
import numpy as np
from nltk.stem import PorterStemmer
import nltk
from nltk.stem import 	WordNetLemmatizer

class textProcessing:

  def __init__(self,articles=[]):
    self.listStopWords=stopwords.words('english')
    self.garbage=['This is an open access article, free of all copyright, and may be freely reproduced, distributed, transmitted, modified, built upon, or otherwise used by anyone for any lawful purpose','The work is made available under the Creative Commons CC0 public domain dedication.','Elsevier B.V.','	©','Elsevier LtdA','by the authors. Licensee MDPI, Basel, Switzerland.Abstract: ','Springer Nature Singapore Pte Ltd. ']

    if(len(articles)!=0):
      self.size=len(articles)
      self.listArticles=articles
      self.createAbstractList()

  def createAbstractList(self):
    self.listAbstracts=[]
    for article in self.listArticles:
      keywords=""
      for keyword in article.keywords:
        keywords=keywords+" "+keyword
      self.listAbstracts.append(self.cleanAbstract(article.abstract+" "+article.title+" "+keywords))

  def cleanAbstract(self,abstract):
    for item in self.garbage:
      abstract=abstract.replace(item,'')
    abstract=abstract.lower()
    abstract_tokens=word_tokenize(abstract)
    abstract_without_sw = [word for word in abstract_tokens if not word in self.listStopWords]
    abstract_without_sw = [word for word in abstract_without_sw if word.isalpha()]
    ps = PorterStemmer()
    abstract_without_sw = [ps.stem(word) for word in abstract_without_sw]
    # wordnet_lemmatizer = WordNetLemmatizer()
    # abstract_without_sw=[wordnet_lemmatizer.lemmatize(word) for word in abstract_without_sw]
    abstract=(" ").join(abstract_without_sw)
    return abstract
  def preProcesingListData(self, listData):
    newData=[]
    for item in listData:
      newData.append(self.cleanAbstract(item))
    return newData
  def cleanAbstract2(self,abstract):
    for item in self.garbage:
      abstract=abstract.replace(item,'')
    abstract=abstract.lower()
    abstract_tokens=word_tokenize(abstract)
    abstract_without_sw = [word for word in abstract_tokens if not word in self.listStopWords]
    abstract_without_sw = [word for word in abstract_without_sw if word.isalpha()]
    abstract=(" ").join(abstract_without_sw)
    return abstract

  def createTfIDF(self):
    vectorizer = TfidfVectorizer(ngram_range=(2, 2), norm=None)
    vectors = vectorizer.fit_transform(self.listAbstracts)
    self.dense=vectors.todense()
  def createTfIDF(self, data):
    self.vectorizer = TfidfVectorizer(ngram_range=(1, 1),norm=None)
    return self.vectorizer.fit_transform(data)

  def tfidf(self,data,ngram_range=(1, 3)):
    min=int(0.01*len(data))
    min = 2 if min<2 else min
    min = 10 if min>10 else min
    max=0.80
    self.vectorizer = TfidfVectorizer(ngram_range=ngram_range, stop_words='english',use_idf=True,norm=None, max_df=max, min_df=min)
    tfidf_matrix = self.vectorizer.fit_transform(data)
    return tfidf_matrix
  def tfidf_for_labels(self,data,ngram_range=(3, 3)):
    vectorizer = TfidfVectorizer(ngram_range=ngram_range, stop_words='english',use_idf=True,norm=None)
    tfidf_matrix = vectorizer.fit_transform(data)
    return tfidf_matrix, vectorizer

  def createDistanceOneToMany(self):
    self.distances=np.zeros(self.size,dtype=float)
    for j in range(1,self.size):
      self.distances[j]=cosine_similarity(np.array(self.dense[0]), np.array(self.dense[j]))
    data=[[item] for item in self.distances]
    mm = MinMaxScaler()
    mm_data = mm.fit_transform(data)
    self.distances=np.array([item[0] for item in mm_data])

  def createDistances(self):
    self.distances=np.zeros((self.size,self.size),dtype=float)
    for i in range(0,self.size):
      for j in range(i,self.size):
        if(i!=j):
          self.distances[i][j]=cosine_similarity(np.array(self.dense[i]), np.array(self.dense[j]))
          self.distances[j][i]=self.distances[i][j]
      data=[[item] for item in self.distances[i]]
      mm = MinMaxScaler()
      mm_data = mm.fit_transform(data)
      self.distances[i]=np.array([item[0] for item in mm_data])
      self.distances[i][i]=1


# -*- coding: utf-8 -*-
"""
Created on Wed Mar  2 15:35:25 2022

@author: Yigan
"""
from . import graph
from . import statemachine as st
from . import mainalgo as ma
from . import display as ds
from .pruning import BurningAlgo,ETPruningAlgo,AnglePruningAlgo
from . import tree
from . import treealgorithm
import numpy as np

def algo_st():
    return ma.SkeletonApp.inst().algoStatus

def app_st():
    return ma.SkeletonApp.inst().appStatus

def tRec():
    return ma.SkeletonApp.inst().timer

def get_size() -> float:
    refer = 128
    x, y, c = app_st().shape
    m = float(max([x,y,c]))
    return m / refer

class ReadState(st.State):

    def execute(self):
        algo_st().raw_data = self.__read_data()
        app_st().shape = algo_st().raw_data.shape
        tRec().stamp("Read Data")

    def get_next(self):
        return ThreshState()

    def __read_data(self):
        viewer = ds.Display.current().viewer
        layer = viewer.layers[0]
        return layer.data_raw

class ThreshState(st.State):

    def execute(self):
        if algo_st().raw_data is None:
            return
        algo_st().biimg = graph.BinaryImage(algo_st().raw_data, int(app_st().biThresh/100.0*255))
        tRec().stamp("Threshold")

    def get_next(self):
        if algo_st().raw_data is None:
            return None
        return BoundaryState()

class BoundaryState(st.State):

    def execute(self):
        algo_st().boundary = graph.get_edge_vertices(algo_st().biimg)
        tRec().stamp("Find Edge")

        peConfig = ma.get_vorgraph_config(get_size())
        peConfig.pointConfig.edge_color = "red"
        ds.Display.current().draw_layer(graph.Graph(algo_st().boundary,[],[]), peConfig, ds.boundary)
        tRec().stamp("Draw Boundary")

    def get_next(self):
        return VorState()

class VorState(st.State):

    def execute(self):
        algo_st().vor = graph.get_voronoi(algo_st().boundary)
        tRec().stamp("Voronoi")

    def get_next(self):
        return PruneState()

class PruneState(st.State):

    def execute(self):
        algo_st().graph = graph.graph_in_image(algo_st().vor.graph, algo_st().biimg)
        tRec().stamp("Prune Voronoi")

        peConfig = ma.get_vorgraph_config(get_size())
        ds.Display.current().draw_layer(algo_st().vor.graph, peConfig, ds.internalVoronoi)
        tRec().stamp("Draw Prune Voronoi")

    def get_next(self):
        return BTState()

class BTState(st.State):

    def execute(self):
        closestDist = graph.get_closest_dists(algo_st().graph.point_ids, algo_st().vor)
        tRec().stamp("Calc Radius")

        algo_st().algo = BurningAlgo(algo_st().graph, closestDist, max(app_st().shape))
        algo_st().algo.burn()
        tRec().stamp("Burn")

        bts = algo_st().algo.npGraph.get_bts()
        ets = algo_st().algo.npGraph.get_ets()
        self.__draw(bts, ds.burnTime)
        self.__draw(ets, ds.erosionT)
        tRec().stamp("Draw Burn Graph")

    def get_next(self):
        return PruneChoosingState()

    def __draw(self, radi, layerName):
        peConfig = ma.get_vorgraph_config(get_size())
        colors = graph.get_color_list(radi)
        peConfig.pointConfig.edge_color = colors
        peConfig.edgeConfig.edge_color = graph.get_edge_color_list(colors, algo_st().graph.edgeIndex)
        ds.Display.current().draw_layer(algo_st().graph, peConfig, layerName)

class PruneChoosingState(st.State):

    def get_next(self):
        return ETPruneState() if app_st().method == 0 else AngleState()

class AngleState(st.State):

    def execute(self):

        if algo_st().algo is None:
            return

        angles = graph.get_angle(algo_st().graph.edge_ids, algo_st().vor)
        #print(angles)
        algo_st().algo.npGraph.set_angles(angles)
        tRec().stamp("calc angles")

    def get_next(self):
        return AnglePruneState()

class ETPruneState(st.State):

    def execute(self):
        if algo_st().algo is None:
            return

        prune_algo = ETPruningAlgo(algo_st().algo.graph, algo_st().algo.npGraph)
        pruneT = app_st().etThresh / 100.0 * max(app_st().shape)
        algo_st().final = prune_algo.prune(pruneT)
        tRec().stamp("ET Prune")

        peConfig = ma.get_vorgraph_config(get_size())
        peConfig.pointConfig.face_color = "red"
        peConfig.pointConfig.edge_color = "red"
        peConfig.edgeConfig.face_color = "red"
        peConfig.edgeConfig.edge_color = "red"

        ds.Display.current().draw_layer(algo_st().final, peConfig, ds.final)
        tRec().stamp("Draw Final")

class AnglePruneState(st.State):

    def execute(self):
        if algo_st().algo is None:
            return

        pruneT = np.pi * app_st().etThresh / 100.0

        prune_algo = AnglePruningAlgo(algo_st().algo.graph, algo_st().algo.npGraph)
        centroid_graph, centroid_points_color, reward_list, cost_list, point_map, point_pair_map =  prune_algo.prune(pruneT)

        peConfig = ma.get_angular_config(get_size())
        centroid_peConfig = ma.get_angular_centroid_config(get_size())

        all_edge = algo_st().algo.npGraph.get_paths()
        edge_colors = graph.get_color_from_edge(all_edge)

        point_colors = algo_st().algo.npGraph.get_junction_color()

        peConfig.pointConfig.edge_color = point_colors
        peConfig.pointConfig.face_color = point_colors
        peConfig.edgeConfig.edge_color = edge_colors

        ds.Display.current().draw_layer(algo_st().graph, peConfig, ds.angle)
        tRec().stamp("cluster by angles")

        centroid_peConfig.pointConfig.edge_color = centroid_points_color
        centroid_peConfig.pointConfig.face_color = centroid_points_color
        centroid_peConfig.edgeConfig.edge_color = "purple"

        ds.Display.current().draw_layer(centroid_graph, centroid_peConfig, ds.pcst)
        tRec().stamp("draw_PCST")

        initial_tree = tree.Tree(centroid_graph.points, centroid_graph.edgeIndex, reward_list, cost_list)
        result_tree = treealgorithm.Algorithm(initial_tree).execute()

        PCST_result_graph = result_tree.to_graph()

        PCST_result_peConfig = ma.get_PCST_result_config(get_size())

        skeleton_result_graph = graph.cluster_to_skeleton(PCST_result_graph, point_map, point_pair_map)

        ds.Display.current().draw_layer(PCST_result_graph, PCST_result_peConfig, ds.pcstResult)

        tRec().stamp("draw_PCST_result")

        skeleton_result_peConfig = ma.get_skeleton_result_config(get_size())

        ds.Display.current().draw_layer(skeleton_result_graph, skeleton_result_peConfig, ds.skeletonResult)

        tRec().stamp("draw_skeleton_result")


import requests
import pprint
from assertpy import assert_that
from config import BASE_URI
from Blog.blog_posts import get_token_login

def test_aboutus_list():
    r = list_aboutus()
    assert_that(r.status_code).is_equal_to(200)

def list_aboutus():
    list_URI = f'{BASE_URI}/cms/aboutus-list/'
    r = requests.get(list_URI)
    return r

def test_about_us_delete():
    id = id_of_aboutus()
    #pprint.pprint(id)
    delete_URI = f'{BASE_URI}/cms/aboutus-delete/{id}/'
    headers = {
        'Authorization': f'JWT {get_token_login()}'
    }
    r = requests.delete(delete_URI, headers=headers)
    assert_that(r.status_code).is_equal_to(204)

def id_of_aboutus():
    about_us = list_aboutus().json()
    id = about_us[0]['id']
    return id

def test_aboutus_create():
    create_URI = f'{BASE_URI}/cms/aboutus-create/'
    data = {
        'content': 'This is my first content'
    }
    headers = {

    }
    r = requests.post(create_URI, data=data, headers=headers)
    assert_that(r.status_code).is_equal_to(201)

def test_aboutus_detail():
    about_id = id_of_aboutus()
    headers = {
        'Authorization': f'JWT {get_token_login()}'
    }
    detail_URI = f'{BASE_URI}/cms/aboutus-detail/{about_id}/'
    r = requests.get(detail_URI, headers=headers)
    #pprint.pprint(r.json())
    assert_that(r.status_code).is_equal_to(200)

def test_aboutus_update():
    aboutus_id = id_of_aboutus()
    updated_URI = f'{BASE_URI}/cms/aboutus-update/{aboutus_id}/'
    headers = {
        'Authorization': f'JWT {get_token_login()}'
    }
    data = {
        'content': 'Here we see the changed content'
    }
    r = requests.patch(updated_URI, data=data, headers=headers)
    assert_that(r.status_code).is_equal_to(200)
    #pprint.pprint(list_aboutus().json())


from fastapi import APIRouter, HTTPException

from back.crud.setting import (
    AIRFLOW_SETTING_PATH,
    is_airflow_setting,
    is_setting,
    update_airflow_settings,
    update_settings,
)
from back.dependency.security import api_security
from back.init.setting import init_settings_with_examples
from back.model.scope import ScopeEnum
from back.settings import (
    DEFAULT_AIRFLOW_SETTING_NAME,
    DEFAULT_SETTING_NAME,
    Setting,
    setting,
)

router = APIRouter(prefix="/system")

@router.get(
    "",
    response_model=Setting,
    dependencies=[api_security([ScopeEnum.setting_system_get.value])],
)
def get_settings() -> Setting:
    return setting

@router.post(
    "",
    response_model=Setting,
    dependencies=[api_security([ScopeEnum.setting_system_post.value])],
)
def post_settings(setting: Setting) -> Setting:
    if is_setting():
        HTTPException(
            status_code=409, detail=f"`{DEFAULT_SETTING_NAME}` already exists."
        )
    setting = init_settings_with_examples(setting)
    update_settings(setting)
    return setting

@router.put(
    "",
    response_model=Setting,
    dependencies=[api_security([ScopeEnum.setting_system_put.value])],
)
def put_settings(setting: Setting) -> Setting:
    update_settings(setting, force=True)
    return setting

@router.get(
    "/airflow",
    response_model=Setting,
    dependencies=[api_security([ScopeEnum.setting_system_airflow_get.value])],
)
def get_airflow_settings() -> Setting:
    setting = Setting(_env_file=str(AIRFLOW_SETTING_PATH))
    return setting

@router.post(
    "/airflow",
    response_model=Setting,
    dependencies=[api_security([ScopeEnum.setting_system_airflow_post.value])],
)
def post_airflow_settings(setting: Setting) -> Setting:
    if is_airflow_setting():
        HTTPException(
            status_code=409, detail=f"`{DEFAULT_AIRFLOW_SETTING_NAME}` already exists."
        )
    setting = init_settings_with_examples(setting)
    update_airflow_settings(setting)
    return setting

@router.put(
    "/airflow",
    response_model=Setting,
    dependencies=[api_security([ScopeEnum.setting_system_airflow_put.value])],
)
def put_airflow_settings(setting: Setting) -> Setting:
    update_airflow_settings(setting)
    return setting


def compare(S1,S2):
    ngrams = [
        S1[i:i+4] for i in range(len(S1))
    ]
    count = 0
    for ngram in ngrams:
        count += S2.count(ngram)
    return count/max(len(S1), len(S2))

compare('электроника','электричество')


from App.class_init import InitializeClass as initializeClass
from Products.Five.security import protectName

from five import grok
from grokcore.view.meta.views import TemplateGrokker
import martian

from infrae.rest.components import REST, ALLOWED_REST_METHODS
from infrae.rest.components import RESTWithTemplate

class RESTSecurityGrokker(martian.ClassGrokker):
    """Grok REST views.
    """
    martian.component(REST)
    martian.priority(100)
    martian.directive(grok.require, name='permission')

    def execute(self, factory, permission, config, **kw):
        """Register the REST component as a view on the IREST layer.
        """
        # Set name. The priority of the grokker must be higher than 0
        # to be call before the component is registered by
        # zeam.component.
        if not grok.name.bind().get(factory):
            grok.name.set(factory, factory.__name__.lower())

        if permission is None:
            permission = 'zope.Public'

        for method in ALLOWED_REST_METHODS:
            if hasattr(factory, method):
                config.action(
                    discriminator = ('five:protectName', factory, method),
                    callable = protectName,
                    args = (factory, method, permission))

        config.action(
            discriminator = ('five:initialize:class', factory),
            callable = initializeClass,
            args = (factory,))
        return True

class RESTWithTemplateGrokker(TemplateGrokker):
    martian.component(RESTWithTemplate)

    def has_render(self, factory):
        return False

    def has_no_render(self, factory):
        return False


import logging
import sys

# citest modules.
from citest.service_testing import HttpContractBuilder
from citest.service_testing import NoOpOperation
from citest.base import JournalLogger
import citest.base
import citest.gcp_testing as gcp
import citest.json_predicate as jp
import citest.service_testing as st
from citest.json_contract import ObservationPredicateFactory

ov_factory = ObservationPredicateFactory()

# Spinnaker modules.
import spinnaker_testing as sk
import spinnaker_testing.kato as kato

GCP_STANDARD_IMAGES = {
    "centos-cloud": ["centos-7", "centos-6"],
    "coreos-cloud": ["coreos-stable", "coreos-beta", "coreos-alpha"],
    "debian-cloud": ["debian-8"],
    "opensuse-cloud": [""],
    "rhel-cloud": ["rhel-7", "rhel-6"],
    "suse-cloud": [""],
    "ubuntu-os-cloud": [
        "ubuntu-1604-lts",
        "ubuntu-1404-lts",
        "ubuntu-1204-lts",
        "ubuntu-1510",
    ],
    "windows-cloud": ["windows-2012-r2", "windows-2008-r2"],
}

class GoogleKatoTestScenario(sk.SpinnakerTestScenario):
    # _instance_names and _instance_zones will be set in create_instances_.
    # We're breaking them out so that they can be shared by other methods,
    # especially terminate.
    use_instance_names = []
    use_instance_zones = []
    __use_lb_name = ""  # The load balancer name.
    __use_lb_tp_name = ""  # The load balancer's target pool name.
    __use_lb_hc_name = ""  # The load balancer's health check name.
    __use_lb_target = ""  # The load balancer's 'target' resource.
    __use_http_lb_name = ""  # The HTTP load balancer name.
    __use_http_lb_proxy_name = ""  # The HTTP load balancer target proxy name.
    __use_http_lb_hc_name = ""  # The HTTP load balancer health check name.
    __use_http_lb_bs_name = ""  # The HTTP load balancer backend service name.
    __use_http_lb_fr_name = ""  # The HTTP load balancer forwarding rule.
    __use_http_lb_map_name = ""  # The HTTP load balancer url map name.
    __use_http_lb_http_proxy_name = ""  # The HTTP load balancer target http proxy.

    @classmethod
    def new_agent(cls, bindings):
        """Implements the base class interface to create a new agent.

        This method is called by the base classes during setup/initialization.

        Args:
          bindings: The bindings dictionary with configuration information
            that this factory can draw from to initialize. If the factory would
            like additional custom bindings it could add them to initArgumentParser.

        Returns:
          A citest.service_testing.BaseAgent that can interact with Kato.
          This is the agent that test operations will be posted to.
        """
        return kato.new_agent(bindings)

    def create_instances(self):
        """Creates test adding instances to GCE.

         Create three instances.
           * The first two are of different types and zones, which
             we'll check. Future tests will also be using these
             from different zones (but same region).

           * The third is a duplicate in the same zone as another
             so we can check duplicate deletes (which limit one zone per call).

         We'll set the class properties use_instance_names and use_instance_zones
         so that they can be communicated to future tests to reference.

        Returns:
          st.OperationContract
        """
        # We're going to make specific instances so we can refer to them later
        # in tests involving instances. The instances are decorated to trace back
        # to this particular run so as not to conflict with other tests that may
        # be running.
        self.use_instance_names = [
            "katotest%sa" % self.test_id,
            "katotest%sb" % self.test_id,
            "katotest%sc" % self.test_id,
        ]

        # Put the instance in zones. Force one zone to be different
        # to ensure we're testing zone placement. We arent bothering
        # with different regions at this time.
        test_zone = self.bindings["TEST_GCE_ZONE"]
        other_zone = self.bindings["TEST_GCE_REGION"] + (
            "-b" if test_zone[-1] != "b" else "-c"
        )

        self.use_instance_zones = [test_zone, other_zone, test_zone]

        # Give the instances images and machine types. Again we're forcing
        # one to be different to ensure that we're using the values.
        image_name = [
            self.bindings["TEST_GCE_IMAGE_NAME"],
            "debian-9-stretch-v20190116",
            self.bindings["TEST_GCE_IMAGE_NAME"],
        ]
        if image_name[0] == image_name[1]:
            image_name[1] = "ubuntu-minimal-1604-xenial-v20190122"
        machine_type = ["f1-micro", "g1-small", "f1-micro"]

        # The instance_spec will turn into the payload of instances we request.
        instance_spec = []
        builder = gcp.GcpContractBuilder(self.gcp_observer)
        for i in range(3):
            # pylint: disable=bad-continuation
            instance_spec.append(
                {
                    "createGoogleInstanceDescription": {
                        "instanceName": self.use_instance_names[i],
                        "image": image_name[i],
                        "instanceType": machine_type[i],
                        "zone": self.use_instance_zones[i],
                        "credentials": self.bindings["SPINNAKER_GOOGLE_ACCOUNT"],
                    }
                }
            )

            # Verify we created an instance, whether or not it boots.
            (
                builder.new_clause_builder(
                    "Instance %d Created" % i, retryable_for_secs=90
                )
                .aggregated_list_resource("instances")
                .EXPECT(
                    ov_factory.value_list_path_contains(
                        "name", jp.STR_SUBSTR(self.use_instance_names[i])
                    )
                )
            )
            if i < 2:
                # Verify the details are what we asked for.
                # Since we've finished the created clause, this already exists.
                # Note we're only checking the first two since they are different
                # from one another. Anything after that isnt necessary for the test.
                # The clause above already checked that they were all created so we
                # can assume from this test that the details are ok as well.
                (
                    builder.new_clause_builder("Instance %d Details" % i)
                    .inspect_resource(
                        "instances",
                        self.use_instance_names[i],
                        zone=self.use_instance_zones[i],
                    )
                    .contains_path_value("machineType", machine_type[i])
                )
                # Verify the instance eventually boots up.
                # We can combine this with above, but we'll probably need
                # to retry this, but not the above, so this way if the
                # above is broken (wrong), we wont retry thinking it isnt there yet.
                (
                    builder.new_clause_builder(
                        "Instance %d Is Running" % i, retryable_for_secs=360
                    )
                    .inspect_resource(
                        "instances",
                        self.use_instance_names[i],
                        zone=self.use_instance_zones[i],
                    )
                    .EXPECT(
                        ov_factory.value_list_path_contains(
                            "status", jp.STR_EQ("RUNNING")
                        )
                    )
                )

        payload = self.agent.make_json_payload_from_object(instance_spec)

        return st.OperationContract(
            self.new_post_operation(title="create_instances", data=payload, path="ops"),
            contract=builder.build(),
        )

    def terminate_instances(self, names, zone):
        """Creates test for removing specific instances.

        Args:
          names: A list of instance names to be removed.
          zone: The zone containing the instances.

        Returns:
          st.OperationContract
        """
        builder = gcp.GcpContractBuilder(self.gcp_observer)
        clause = builder.new_clause_builder(
            "Instances Deleted", retryable_for_secs=30, strict=True
        ).aggregated_list_resource("instances")
        for name in names:
            # If one of our instances still exists, it should be STOPPING.
            name_matches_pred = jp.PathContainsPredicate("name", name)
            is_stopping_pred = jp.PathEqPredicate("status", "STOPPING")

            # We want the condition to apply to all the observed objects so we'll
            # map the constraint over the observation. Otherwise, if dont map it,
            # then we'd expect the constraint to hold somewhere among the observed
            # objects, but not necessarily all of them.
            clause.AND(
                ov_factory.value_list_matches(
                    [jp.IF(name_matches_pred, is_stopping_pred)], strict=True
                )
            )

        # pylint: disable=bad-continuation
        payload = self.agent.type_to_payload(
            "terminateInstances",
            {
                "instanceIds": names,
                "zone": zone,
                "credentials": self.bindings["SPINNAKER_GOOGLE_ACCOUNT"],
            },
        )

        return st.OperationContract(
            self.new_post_operation(
                title="terminate_instances", data=payload, path="gce/ops"
            ),
            contract=builder.build(),
        )

    def upsert_google_server_group_tags(self):
        # pylint: disable=bad-continuation
        server_group_name = "katotest-server-group"
        payload = self.agent.type_to_payload(
            "upsertGoogleServerGroupTagsDescription",
            {
                "credentials": self.bindings["SPINNAKER_GOOGLE_ACCOUNT"],
                "zone": self.bindings["TEST_GCE_ZONE"],
                "serverGroupName": "katotest-server-group",
                "tags": ["test-tag-1", "test-tag-2"],
            },
        )

        builder = gcp.GcpContractBuilder(self.gcp_observer)
        (
            builder.new_clause_builder("Server Group Tags Added")
            .inspect_resource("instanceGroupManagers", server_group_name)
            .EXPECT(
                ov_factory.value_list_contains(
                    jp.DICT_MATCHES(
                        {
                            "name": jp.STR_SUBSTR(server_group_name),
                            jp.build_path("tags", "items"): jp.LIST_MATCHES(
                                ["test-tag-1", "test-tag-2"]
                            ),
                        }
                    )
                )
            )
        )

        return st.OperationContract(
            self.new_post_operation(
                title="upsert_server_group_tags", data=payload, path="ops"
            ),
            contract=builder.build(),
        )

    def create_http_load_balancer(self):
        logical_http_lb_name = "katotest-httplb-" + self.test_id
        self.__use_http_lb_name = logical_http_lb_name

        self.__use_http_lb_hc_name = logical_http_lb_name + "-health-check"
        self.__use_http_lb_bs_name = logical_http_lb_name + "-backend-service"
        self.__use_http_lb_fr_name = logical_http_lb_name
        self.__use_http_lb_map_name = logical_http_lb_name + "-url-map"
        self.__use_http_lb_proxy_name = logical_http_lb_name + "-target-http-proxy"

        interval = 231
        healthy = 8
        unhealthy = 9
        timeout = 65
        path = "/hello/world"
        port_range = "123-456"

        health_check = {
            "checkIntervalSec": interval,
            "healthyThreshold": healthy,
            "unhealthyThreshold": unhealthy,
            "timeoutSec": timeout,
            "requestPath": path,
        }

        # pylint: disable=bad-continuation
        payload = self.agent.type_to_payload(
            "createGoogleHttpLoadBalancerDescription",
            {
                "healthCheck": health_check,
                "portRange": port_range,
                "loadBalancerName": logical_http_lb_name,
                "credentials": self.bindings["SPINNAKER_GOOGLE_ACCOUNT"],
            },
        )

        hc_dict = dict(health_check)
        del hc_dict["requestPath"]
        hc_match = {name: jp.NUM_EQ(value) for name, value in health_check.items()}
        hc_match["requestPath"] = jp.STR_EQ(path)
        hc_match["name"] = (jp.STR_SUBSTR(self.__use_http_lb_hc_name),)
        builder = gcp.GcpContractBuilder(self.gcp_observer)
        (
            builder.new_clause_builder("Http Health Check Added")
            .list_resource("httpHealthChecks")
            .EXPECT(ov_factory.value_list_contains(jp.DICT_MATCHES(hc_match)))
        )
        (
            builder.new_clause_builder(
                "Global Forwarding Rule Added", retryable_for_secs=15
            )
            .list_resource("globalForwardingRules")
            .EXPECT(
                ov_factory.value_list_contains(
                    jp.DICT_MATCHES(
                        {
                            "name": jp.STR_SUBSTR(self.__use_http_lb_fr_name),
                            "portRange": jp.STR_EQ(port_range),
                        }
                    )
                )
            )
        )
        (
            builder.new_clause_builder("Backend Service Added")
            .list_resource("backendServices")
            .EXPECT(
                ov_factory.value_list_contains(
                    jp.DICT_MATCHES(
                        {
                            "name": jp.STR_SUBSTR(self.__use_http_lb_bs_name),
                            "healthChecks": jp.STR_SUBSTR(self.__use_http_lb_hc_name),
                        }
                    )
                )
            )
        )
        (
            builder.new_clause_builder("Url Map Added")
            .list_resource("urlMaps")
            .EXPECT(
                ov_factory.value_list_contains(
                    jp.DICT_MATCHES(
                        {
                            "name": jp.STR_SUBSTR(self.__use_http_lb_map_name),
                            "defaultService": jp.STR_SUBSTR(self.__use_http_lb_bs_name),
                        }
                    )
                )
            )
        )
        (
            builder.new_clause_builder("Target Http Proxy Added")
            .list_resource("targetHttpProxies")
            .EXPECT(
                ov_factory.value_list_contains(
                    jp.DICT_MATCHES(
                        {
                            "name": jp.STR_SUBSTR(self.__use_http_lb_proxy_name),
                            "urlMap": jp.STR_SUBSTR(self.__use_http_lb_map_name),
                        }
                    )
                )
            )
        )

        return st.OperationContract(
            self.new_post_operation(
                title="create_http_load_balancer", data=payload, path="ops"
            ),
            contract=builder.build(),
        )

    def delete_http_load_balancer(self):
        # pylint: disable=bad-continuation
        payload = self.agent.type_to_payload(
            "deleteGoogleHttpLoadBalancerDescription",
            {
                "loadBalancerName": self.__use_http_lb_name,
                "credentials": self.bindings["SPINNAKER_GOOGLE_ACCOUNT"],
            },
        )

        builder = gcp.GcpContractBuilder(self.gcp_observer)
        (
            builder.new_clause_builder("Health Check Removed")
            .list_resource("httpHealthChecks")
            .EXPECT(
                ov_factory.value_list_path_excludes(
                    "name", jp.STR_SUBSTR(self.__use_http_lb_hc_name)
                )
            )
        )
        (
            builder.new_clause_builder("Global Forwarding Rules Removed")
            .list_resource("globalForwardingRules")
            .EXPECT(
                ov_factory.value_list_path_excludes(
                    "name", jp.STR_SUBSTR(self.__use_http_lb_fr_name)
                )
            )
        )
        (
            builder.new_clause_builder("Backend Service Removed")
            .list_resource("backendServices")
            .EXPECT(
                ov_factory.value_list_path_excludes(
                    "name", jp.STR_SUBSTR(self.__use_http_lb_bs_name)
                )
            )
        )
        (
            builder.new_clause_builder("Url Map Removed")
            .list_resource("urlMaps")
            .EXPECT(
                ov_factory.value_list_path_excludes(
                    "name", jp.STR_SUBSTR(self.__use_http_lb_map_name)
                )
            )
        )
        (
            builder.new_clause_builder("Target Http Proxy Removed")
            .list_resource("targetHttpProxies")
            .EXPECT(
                ov_factory.value_list_path_excludes(
                    "name", jp.STR_SUBSTR(self.__use_http_lb_proxy_name)
                )
            )
        )

        return st.OperationContract(
            self.new_post_operation(
                title="delete_http_load_balancer", data=payload, path="ops"
            ),
            contract=builder.build(),
        )

    def upsert_load_balancer(self):
        self.__use_lb_name = "katotest-lb-" + self.test_id
        self.__use_lb_hc_name = "%s-hc" % self.__use_lb_name
        self.__use_lb_tp_name = "%s-tp" % self.__use_lb_name
        self.__use_lb_target = "{}/targetPools/{}".format(
            self.bindings["TEST_GCE_REGION"], self.__use_lb_tp_name
        )

        interval = 123
        healthy = 4
        unhealthy = 5
        timeout = 78
        path = "/" + self.__use_lb_target

        health_check = {
            "checkIntervalSec": interval,
            "healthyThreshold": healthy,
            "unhealthyThreshold": unhealthy,
            "timeoutSec": timeout,
            "requestPath": path,
        }

        # pylint: disable=bad-continuation
        payload = self.agent.type_to_payload(
            "upsertGoogleLoadBalancerDescription",
            {
                "healthCheck": health_check,
                "region": self.bindings["TEST_GCE_REGION"],
                "credentials": self.bindings["SPINNAKER_GOOGLE_ACCOUNT"],
                "loadBalancerName": self.__use_lb_name,
            },
        )

        builder = gcp.GcpContractBuilder(self.gcp_observer)
        (
            builder.new_clause_builder("Forwarding Rules Added", retryable_for_secs=30)
            .list_resource("forwardingRules")
            .EXPECT(
                ov_factory.value_list_contains(
                    jp.DICT_MATCHES(
                        {
                            "name": jp.STR_SUBSTR(self.__use_lb_name),
                            "target": jp.STR_SUBSTR(self.__use_lb_target),
                        }
                    )
                )
            )
        )
        (
            builder.new_clause_builder("Target Pool Added", retryable_for_secs=15)
            .list_resource("targetPools")
            .EXPECT(
                ov_factory.value_list_path_contains(
                    "name", jp.STR_SUBSTR(self.__use_lb_tp_name)
                )
            )
        )

        # We list the resources here because the name isnt exact
        # and the list also returns the details we need.
        hc_dict = dict(health_check)
        del hc_dict["requestPath"]

        hc_match = {name: jp.NUM_EQ(value) for name, value in hc_dict.items()}
        hc_match["requestPath"] = jp.STR_EQ(path)
        hc_match["name"] = jp.STR_SUBSTR(self.__use_http_lb_hc_name)
        (
            builder.new_clause_builder("Health Check Added", retryable_for_secs=15)
            .list_resource("httpHealthChecks")
            .EXPECT(ov_factory.value_list_contains(jp.DICT_MATCHES(hc_match)))
        )

        return st.OperationContract(
            self.new_post_operation(
                title="upsert_load_balancer", data=payload, path="ops"
            ),
            contract=builder.build(),
        )

    def delete_load_balancer(self):
        # pylint: disable=bad-continuation
        payload = self.agent.type_to_payload(
            "deleteGoogleLoadBalancerDescription",
            {
                "region": self.bindings["TEST_GCE_REGION"],
                "credentials": self.bindings["SPINNAKER_GOOGLE_ACCOUNT"],
                "loadBalancerName": self.__use_lb_name,
            },
        )

        builder = gcp.GcpContractBuilder(self.gcp_observer)
        (
            builder.new_clause_builder("Health Check Removed")
            .list_resource("httpHealthChecks")
            .EXPECT(
                ov_factory.value_list_path_excludes(
                    "name", jp.STR_SUBSTR(self.__use_lb_hc_name)
                )
            )
        )
        (
            builder.new_clause_builder("Target Pool Removed")
            .list_resource("targetPools")
            .EXPECT(
                ov_factory.value_list_path_excludes(
                    "name", jp.STR_SUBSTR(self.__use_lb_tp_name)
                )
            )
        )
        (
            builder.new_clause_builder("Forwarding Rule Removed")
            .list_resource("forwardingRules")
            .EXPECT(
                ov_factory.value_list_path_excludes(
                    "name", jp.STR_SUBSTR(self.__use_lb_name)
                )
            )
        )

        return st.OperationContract(
            self.new_post_operation(
                title="delete_load_balancer", data=payload, path="ops"
            ),
            contract=builder.build(),
        )

    def register_load_balancer_instances(self):
        """Creates test registering the first two instances with a load balancer.

           Assumes that create_instances test has been run to add
           the instances. Note by design these were in two different zones
           but same region as required by the API this is testing.

           Assumes that upsert_load_balancer has been run to
           create the load balancer itself.
        Returns:
          st.OperationContract
        """
        # pylint: disable=bad-continuation
        payload = self.agent.type_to_payload(
            "registerInstancesWithGoogleLoadBalancerDescription",
            {
                "loadBalancerNames": [self.__use_lb_name],
                "instanceIds": self.use_instance_names[:2],
                "region": self.bindings["TEST_GCE_REGION"],
                "credentials": self.bindings["SPINNAKER_GOOGLE_ACCOUNT"],
            },
        )

        builder = gcp.GcpContractBuilder(self.gcp_observer)
        (
            builder.new_clause_builder(
                "Instances in Target Pool", retryable_for_secs=15
            )
            .list_resource("targetPools")
            .EXPECT(
                ov_factory.value_list_contains(
                    jp.DICT_MATCHES(
                        {
                            "name": jp.STR_SUBSTR(self.__use_lb_tp_name),
                            "instances": jp.LIST_MATCHES(
                                [
                                    jp.STR_SUBSTR(self.use_instance_names[0]),
                                    jp.STR_SUBSTR(self.use_instance_names[1]),
                                ]
                            ),
                        }
                    )
                )
            )
            .AND(
                ov_factory.value_list_excludes(
                    jp.DICT_MATCHES(
                        {
                            "name": jp.STR_SUBSTR(self.__use_lb_tp_name),
                            "instances": jp.LIST_MATCHES(
                                [jp.STR_SUBSTR(self.use_instance_names[2])]
                            ),
                        }
                    )
                )
            )
        )

        return st.OperationContract(
            self.new_post_operation(
                title="register_load_balancer_instances", data=payload, path="ops"
            ),
            contract=builder.build(),
        )

    def deregister_load_balancer_instances(self):
        """Creates a test unregistering instances from load balancer.

        Returns:
          st.OperationContract
        """
        # pylint: disable=bad-continuation
        payload = self.agent.type_to_payload(
            "deregisterInstancesFromGoogleLoadBalancerDescription",
            {
                "loadBalancerNames": [self.__use_lb_name],
                "instanceIds": self.use_instance_names[:2],
                "region": self.bindings["TEST_GCE_REGION"],
                "credentials": self.bindings["SPINNAKER_GOOGLE_ACCOUNT"],
            },
        )

        # NOTE(ewiseblatt): 20150530
        # This displays an error that 'instances' field doesnt exist.
        # That's because it was removed because all the instances are gone.
        # I dont have a way to express that the field itself is optional,
        # just the record. Leaving it as is because displaying this type of
        # error is usually helpful for development.
        builder = gcp.GcpContractBuilder(self.gcp_observer)
        (
            builder.new_clause_builder(
                "Instances not in Target Pool", retryable_for_secs=30
            )
            .list_resource("targetPools", region=self.bindings["TEST_GCE_REGION"])
            .EXPECT(
                ov_factory.value_list_excludes(
                    jp.DICT_MATCHES(
                        {
                            "name": jp.STR_SUBSTR(self.__use_lb_tp_name),
                            "instances": jp.LIST_MATCHES(
                                [
                                    jp.STR_SUBSTR(self.use_instance_names[0]),
                                    jp.STR_SUBSTR(self.use_instance_names[1]),
                                ]
                            ),
                        }
                    )
                )
            )
        )

        return st.OperationContract(
            self.new_post_operation(
                title="deregister_load_balancer_instances", data=payload, path="ops"
            ),
            contract=builder.build(),
        )

    def list_available_images(self):
        """Creates a test that confirms expected available images.

        Returns:
          st.OperationContract
        """
        logger = logging.getLogger(__name__)

        # Get the list of images available (to the service account we are using).
        context = citest.base.ExecutionContext()
        gcp_agent = self.gcp_observer
        JournalLogger.begin_context("Collecting expected available images")
        relation_context = "ERROR"
        try:
            logger.debug("Looking up available images.")

            json_doc = gcp_agent.list_resource(context, "images")
            for project in GCP_STANDARD_IMAGES.keys():
                logger.info("Looking for images from project=%s", project)
                found = gcp_agent.list_resource(context, "images", project=project)
                for image in found:
                    if not image.get("deprecated", None):
                        json_doc.append(image)

            # Produce the list of images that we expect to receive from spinnaker
            # (visible to the primary service account).
            spinnaker_account = self.bindings["SPINNAKER_GOOGLE_ACCOUNT"]

            logger.debug('Configured with Spinnaker account "%s"', spinnaker_account)
            expect_images = [
                {"account": spinnaker_account, "imageName": image["name"]}
                for image in json_doc
            ]
            expect_images = sorted(expect_images, key=lambda k: k["imageName"])
            relation_context = "VALID"
        finally:
            JournalLogger.end_context(relation=relation_context)

        # pylint: disable=bad-continuation
        builder = HttpContractBuilder(self.agent)
        (
            builder.new_clause_builder("Has Expected Images")
            .get_url_path("/gce/images/find")
            .EXPECT(
                ov_factory.value_list_matches(
                    [jp.DICT_SUBSET(image_entry) for image_entry in expect_images],
                    strict=True,
                    unique=True,
                )
            )
        )

        return st.OperationContract(
            NoOpOperation("List Available Images"), contract=builder.build()
        )

class GoogleKatoIntegrationTest(st.AgentTestCase):
    @property
    def scenario(self):
        return citest.base.TestRunner.global_runner().get_shared_data(
            GoogleKatoTestScenario
        )

    def Xtest_a_upsert_server_group_tags(self):
        self.run_test_case(self.scenario.upsert_google_server_group_tags())

    def test_a_upsert_load_balancer(self):
        self.run_test_case(self.scenario.upsert_load_balancer())

    def test_b_create_instances(self):
        self.run_test_case(self.scenario.create_instances())

    def test_c_register_load_balancer_instances(self):
        self.run_test_case(self.scenario.register_load_balancer_instances())

    def Xtest_d_create_http_load_balancer(self):
        self.run_test_case(self.scenario.create_http_load_balancer())

    def Xtest_v_delete_http_load_balancer(self):
        self.run_test_case(
            self.scenario.delete_http_load_balancer(),
            timeout_ok=True,
            retry_interval_secs=10,
            max_retries=9,
        )

    def test_w_deregister_load_balancer_instances(self):
        self.run_test_case(self.scenario.deregister_load_balancer_instances())

    def test_x_terminate_instances(self):
        # delete 1 which was in a different zone than the other two.
        # Then delete [0,2] together, which were in the same zone.
        try:
            self.run_test_case(
                self.scenario.terminate_instances(
                    [self.scenario.use_instance_names[1]],
                    self.scenario.use_instance_zones[1],
                )
            )
        finally:
            # Always give this a try, even if the first test fails.
            # that increases our chances of cleaning everything up.
            self.run_test_case(
                self.scenario.terminate_instances(
                    [
                        self.scenario.use_instance_names[0],
                        self.scenario.use_instance_names[2],
                    ],
                    self.scenario.use_instance_zones[0],
                )
            )

    def test_z_delete_load_balancer(self):
        # TODO(ewiseblatt): 20151220
        # The retry here is really due to the 400 "not ready" race condition
        # within GCP. Would be better to couple this to the agent and not the
        # test so that it is easier to maintain. Need to add a generalization
        # so the agent can see this is a delete test, got a 400, and only
        # in that condition override the default retry parameters, then stick
        # with the defaults here.
        self.run_test_case(self.scenario.delete_load_balancer(), max_retries=5)

    def Xtest_available_images(self):
        self.run_test_case(self.scenario.list_available_images())

def main():
    return citest.base.TestRunner.main(
        parser_inits=[GoogleKatoTestScenario.initArgumentParser],
        test_case_list=[GoogleKatoIntegrationTest],
    )

if __name__ == "__main__":
    sys.exit(main())