How to use isalive method in pyatom

Best Python code snippet using pyatom_python

SpaceInvaders.py

Source:SpaceInvaders.py Github

copy

Full Screen

1import pygame2import time3import random4from pygame.locals import *5#Initialze pygame6pygame.init()7#Defining RGB values for colours8BLACK = (0, 0, 0)9WHITE = (255, 255, 255)10GREY = (174, 182, 191)11GREEN = (0, 255, 0)12RED = (255, 0, 0)13BLUE = (0, 0, 255)14ORANGE = (255, 130, 0)15GOLD = (230, 215, 0)16YELLOW = (255,255,0)17font_comic = pygame.font.SysFont('Comic Sans MS', 40)18font_times = pygame.font.SysFont('Times New Roman', 40)19font_points = pygame.font.SysFont('Times New Roman', 20)20screen = pygame.display.set_mode((800,650)) #Initialzing display21pygame.display.set_caption('SPACE INVADER') 22clock = pygame.time.Clock()23score=024try:25 file_highscore=open("highscore.txt",'r') #reads highscore from text file26 file_highscore.seek(0,0)27 highest_score=int(file_highscore.read())28 file_highscore.close()29except:30 highest_score=031def WelcomeScreen(): #initial screen before the game begins32 global quit,game_state,invader1,invader2,invaderM,invader333 mouse_pos=pygame.mouse.get_pos()34 mouse_click=pygame.mouse.get_pressed()35 screen.fill(GREY)36 logo = pygame.image.load('space-invaders-logo_transparent.png')37 screen.blit(logo,(30,40))38 screen.blit(invader1,(150,550))39 points1 = font_points.render(':- 10 POINTS', False, BLUE)40 screen.blit(points1,(200,550))41 screen.blit(invader2,(450,550))42 points1 = font_points.render(':- 20 POINTS', False, BLUE)43 screen.blit(points1,(500,550))44 screen.blit(invader3,(150,600))45 points1 = font_points.render(':- 30 POINTS', False, BLUE)46 screen.blit(points1,(200,600))47 screen.blit(invaderM,(450,600))48 points1 = font_points.render(':- Mystery (Hit Thrice)', False, BLUE)49 screen.blit(points1,(500,600))50 if ((mouse_pos[0]-400)**2 + (mouse_pos[1]-400)**2) <=8100 :51 pygame.draw.circle(screen, RED,(400,400),90)52 if mouse_click[0]:53 game_state = 154 Game()55 56 else:57 pygame.draw.circle(screen, BLUE,(400,400),90)58 text_welcome = font_times.render('START', False, WHITE)59 screen.blit(text_welcome,(340,360))60 text_welcome = font_times.render('GAME', False, WHITE)61 screen.blit(text_welcome,(340,400))62def Game(): #The game has begin... initialize all classes and display63 global score,highest_score,ship,ship_x,ship_y,vel_ship,invader1a_list,invader1b_list,invader1c_list,invader2_list,invader3_list,invader_mys,obstruct1,obstruct2,obstruct3,obstruct4,invader1,invader2,invader3,invaderM,bullet,i1fire,i2fire,i3fire,i4fire,speed_invader1,speed_invader2,speed_invader3,speed_mys,bullet1,vel_bull,fire_alive64 background = pygame.image.load('background.png')65 screen.blit(background,(0,0))66 text_score = font_comic.render('SCORE:', False, GOLD)67 screen.blit(text_score,(150,25))68 text_score = font_comic.render(str(score), False, ORANGE)69 screen.blit(text_score,(270,25))70 text_score = font_comic.render('HIGH SCORE:', False, GOLD)71 screen.blit(text_score,(430,25))72 if highest_score<score:73 highest_score=score74 fo=open('highscore.txt','w')75 fo.write(str(highest_score))76 fo.close()77 text_score = font_comic.render(str(highest_score), False, ORANGE)78 screen.blit(text_score,(630,25))79 Spaceship()80 for i in range(len(invader1a_list)):81 if invader1a_list[i].isAlive:82 invader1a_list[i].Get_Invader()83 if invader1b_list[i].isAlive:84 invader1b_list[i].Get_Invader()85 if invader1c_list[i].isAlive:86 invader1c_list[i].Get_Invader()87 for i in range(len(invader2_list)):88 if invader2_list[i].isAlive: 89 invader2_list[i].Get_Invader()90 if invader3_list[i].isAlive:91 invader3_list[i].Get_Invader()92 if invader_mys.isAlive:93 invader_mys.Get_Invader()94 Move_Invader()95 for obstructs in obstruct1:96 if obstructs.isAlive:97 obstructs.Obs()98 for obstructs in obstruct2:99 if obstructs.isAlive:100 obstructs.Obs()101 for obstructs in obstruct3:102 if obstructs.isAlive:103 obstructs.Obs()104 for obstructs in obstruct4:105 if obstructs.isAlive:106 obstructs.Obs()107 Reach_bottom()108 Check_hit()109 if bullet1.isAlive:110 bullet1.bullet_y+=vel_bull111 bullet1.Get_Bullet()112 if bullet1.bullet_y<= 50:113 bullet1.isAlive=False114 firebyinvader()115 checkhit_invader()116 117def Left_press(): #increase velocity118 global vel_ship119 vel_ship=-10120 pass121def Right_press(): #increase velocity122 global vel_ship123 vel_ship=10124 pass125def Release_arrowkey(): #velocity=0126 global vel_ship127 vel_ship=0128 pass129def Game_end(): ##Increase size of font and positioning correctly left130 global quit,game_state131 mouse=pygame.mouse.get_pos()132 click=pygame.mouse.get_pressed()133 screen.fill(GOLD)134 myfont_win = pygame.font.SysFont('Comic Sans MS', 120)135 col=random.choice((1,2,3))136 if game_state==2:137 game_endtext='YOU LOST <_>..!!'138 if game_state == 3:139 game_endtext='YOU WONNN <_>..!!'140 if col==1:141 textsurface=myfont_win.render(str(game_endtext),False, BLUE)142 screen.blit(textsurface,(100,200)) 143 if col==2:144 textsurface=myfont_win.render(str(game_endtext),False, ORANGE)145 screen.blit(textsurface,(100,200))146 if col==3:147 textsurface=myfont_win.render(str(game_endtext),False, RED)148 screen.blit(textsurface,(100,200))149 if mouse[0] < 375 and mouse[0] > 200 and mouse[1] < 450 and mouse[1] > 400:150 pygame.draw.rect(screen, GREEN,(200, 400,125,50))151 if click[0]==1:152 quit=True153 else:154 pygame.draw.rect(screen,RED,(200,400,125,50)) 155 textsurface = myfont_quit.render('QUIT', False, WHITE)156 screen.blit(textsurface,(222,410)) 157 if mouse[0] < 650 and mouse[0] > 430 and mouse[1] < 450 and mouse[1] > 400:158 pygame.draw.rect(screen, GREEN,(410,400,220,50))159 if click[0]==1:160 initial()161 game_state = 1 162 Game()163 else:164 pygame.draw.rect(screen,BLUE,(410,400,220,50)) 165 textsurface = myfont_quit.render('PLAY AGAIN', False, WHITE)166 screen.blit(textsurface,(435,412))167def Fire_Spaceship(): #release a bullet from same x coordinate upwards168 global bullet1,vel_bull169 if bullet1.isAlive:170 pass171 else:172 bullet1=Bullet(bullet)173 bullet1.isAlive = True174 vel_bull=-15175 pass176def firebyinvader():177 global i1fire,i2fire,i3fire,i4fire,fire_alive,fire,fire_y,shooter,fire_x178 shot = [i1fire,i2fire,i3fire,i4fire]179 if fire_alive == False:180 fire = random.choice(shot)181 fire_alive = True182 if fire == i1fire:183 shooter = random.choice(invader1b_list)184 fire_y=shooter.invader_y185 fire_x=shooter.invader_x186 elif fire == i2fire:187 shooter = random.choice(invader3_list)188 fire_y=shooter.invader_y189 fire_x=shooter.invader_x190 elif fire == i3fire:191 shooter = random.choice(invader2_list)192 fire_y=shooter.invader_y193 fire_x=shooter.invader_x194 else:195 shooter = invader_mys196 fire_y=shooter.invader_y197 fire_x=shooter.invader_x198 screen.blit(fire,(fire_x,fire_y))199 fire_y+=7200 if fire_y > ship_y+5:201 fire_alive=False202def initial():203 global ship,ship_x,ship_y,vel_ship,invader1a_list,invader1b_list,invader1c_list,invader2_list,invader3_list,invader_mys,obstruct1,obstruct2,obstruct3,obstruct4,invader1,invader2,invader3,invaderM,bullet,i1fire,i2fire,i3fire,i4fire,speed_invader1,speed_invader2,speed_invader3,speed_mys,bullet1,score,fire_alive204 # Spaceship object and starting coordinates205 score=0206 ship = pygame.image.load('tank.png').convert_alpha()207 ship_y = screen.get_height() - 70208 ship_x = screen.get_width()/2 - ship.get_width()/2209 vel_ship=0210 fire_alive = False211 # Invader Class212 invader1 = pygame.image.load('i1.png').convert_alpha()213 invader2 = pygame.image.load('i3.png').convert_alpha() 214 invader3 = pygame.image.load('i2.png').convert_alpha() 215 invaderM = pygame.image.load('i4.png').convert_alpha()216 i1fire = pygame.image.load('i1fire.png')217 i2fire = pygame.image.load('i2fire.png')218 i3fire = pygame.image.load('i3fire.png')219 i4fire = pygame.image.load('i4fire.png')220 bullet = pygame.image.load('bullet.jpg')221 bullet1 = Bullet(bullet)222 bullet2 = Bullet(i1fire)223 bullet3 = Bullet(i2fire)224 bullet4 = Bullet(i3fire)225 bullet5 = Bullet(i4fire)226 speed_invader1 = 2227 speed_invader2 = -3228 speed_invader3 = 3229 speed_mys = 8230 ##Initalise invaders231 invader1a_list=[Invader1(invader1,i,300) for i in range(200,650,50)]232 invader1b_list=[Invader1(invader1,i,265) for i in range(200,650,50)]233 invader1c_list=[Invader1(invader1,i,230) for i in range(200,650,50)]234 invader2_list=[Invader2(invader2,i,185) for i in range(220,680,50)]235 invader3_list=[Invader3(invader3,i,140) for i in range(220,590,40)]236 invader_mys=Invader_Mystery(invaderM,380,100)237 obstruct1 = [Obstruct(100+i,500+j) for j in range(0,30,10) for i in range (0,50,10)]238 obstruct2 = [Obstruct(300+i,500+j) for j in range(0,30,10) for i in range (0,50,10)]239 obstruct3 = [Obstruct(500+i,500+j) for j in range(0,30,10) for i in range (0,50,10)]240 obstruct4 = [Obstruct(700+i,500+j) for j in range(0,30,10) for i in range (0,50,10)]241# Function to change positon and display Spaceship242def Spaceship():243 global vel_ship,ship,ship_y,ship_x244 ship_x+=vel_ship245 if ship_x <= 0 + 5: #stop if hits left wall246 ship_x-=vel_ship247 vel_ship=0248 if ship_x >= 800 - ship.get_width(): #stop if hits right wall249 ship_x-=vel_ship250 vel_ship=0251 screen.blit(ship,(ship_x,ship_y))252class Invader1: #score=10253 def __init__(self,invader1,invader_x,invader1_y):254 self.invader1 = invader1255 self.invader_x = invader_x256 self.invader_y = invader1_y257 self.isAlive = True258 259 def Get_Invader(self):260 screen.blit(self.invader1,(self.invader_x,self.invader_y))261 def Invader_Attack(self):262 global score263 self.isAlive = False264 score += 10 #score=10265class Invader2: #score=20266 def __init__(self,invader2,invader_x,invader2_y):267 self.invader2 = invader2268 self.invader_x = invader_x269 self.invader_y = invader2_y270 self.isAlive = True271 272 def Get_Invader(self):273 screen.blit(self.invader2,(self.invader_x,self.invader_y))274 def Invader_Attack(self):275 global score276 self.isAlive = False277 score += 20 #score=20278class Invader3: #score=30279 def __init__(self,invader3,invader_x,invader3_y):280 self.invader3 = invader3281 self.invader_x = invader_x282 self.invader_y = invader3_y283 self.isAlive = True284 285 def Get_Invader(self):286 screen.blit(self.invader3,(self.invader_x,self.invader_y))287 def Invader_Attack(self):288 global score289 self.isAlive = False290 score += 30 #score=30291class Invader_Mystery: #needs to be attacked thrice to be killed and score =100292 def __init__(self,invader_m,invader_x,invaderM_y):293 self.invaderM = invader_m294 self.invader_x = invader_x295 self.invader_y = invaderM_y296 self.isAlive = True297 self.count=0298 299 def Get_Invader(self):300 screen.blit(self.invaderM,(self.invader_x,self.invader_y))301 def Invader_Attack(self):302 global score303 self.count+=1304 if self.count==3:305 self.isAlive = False 306 score += 100307class Bullet:308 global ship_x,ship_y309 def __init__(self,bullet):310 self.bullet = bullet311 self.bullet_x = ship_x + ship.get_width()/2 -5312 self.bullet_y = ship_y313 self.isAlive = False314 def Get_Bullet(self):315 screen.blit(self.bullet,(self.bullet_x,self.bullet_y))316# Invader Movement Function317def Move_Invader():318 global invader1a_list,invader2_list,invader1b_list,invader1c_list,invader3_list,invader_mys,speed_invader1,speed_invader2,speed_mys,speed_invader3319 320 left = 0321 right = -1322 flag_left,flag_right = 1,1323 while flag_left or flag_right: #to check the leftmost and rightmost alive row324 if invader1a_list[left].isAlive or invader1b_list[left].isAlive or invader1c_list[left].isAlive:325 flag_left=0326 else:327 left += 1328 if invader1a_list[right].isAlive or invader1b_list[right].isAlive or invader1c_list[right].isAlive:329 flag_right=0330 else:331 right -= 1332 if (invader1a_list[right].invader_x >= 750 or invader1a_list[left].invader_x <= 15) :333 speed_invader1 *= -1.05334 for invaders in invader1a_list:335 invaders.invader_x += speed_invader1336 invaders.invader_y += 0.13337 for invaders in invader1b_list:338 invaders.invader_x += speed_invader1339 invaders.invader_y += 0.13340 for invaders in invader1c_list:341 invaders.invader_x += speed_invader1342 invaders.invader_y += 0.13343 left = 0344 right = -1345 flag_left,flag_right = 1,1346 while flag_left or flag_right: #to check the leftmost and rightmost alive row347 if invader2_list[left].isAlive:348 flag_left=0349 else:350 left += 1351 if invader2_list[right].isAlive:352 flag_right=0353 else:354 right -= 1355 if (invader2_list[right].invader_x >= 750 or invader2_list[left].invader_x <= 15) :356 speed_invader2 *= -1.03357 for invaders in invader2_list:358 invaders.invader_x += speed_invader2359 invaders.invader_y += 0.13360 left = 0361 right = -1362 flag_left,flag_right = 1,1363 while flag_left or flag_right: #to check the leftmost and rightmost alive row364 if invader3_list[left].isAlive:365 flag_left=0366 else:367 left += 1368 if invader3_list[right].isAlive:369 flag_right=0370 else:371 right -= 1372 if (invader3_list[right].invader_x >= 750 or invader3_list[left].invader_x <= 15) :373 speed_invader3 *= -1.03374 for invaders in invader3_list:375 invaders.invader_x += speed_invader3376 invaders.invader_y += 0.13377 if (invader_mys.invader_x >= 740 or invader_mys.invader_x <=15 ):378 speed_mys *= -1.01379 invader_mys.invader_x += speed_mys380 invader_mys.invader_y += 0.1381class Obstruct:382 def __init__(self,x,y):383 self.x = x384 self.y = y385 self.isAlive = True386 def Obs(self):387 '''pygame.draw.rect(screen,GREY,(self.x,self.y,10,10))388 for i in range (0,4):389 pygame.draw.line(screen,BLACK,(self.x,self.y+2*(i+1)),(self.x+10,self.y+2*(i+1)))390 pygame.draw.line(screen,BLACK,(self.x+2*(i+1),self.y),(self.x+2*(i+1),self.y+10))'''391 obs_image=pygame.image.load('spray_10.jpg').convert_alpha()392 screen.blit(obs_image,(self.x,self.y))393 def Obs_Attack():394 self.isAlive = False395def Reach_bottom():396 global game_state397 for invader in invader1a_list:398 if invader.isAlive and (invader.invader_y>450):399 game_state=2400 return False401 for invader in invader1b_list:402 if invader.isAlive and (invader.invader_y>450):403 game_state=2404 return False405 for invader in invader1c_list:406 if invader.isAlive and (invader.invader_y>450):407 game_state=2408 return False409 for invader in invader2_list:410 if invader.isAlive and (invader.invader_y>450):411 game_state=2412 return False413 for invader in invader3_list:414 if invader.isAlive and (invader.invader_y>450):415 game_state=2416 return False417 if invader_mys.isAlive and (invader_mys.invader_y>410):418 game_state =2419def Check_hit():420 if bullet1.isAlive:421 for i in range(len(invader1a_list)):422 if invader1a_list[i].isAlive:423 if bullet1.bullet_x+3 >= invader1a_list[i].invader_x and bullet1.bullet_x+3 <= invader1a_list[i].invader_x + 28 and bullet1.bullet_y+9 >= invader1a_list[i].invader_y and bullet1.bullet_y+9 <= invader1a_list[i].invader_y + 26:424 bullet1.isAlive = False425 invader1a_list[i].Invader_Attack()426 if invader1b_list[i].isAlive:427 if bullet1.bullet_x+3 >= invader1b_list[i].invader_x and bullet1.bullet_x+3 <= invader1b_list[i].invader_x + 28 and bullet1.bullet_y+9 >= invader1b_list[i].invader_y and bullet1.bullet_y+9 <= invader1b_list[i].invader_y + 26:428 bullet1.isAlive = False429 invader1a_list[i].Invader_Attack()430 if invader1c_list[i].isAlive:431 if bullet1.bullet_x+3 >= invader1c_list[i].invader_x and bullet1.bullet_x+3 <= invader1c_list[i].invader_x + 28 and bullet1.bullet_y+9 >= invader1c_list[i].invader_y and bullet1.bullet_y+9 <= invader1c_list[i].invader_y + 26:432 bullet1.isAlive = False433 invader1a_list[i].Invader_Attack()434 for i in range(len(invader2_list)):435 if invader2_list[i].isAlive: 436 if bullet1.bullet_x+3 >= invader2_list[i].invader_x and bullet1.bullet_x+3 <= invader2_list[i].invader_x + 35 and bullet1.bullet_y+9 >= invader2_list[i].invader_y and bullet1.bullet_y+9 <= invader2_list[i].invader_y + 24:437 bullet1.isAlive = False438 invader2_list[i].Invader_Attack()439 if invader3_list[i].isAlive:440 if bullet1.bullet_x+3 >= invader3_list[i].invader_x and bullet1.bullet_x+3 <= invader3_list[i].invader_x + 28 and bullet1.bullet_y+9 >= invader3_list[i].invader_y and bullet1.bullet_y+9 <= invader3_list[i].invader_y + 26:441 bullet1.isAlive = False442 invader3_list[i].Invader_Attack()443 if invader_mys.isAlive:444 if bullet1.bullet_x+3 >= invader_mys.invader_x and bullet1.bullet_x+3 <= invader_mys.invader_x + 28 and bullet1.bullet_y+9 >= invader_mys.invader_y and bullet1.bullet_y+9 <= invader_mys.invader_y + 26:445 bullet1.isAlive = False446 invader_mys.Invader_Attack()447 for obstructs in obstruct1:448 if obstructs.isAlive:449 if bullet1.bullet_x+3 >= obstructs.x and bullet1.bullet_x+3 <= obstructs.x + 10 and bullet1.bullet_y+9 >= obstructs.y and bullet1.bullet_y+3 <= obstructs.y + 10:450 bullet1.isAlive = False451 obstructs.isAlive = False452 for obstructs in obstruct2:453 if obstructs.isAlive:454 if bullet1.bullet_x+3 >= obstructs.x and bullet1.bullet_x+3 <= obstructs.x + 10 and bullet1.bullet_y+9 >= obstructs.y and bullet1.bullet_y+3 <= obstructs.y + 10:455 bullet1.isAlive = False456 obstructs.isAlive = False457 for obstructs in obstruct3:458 if obstructs.isAlive:459 if bullet1.bullet_x+3 >= obstructs.x and bullet1.bullet_x+3 <= obstructs.x + 10 and bullet1.bullet_y+9 >= obstructs.y and bullet1.bullet_y+3 <= obstructs.y + 10:460 bullet1.isAlive = False461 obstructs.isAlive = False462 for obstructs in obstruct4:463 if obstructs.isAlive:464 if bullet1.bullet_x+3 >= obstructs.x and bullet1.bullet_x+3 <= obstructs.x + 10 and bullet1.bullet_y+9 >= obstructs.y and bullet1.bullet_y+3 <= obstructs.y + 10:465 bullet1.isAlive = False466 obstructs.isAlive = False467def checkhit_invader():468 global fire_alive,game_state469 if fire_alive==True:470 for obstructs in obstruct1:471 if obstructs.isAlive:472 if fire_x+3 >= obstructs.x and fire_x+3 <= obstructs.x + 10 and fire_y+9 >= obstructs.y and fire_y+3 <= obstructs.y + 10:473 fire_alive = False474 obstructs.isAlive = False475 for obstructs in obstruct2:476 if obstructs.isAlive:477 if fire_x+3 >= obstructs.x and fire_x+3 <= obstructs.x + 10 and fire_y+9 >= obstructs.y and fire_y+3 <= obstructs.y + 10:478 fire_alive = False479 obstructs.isAlive = False480 for obstructs in obstruct3:481 if obstructs.isAlive:482 if fire_x+3 >= obstructs.x and fire_x+3 <= obstructs.x + 10 and fire_y+9 >= obstructs.y and fire_y+3 <= obstructs.y + 10:483 fire_alive = False484 obstructs.isAlive = False485 for obstructs in obstruct4:486 if obstructs.isAlive:487 if fire_x+3 >= obstructs.x and fire_x+3 <= obstructs.x + 10 and fire_y+9 >= obstructs.y and fire_y+3 <= obstructs.y + 10:488 fire_alive = False489 obstructs.isAlive = False490 if (ship_x-5 < fire_x < ship_x + ship.get_width()-5) and (fire_y > ship_y):491 game_state=2492 Game_end()493##MAIN494initial()495WelcomeScreen() ## contains initial screen496## game state=0 means start page... =1 means game is going on... =2 means game lost... =3 means won the game497game_state=0 498quit=False499while not quit:500 if not game_state:501 WelcomeScreen()502 elif game_state==1:503 Game()504 elif game_state == 2:505 Game_end()506 elif game_state ==3:507 Game_end()508 for event in pygame.event.get(): 509 if event.type == pygame.KEYDOWN:510 if event.key == pygame.K_LEFT:511 Left_press()512 if event.type == pygame.KEYDOWN:513 if event.key == pygame.K_RIGHT:514 Right_press()515 if event.type == pygame.KEYUP:516 if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:517 Release_arrowkey()518 if event.type == pygame.KEYDOWN:519 if event.key == pygame.K_SPACE:520 Fire_Spaceship()521 if event.type == pygame.QUIT:522 quit = True523 524 clock.tick(20) #Sets FPS of the game525 pygame.display.update()...

Full Screen

Full Screen

test_isalive.py

Source:test_isalive.py Github

copy

Full Screen

...23from . import PexpectTestCase24class IsAliveTestCase(PexpectTestCase.PexpectTestCase):25 """Various tests for the running status of processes."""26 def test_expect_wait(self):27 """Ensure consistency in wait() and isalive()."""28 p = pexpect.spawn('sleep 1')29 assert p.isalive()30 assert p.wait() == 031 assert not p.isalive()32 # In previous versions of ptyprocess/pexpect, calling wait() a second33 # time would raise an exception, but not since v4.034 assert p.wait() == 035 def test_expect_wait_after_termination(self):36 """Ensure wait on a process terminated by kill -9."""37 p = pexpect.spawn('sleep 3')38 assert p.isalive()39 p.kill(9)40 time.sleep(1)41 # when terminated, the exitstatus is None, but p.signalstatus42 # and p.terminated reflects that the kill -9 nature.43 assert p.wait() is None44 assert p.signalstatus == 945 assert p.terminated == True46 assert not p.isalive()47 def test_signal_wait(self):48 '''Test calling wait with a process terminated by a signal.'''49 if not hasattr(signal, 'SIGALRM'):50 return 'SKIP'51 p = pexpect.spawn(self.PYTHONBIN, ['alarm_die.py'])52 p.wait()53 assert p.exitstatus is None54 self.assertEqual(p.signalstatus, signal.SIGALRM)55 def test_expect_isalive_dead_after_normal_termination (self):56 p = pexpect.spawn('ls', timeout=15)57 p.expect(pexpect.EOF)58 assert not p.isalive()59 def test_expect_isalive_dead_after_SIGHUP(self):60 p = pexpect.spawn('cat', timeout=5, ignore_sighup=False)61 assert p.isalive()62 force = False63 if sys.platform.lower().startswith('sunos'):64 # On Solaris (SmartOs), and only when executed from cron(1), SIGKILL65 # is required to end the sub-process. This is done using force=True66 force = True67 assert p.terminate(force) == True68 p.expect(pexpect.EOF)69 assert not p.isalive()70 def test_expect_isalive_dead_after_SIGINT(self):71 p = pexpect.spawn('cat', timeout=5)72 assert p.isalive()73 force = False74 if sys.platform.lower().startswith('sunos'):75 # On Solaris (SmartOs), and only when executed from cron(1), SIGKILL76 # is required to end the sub-process. This is done using force=True77 force = True78 assert p.terminate(force) == True79 p.expect(pexpect.EOF)80 assert not p.isalive()81 def test_expect_isalive_dead_after_SIGKILL(self):82 p = pexpect.spawn('cat', timeout=5)83 assert p.isalive()84 p.kill(9)85 p.expect(pexpect.EOF)86 assert not p.isalive()87 def test_forced_terminate(self):88 p = pexpect.spawn(self.PYTHONBIN, ['needs_kill.py'])89 p.expect('READY')90 assert p.terminate(force=True) == True91 p.expect(pexpect.EOF)92 assert not p.isalive()93### Some platforms allow this. Some reset status after call to waitpid.94### probably not necessary, isalive() returns early when terminate is False.95 def test_expect_isalive_consistent_multiple_calls (self):96 '''This tests that multiple calls to isalive() return same value.97 '''98 p = pexpect.spawn('cat')99 assert p.isalive()100 assert p.isalive()101 p.sendeof()102 p.expect(pexpect.EOF)103 assert not p.isalive()104 assert not p.isalive()105if __name__ == '__main__':106 unittest.main()...

Full Screen

Full Screen

cgol.py

Source:cgol.py Github

copy

Full Screen

1import sys2import pyopencl as cl3import numpy as np4class CGOL():5 kernel = r"""6 int checkField(__global signed char *cells, int x, int y, int size){7 x = (x + size) % size;8 y = (y + size) % size;9 if (cells[x*size+y] > 0) return 1;10 else return 0;11 }12 __kernel void cgol(__global signed char *cells_in, __global signed char *cells_out, int size){13 int x = get_global_id(0);14 int y = get_global_id(1);15 16 int neighbours = 0;17 int isAlive = cells_in[x*size+y];18 for (int i = -1; i<2; i++){19 for (int j = -1; j<2; j++){20 if(i!=0 || j!=0){21 neighbours += checkField(cells_in, x+i, y+j, size);22 }23 }24 }25 //Any live cell with fewer than two live neighbours dies, as if by underpopulation.26 if(isAlive > 0 && neighbours < 2) isAlive = 0;27 //Any live cell with two or three live neighbours lives on to the next generation.28 else if(isAlive > 0 && neighbours < 4) isAlive = 1;29 //Any live cell with more than three live neighbours dies, as if by overpopulation.30 else if(isAlive > 0 && neighbours > 3) isAlive = 0;31 //Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.32 else if(isAlive == 0 && neighbours == 3)isAlive = 1;33 cells_out[x*size+y] = (signed char) isAlive;34 }35 """36 def __init__(self, size):37 self.cells = np.random.randint(low=2, size=(size,size), dtype=np.byte)38 self.size = size39 # Prep OpelCL40 self.context = cl.create_some_context()41 self.queue = cl.CommandQueue(self.context)42 self.cells_buffer = cl.Buffer(self.context, cl.mem_flags.READ_WRITE, self.cells.nbytes)43 self.cells_buffer_out = cl.Buffer(self.context, cl.mem_flags.READ_WRITE, self.cells.nbytes)44 cl.enqueue_copy(self.queue, self.cells_buffer, self.cells).wait()45 self.programm = cl.Program(self.context, self.kernel)46 try:47 self.programm.build()48 except Exception:49 print("Error:")50 print(self.programm.get_build_info(self.context.devices[0], cl.program_build_info.LOG))51 raise52 53 def calculate_next_generation(self):54 self.programm.cgol(55 self.queue, #queue56 (self.size,self.size), #global_size57 None, #local_size58 self.cells_buffer, # Argument 0 (cells_in)59 self.cells_buffer_out, # Argument 1 (cells_out)60 np.int32(self.size)) # Argument 2 (size)61 self.cells_buffer_out, self.cells_buffer = self.cells_buffer, self.cells_buffer_out62 def get_cells(self):63 cl.enqueue_copy(self.queue, self.cells, self.cells_buffer).wait()64 return self.cells65 def print_current_generation(self):66 cells = self.get_cells()67 s = ""68 for row in cells:69 for element in row:70 s+= f"\033[91mX \033[0m" if element == 1 else "\033[92mX \033[0m"71 s+='\n'72 print(s)73 def save_current_generation(self, path):74 cells = self.get_cells()75 s=""76 for row in cells:77 for element in row:78 s+= f"X " if element == 1 else "- "79 s+='\n'80 with open(path, "w") as f:81 f.write(s)82if __name__ == '__main__':83 SIZE = int(sys.argv[1])84 GENERATIONS = int(sys.argv[2])85 print(f'Start CGOL OPENCL')86 print(f"Generations: {GENERATIONS}")87 print(f"Size: {SIZE}")88 cgol = CGOL(size=SIZE)89 for i in range(GENERATIONS):90 cgol.calculate_next_generation()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pyatom automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful