Best Python code snippet using ATX
glnav.py
Source:glnav.py
...149 """Change the background colour of the widget."""150 self.r_back = r151 self.g_back = g152 self.b_back = b153 self._redraw()154 def set_centerpoint(self, x, y, z):155 """Set the new center point for the model.156 This is where we are looking."""157 self.xcenter = x158 self.ycenter = y159 self.zcenter = z160 self._redraw()161 def set_latitudelimits(self, minlat, maxlat):162 """Set the new "latitude" limits for rotations."""163 if maxlat > 180:164 return165 if minlat < -180:166 return167 if maxlat <= minlat:168 return169 self.maxlat = maxlat170 self.minlat = minlat171 self._redraw()172 def set_eyepoint(self, distance):173 """Set how far the eye is from the position we are looking."""174 self.distance = distance175 self._redraw()176 def set_eyepoint_from_extents(self, e1, e2):177 """Set how far the eye is from the position we are looking178 based on the screen width and height of a subject."""179 w = self.winfo_width()180 h = self.winfo_height()181 ztran = max(2.0, e1, e2 * w/h) ** 2182 self.set_eyepoint(ztran - self.zcenter)183 def reset(self):184 """Reset rotation matrix for this widget."""185 glMatrixMode(GL_MODELVIEW)186 glLoadIdentity()187 self._redraw()188 def recordMouse(self, x, y):189 self.xmouse = x190 self.ymouse = y191 def startRotate(self, x, y):192 self.recordMouse(x, y)193 def scale(self, x, y):194 """Scale the scene. Achieved by moving the eye position.195 Dragging up zooms in, while dragging down zooms out196 """197 scale = 1 - 0.01 * (event.y - self.ymouse)198 # do some sanity checks, scale no more than199 # 1:1000 on any given click+drag200 if scale < 0.001:201 scale = 0.001202 elif scale > 1000:203 scale = 1000204 newdistance = self.distance * scale205 if newdistance < 1e-30 or newdistance > 1e30:206 return207 self.distance = newdistance208 self._redraw()209 self.recordMouse(x, y)210 def rotate(self, x, y):211 """Perform rotation of scene."""212 self.activate()213 self.perspective = True214 glRotateScene(self, 0.5, self.xcenter, self.ycenter, self.zcenter, x, y, self.xmouse, self.ymouse)215 self._redraw()216 self.recordMouse(x, y)217 def translate(self, x, y):218 """Perform translation of scene."""219 self.activate()220 # Scale mouse translations to object viewplane so object tracks with mouse221 win_height = max( 1,self.winfo_height() )222 obj_c = ( self.xcenter, self.ycenter, self.zcenter )223 win = gluProject( obj_c[0], obj_c[1], obj_c[2])224 obj = gluUnProject( win[0], win[1] + 0.5 * win_height, win[2])225 dist = math.sqrt( v3distsq( obj, obj_c ) )226 scale = abs( dist / ( 0.5 * win_height ) )227 glTranslateScene(self, scale, x, y, self.xmouse, self.ymouse)228 self._redraw()229 self.recordMouse(x, y)230 def set_viewangle(self, lat, lon):231 self.lat = lat232 self.lon = lon233 glRotateScene(self, 0.5, self.xcenter, self.ycenter, self.zcenter, 0, 0, 0, 0)234 self.tkRedraw()235 def get_zoom_distance(self):236 data = self.distance237 return data238 def set_zoom_distance(self,data):239 self.distance = data240 self._redraw()241 def zoomin(self):242 self.distance = self.distance / 1.1243 self._redraw()244 def zoomout(self):245 self.distance = self.distance * 1.1246 self._redraw()247 def startZoom(self, y):248 self.y0 = y249 self.original_zoom = self.distance250 def continueZoom(self, y):251 dy = y - self.y0252 self.distance = self.original_zoom * pow(1.25, dy / 16.)253 self._redraw()254 def getRotateMode(self): return False255 def translateOrRotate(self, x, y):256 if self.getRotateMode():257 self.rotate(x, y)258 else:259 self.translate(x, y)260 def rotateOrTranslate(self, x, y):261 if not self.getRotateMode():262 self.rotate(x, y)263 else:264 self.translate(x, y)265 def set_view_x(self):266 self.reset()267 glRotatef(-90, 0, 1, 0)268 glRotatef(-90, 1, 0, 0)269 mid, size = self.extents_info()270 glTranslatef(-mid[0], -mid[1], -mid[2])271 self.set_eyepoint_from_extents(size[1], size[2])272 self.perspective = False273 self.lat = -90274 self.lon = 270275 self._redraw()276 def set_view_y(self):277 self.reset()278 glRotatef(-90, 1, 0, 0)279 if self.is_lathe():280 glRotatef(90, 0, 1, 0)281 mid, size = self.extents_info()282 glTranslatef(-mid[0], -mid[1], -mid[2])283 self.set_eyepoint_from_extents(size[0], size[2])284 self.perspective = False285 self.lat = -90286 self.lon = 0287 self._redraw()288 # lathe backtool display289 def set_view_y2(self):290 self.reset()291 glRotatef(90, 1, 0, 0)292 glRotatef(90, 0, 1, 0)293 mid, size = self.extents_info()294 glTranslatef(-mid[0], -mid[1], -mid[2])295 self.set_eyepoint_from_extents(size[0], size[2])296 self.perspective = False297 self.lat = -90298 self.lon = 0299 self._redraw()300 def set_view_z(self):301 self.reset()302 mid, size = self.extents_info()303 glTranslatef(-mid[0], -mid[1], -mid[2])304 self.set_eyepoint_from_extents(size[0], size[1])305 self.perspective = False306 self.lat = self.lon = 0307 self._redraw()308 def set_view_z2(self):309 self.reset()310 glRotatef(-90, 0, 0, 1)311 mid, size = self.extents_info()312 glTranslatef(-mid[0], -mid[1], -mid[2])313 self.set_eyepoint_from_extents(size[1], size[0])314 self.perspective = False315 self.lat = 0316 self.lon = 270317 self._redraw()318 def set_view_p(self):319 self.reset()320 self.perspective = True321 mid, size = self.extents_info()322 glTranslatef(-mid[0], -mid[1], -mid[2])323 size = (size[0] ** 2 + size[1] ** 2 + size[2] ** 2) ** .5324 if size > 1e99: size = 5. # in case there are no moves in the preview325 w = self.winfo_width()326 h = self.winfo_height()327 fovx = self.fovy * w / h328 fov = min(fovx, self.fovy)329 self.set_eyepoint((size * 1.1 + 1.0) / 2 / math.sin ( fov * math.pi / 180 / 2))330 self.lat = -60331 self.lon = 335332 glRotateScene(self, 1.0, mid[0], mid[1], mid[2], 0, 0, 0, 0)333 self._redraw()...
grid_game.py
Source:grid_game.py
...36 mh = self.grid.height() - my37 self.messageRect = (mx, my, mw, mh)38 self.screen = pygame.display.set_mode([self.grid.width(), self.grid.height()])39 pygame.display.set_caption(mapdata['title'])40 self._redraw()41 def load_locks(self):42 self.locks = []43 if 'locks' not in self.mapdata:44 return45 for lockdata in self.mapdata['locks']:46 code = lockdata['code']47 if isinstance(code, str):48 lock = Lock(code)49 elif isinstance(code, Iterable):50 lock = Lock(*code)51 else:52 lock = Lock(code)53 if 'label' in lockdata:54 lock.message_in_front = lockdata['label']55 if 'message_wrong_code' in lockdata:56 lock.message_wrong_code = lockdata['message_wrong_code']57 if 'auto_destroys' in lockdata:58 lock.auto_destroys = lockdata['auto_destroys']59 if 'position' in lockdata:60 lock.position = lockdata['position']61 self.locks.append(lock)62 def load_friends(self):63 self.friends = []64 if 'friends' not in self.mapdata:65 return66 for data in self.mapdata['friends']:67 friend = Friend(data['data'], data['message'])68 self.friends.append(friend)69 def _redraw(self, player_x=None, player_y=None):70 # This is required so that the app does not appear hanged71 for event in pygame.event.get():72 if event.type == pygame.QUIT:73 self.running = False74 # Cancel redraw if we are not running anymore75 if not self.running:76 return77 # Fill the background78 self.screen.fill((0, 161, 228))79 # Draw cells80 for row in range(self.grid.rows):81 pygame.draw.line(self.screen, (0, 180, 240), (0, row * CELL), (self.grid.width(), row * CELL))82 for col in range(self.grid.cols):83 pygame.draw.line(self.screen, (0, 180, 240), (col * CELL, 0), (col * CELL, self.grid.height()))84 # Draw the walls and the diamond85 for pos, obj in self.grid:86 if obj == WALL:87 self.screen.blit(WALL_TILE, self.grid.rect(pos))88 elif obj == DIAMOND:89 self.screen.blit(DIAMOND_TILE, self.grid.rect(pos))90 elif obj == LOCK:91 self.screen.blit(LOCK_TILE, self.grid.rect(pos))92 elif obj == GREEN_LOCK:93 self.screen.blit(GREEN_LOCK_TILE, self.grid.rect(pos))94 elif obj == FRIEND:95 self.screen.blit(FRIEND_TILE, self.grid.rect(pos))96 # Draw the message board97 if self.messageRect is not None:98 pygame.draw.rect(self.screen, (0, 161, 228), self.messageRect)99 labels = []100 x = self.messageRect[0]101 y = self.messageRect[1]102 if isinstance(self.message, list):103 lines = self.message104 else:105 lines = self.message.splitlines()106 for line in lines:107 label = self.font.render(line, 1, (0, 0, 0))108 self.screen.blit(label, (x, y))109 y += 35110 # Draw the player111 x = player_x or self.grid.xy(self.col, self.row)[0]112 y = player_y or self.grid.xy(self.col, self.row)[1]113 if self.won:114 self.screen.blit(PLAYER_WON_TILE, (x, y, CELL, CELL))115 else:116 self.screen.blit(PLAYER_TILE, (x, y, CELL, CELL))117 # update the display118 pygame.display.update()119 # Sleep for one frame120 self.clock.tick(60)121 def get_lock_in_front(self):122 for lock in self.locks:123 if lock.position is not None:124 if lock.position == [self.col+1, self.row]:125 return lock126 else:127 return lock128 raise Exception("No lock found")129 def look(self):130 obj = self.grid.object_in_sight()131 result = None132 if obj == LOCK:133 result = 'orange lock'134 elif obj == GREEN_LOCK:135 result = 'green lock'136 if result is None:137 self.message = "There is nothing in sight"138 else:139 self.message = [140 f"In front of you you see {result}.",141 f"(the look() function returns '{result}')"142 ]143 self._redraw()144 return result145 def move(self, col_step, row_step):146 newcol = self.col + col_step147 newrow = self.row + row_step148 # Check for collision149 if self.grid[newcol, newrow] in (WALL, LOCK, GREEN_LOCK, FRIEND):150 return151 # Animate movement152 oldx, oldy = self.grid.xy(self.col, self.row)153 self.col = newcol154 self.row = newrow155 x, y = self.grid.xy(self.col, self.row)156 frames = 15157 dx = (x - oldx) / frames158 dy = (y - oldy) / frames159 for i in range(frames + 1):160 self._redraw(oldx + dx * i, oldy + dy * i)161 # Check if we ended up in front of a lock162 if self.grid[self.col + 1, self.row] == LOCK:163 # Show what the lock says164 self.message = self.get_lock_in_front().message_in_front165 self._redraw()166 # Check if we ended up in front of a friend167 if self.grid[self.col + 1, self.row] == FRIEND:168 self.message = self.friends[0].message_in_front169 self._redraw()170 # Check if we have reached the diamond171 if self.grid[self.col, self.row] == DIAMOND:172 self.message = "Congratulations! You won!!!"173 del self.grid[self.col, self.row] # removing the diamond174 self.won = True175 self._redraw()176 def open_lock(self, *codes):177 self.message = f"You are trying to open the lock with code: {', '.join([str(x) for x in codes])}."178 self._redraw()179 sleep(3)180 # Check if we are in front of a lock181 if self.grid[self.col + 1, self.row] not in (LOCK, GREEN_LOCK):182 self.message = "There is no lock in front of you"183 self._redraw()184 return185 # Check if the codes are correct186 lock = self.get_lock_in_front()187 if not lock.open(*codes):188 self.message = lock.message_wrong_code189 if lock.auto_destroys:190 self.grid[self.col+1, self.row] = 'X'191 self.locks.remove(lock)192 self._redraw()193 sleep(1)194 return195 # If the code is correct196 self.message = lock.message_when_open197 del self.grid[self.col + 1, self.row]198 self.locks.remove(lock)199 self._redraw()200 sleep(1)201 def ask(self):202 sleep(.5)203 # Check if we are in front of a friend204 if self.grid[self.col + 1, self.row] != FRIEND:205 self.message = "There is no one to ask in front of you."206 self._redraw()207 return208 friend = self.friends[0]209 self.message = friend.message210 self._redraw()211 sleep(1)212 self.friends.pop(0)213 del self.grid[self.col + 1, self.row]214 self._redraw()215 sleep(1)216 return friend.data217 def run(self):218 while self.running:219 # Did the user click the window close button?220 for event in pygame.event.get():221 if self.keyboard and event.type == pygame.KEYUP:222 if event.key == pygame.K_LEFT:223 self.move(-1, 0)224 elif event.key == pygame.K_RIGHT:225 self.move(1, 0)226 elif event.key == pygame.K_DOWN:227 self.move(0, 1)228 elif event.key == pygame.K_UP:...
ch11-3.py
Source:ch11-3.py
...23 tolN=StockCode.get()+'.sz'24 else :25 print u"ä»
æ¯ææ·±è¯"26 return tolN27def _redraw():28 _redraw.f.clf()29 quotes = quotes_historical_yahoo( getInputs(), date1, date2)30 if len(quotes) == 0:31 raise SystemExit32 dates = [q[0] for q in quotes]33 opens = [q[1] for q in quotes]34 fig =Figure(figsize=(5,4), dpi=100)35 ax = _redraw.f.add_subplot(111)36 ax.plot_date(dates, opens, '-')37 _redraw.canvas.show()38 _redraw.canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)39_redraw.f =Figure(figsize=(5,4), dpi=100)40_redraw.canvas = FigureCanvasTkAgg(_redraw.f, master=root)41button = Tk.Button(master=root, text='redraw', command=_redraw)42button.pack(side=Tk.BOTTOM)43Tk.Label(root, text=u"è¡ç¥¨ä»£ç ").pack(side=Tk.TOP)44StockCode = Tk.Entry(root)45StockCode.pack()46StockCode.insert(0,'000001')47_redraw()...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!