Best Python code snippet using avocado_python
gmountman
Source:gmountman
...119 except AttributeError:120 icon = None121 name = v.get_name()122 mountpoint = None123 can_mount = v.can_mount()124 can_unmount = None125 can_eject = v.can_eject()126 devices.append([icon, name, node,127 mountpoint, can_mount,128 can_unmount, can_eject])129 return sorted(devices, key=lambda node: node[2])130 def update_list(self):131 device = None132 try:133 selectedline = self.treeview_devices.get_selection()134 self.lisstore_devices, iter = selectedline.get_selected()135 device = self.liststore_devices.get_value(iter, 2)136 except TypeError:137 pass...
board.py
Source:board.py
1from ship import Ship2from coord import Coord, coords_x, coords_y3import re4from errors import WrongCoords, CoordOccupied, BoardShipsError5from random import randrange6class Board():7 8 _board_cells = {}9 _ships = []10 def __init__(self):11 self.new_board()12 13 def __str__(self):14 return self.to_str()15 16 def to_str(self, hidden = False):17 s_all = ''18 s_head = ' |'19 for c in coords_y:20 s_head += f'{c}|'21 s_all += f'{s_head}'22 for r in coords_x:23 s_row = f'{r}|'24 for c in coords_y:25 cell = self.find_cell(c+r)26 s_row += f'{cell.to_str(hidden)}|'27 s_all += f'\n{s_row}'28 return s_all29 30 def find_cell(self, coords):31 return self._board_cells[coords]32 @staticmethod33 def get_coords(coords):34 req_string = f'(?i)^([{coords_y}][{coords_x}])\s*([{coords_y}][{coords_x}])?$'35 match = re.match(req_string, coords)36 if not match:37 raise WrongCoords('Ðе коÑÑекÑнÑе кооÑдинаÑÑ, они Ð´Ð¾Ð»Ð¶Ð½Ñ Ð±ÑÑÑ ÑоÑмаÑа A1 A2 или A1')38 coord1 = match.group(1)39 coord2 = match.group(2)40 if not coord2:41 coord2 = coord142 return str.upper(coord1), str.upper(coord2)43 44 @staticmethod45 def get_hit_coord(coord):46 req_string = f'(?i)^([{coords_y}][{coords_x}])$'47 match = re.match(req_string, coord)48 if not match:49 raise WrongCoords('Ðе коÑÑекÑнÑе кооÑдинаÑÑ, они Ð´Ð¾Ð»Ð¶Ð½Ñ Ð±ÑÑÑ ÑоÑмаÑа A1')50 coord1 = match.group(1)51 return str.upper(coord1)52 53 @staticmethod54 def extract_coords(coords):55 try:56 coord1, coord2 = Board.get_coords(coords)57 except WrongCoords as err:58 raise WrongCoords(err)59 x1, y1 = Coord.get_coords_xy(coord1)60 x2, y2 = Coord.get_coords_xy(coord2)61 if x1-x2 != 0 and y1-y2 != 0:62 raise WrongCoords('ÐоÑÐ°Ð±Ð»Ñ Ð´Ð¾Ð»Ð¶ÐµÐ½ бÑÑÑ Ð¿Ð¾ÑÑавлен в линиÑ, напÑимеÑ: A1 A2')63 64 if x1 > x2:65 x1, x2 = x2, x166 if y1 > y2:67 y1, y2 = y2, y168 69 return x1, y1, x2, y270 71 def can_mount_ship(self, coords, ship):72 x1, y1, x2, y2 = self.extract_coords(coords)73 set_ship_length = max(abs(x2-x1), abs(y2-y1)) + 174 if set_ship_length != ship.length:75 return False76 77 can_mount = True78 for x in range(x1, x2+1):79 for y in range(y1, y2+1):80 cell_coord = Coord.get_coords(x, y)81 board_cell = self.find_cell(cell_coord)82 if board_cell.busy:83 can_mount = False84 return can_mount85 86 def set_next_ship(self, coords):87 ship_mounted = False88 for ship in self._ships:89 x1, y1, x2, y2 = self.extract_coords(coords)90 set_ship_length = max(abs(x2-x1), abs(y2-y1)) + 191 if not ship.mounted and set_ship_length == ship.length:92 ship_mounted = self.set_ship(coords, ship)93 break94 if not ship_mounted:95 raise WrongCoords(f'ÐÑе коÑабли ÑазмеÑа {set_ship_length} Ñже ÑÑÑановленÑ')96 97 def set_ship(self, coords, ship):98 if not self.can_mount_ship(coords, ship):99 raise CoordOccupied('Ðевозможно поÑÑавиÑÑ ÐºÐ¾ÑаблÑ')100 101 x1, y1, x2, y2 = self.extract_coords(coords)102 103 ship.mounted = True104 x_b1 = x1 if x1 == 0 else x1 - 1105 x_b2 = x2 if x2 + 1 == len(coords_x) else x2 + 1106 y_b1 = y1 if y1 == 0 else y1 - 1107 y_b2 = y2 if y2 + 1 == len(coords_y) else y2 + 1108 for x in range(x1, x2+1):109 for y in range(y1, y2+1):110 cell_coord = Coord.get_coords(x, y)111 board_cell = self.find_cell(cell_coord)112 board_cell.ship = ship113 for x in range(x_b1, x_b2 + 1):114 for y in range(y_b1, y_b2 + 1):115 cell_coord = Coord.get_coords(x, y)116 board_cell = self.find_cell(cell_coord)117 if not board_cell.busy:118 board_cell.busy = True119 120 return True121 def get_unmounted_ships(self):122 val = []123 for ship in self._ships:124 if not ship.mounted:125 val.append(ship)126 return val127 def check_can_mount_next_ship(self):128 for ship in self._ships:129 if not ship.mounted:130 empty_coords = []131 for coord, cell in self._board_cells.items():132 if not cell.busy:133 empty_coords.append(coord)134 if empty_coords and not len(empty_coords):135 raise BoardShipsError('Ðе возможно поÑÑавиÑÑ ÐµÑе один коÑаблÑ, ÑекÑÑÐ°Ñ Ð¸Ð³Ñа бÑÐ´ÐµÑ Ð·Ð°Ð²ÐµÑÑена!')136 137 can_mount = False138 for coord in empty_coords:139 directions = self.fill_possible_directions(ship, coord)140 if directions and len(directions):141 can_mount = True142 return can_mount143 def set_ships_random(self):144 for ship in self._ships:145 if not ship.mounted:146 cnt = 0147 while True:148 cnt += 1149 coord = self.get_random_un_busy_coord()150 directions = self.fill_possible_directions(ship, coord)151 if directions and len(directions):152 ship_coords = directions[randrange(len(directions))]153 try:154 self.set_ship(ship_coords, ship)155 break156 except CoordOccupied as msg:157 raise CoordOccupied(msg)158 if cnt > 10:159 raise BoardShipsError('Ðе возможно поÑÑавиÑÑ ÐµÑе один коÑаблÑ, ÑекÑÑÐ°Ñ Ð¸Ð³Ñа бÑÐ´ÐµÑ Ð·Ð°Ð²ÐµÑÑена!')160 161 def fill_possible_directions(self, ship, coord):162 x, y = Coord.get_coords_xy(coord)163 ship_shift = ship.length - 1164 directions = []165 if ship_shift == 0:166 directions.append(coord)167 else: 168 self.add_possible_direction(directions, ship, coord, x, y + ship_shift)169 self.add_possible_direction(directions, ship, coord, x, y - ship_shift)170 self.add_possible_direction(directions, ship, coord, x + ship_shift, y)171 self.add_possible_direction(directions, ship, coord, x - ship_shift, y)172 return directions173 174 def add_possible_direction(self, directions, ship, coord1, x, y):175 try:176 coord2_check = Coord.get_coords(x, y)177 coord2 = self.get_hit_coord(coord2_check)178 if coord2:179 coords = f'{coord1} {coord2}'180 if self.can_mount_ship(coords, ship):181 directions.append(coords)182 except:183 return None 184 def make_shot_and_hit(self, input_coord):185 try:186 coord = self.get_hit_coord(input_coord)187 except WrongCoords as msg:188 raise WrongCoords(msg)189 190 cell = self.find_cell(coord)191 if cell.is_hit:192 raise WrongCoords(f'Ðо кооÑдинаÑе {coord} Ñже бÑл вÑÑÑÑел!')193 194 cell.is_hit = True195 if cell.ship:196 ship_hits = 0197 for cell_search in self._board_cells.values():198 if cell_search.ship == cell.ship and cell_search.is_hit:199 ship_hits += 1200 201 if ship_hits == cell.ship.length:202 cell.ship.sunk = True203 return True204 return False205 206 def all_ships_sunk(self):207 for ship in self._ships:208 if not ship.sunk:209 return False210 return True211 212 def new_board(self):213 self._board_cells = {}214 self._ships = []215 for r in coords_x:216 for c in coords_y:217 coord_string = c+r218 new_coord = Coord(coord_string)219 self._board_cells[coord_string] = new_coord220 self._ships.append(Ship(3, 1)) 221 222 for r in range(1, 3):223 ship = Ship(2, r)224 self._ships.append(ship) 225 for r in range(1, 4):226 ship = Ship(1, r)227 self._ships.append(ship) 228 229 def get_random_un_busy_coord(self):230 empty_coords = []231 for coord, cell in self._board_cells.items():232 if not cell.busy:233 empty_coords.append(coord)234 235 if empty_coords and not len(empty_coords):236 raise BoardShipsError('Ðе возможно опÑеделиÑÑ ÑвободнÑÑ ÑÑейкÑ! Такого не должно бÑÑÑ!')237 coord = empty_coords[randrange(len(empty_coords))]238 return coord239 240 def get_random_not_hit_coord(self):241 empty_coords = []242 for coord, cell in self._board_cells.items():243 if not cell.is_hit:244 empty_coords.append(coord)245 246 if empty_coords and not len(empty_coords):247 raise BoardShipsError('Ðе возможно опÑеделиÑÑ ÑвободнÑÑ ÑÑейкÑ! Такого не должно бÑÑÑ!')248 coord = empty_coords[randrange(len(empty_coords))]249 return coord250 251 252if __name__ == '__main__':253 board = Board()254 # board.SetShipRandom()255 # try:256 # board.set_ship('A1 C8')257 # except WrongCoords as err:258 # print (err)259 # except CoordOccupied as err:260 # print (err)261 # board.set_ship('A4')262 # board.set_ship('E2 C2')263 # board.set_ship('E6 D6')264 # board.set_ship('E4 E5')265 # board.set_ship('A3 B3')266 # board.set_ship('D4 E4')267 # board.set_ship('B5 B5')268 # board.set_ship('A2 A2')269 # board.set_next_ship('E4 E2')270 # board.set_next_ship('B2 B3')271 # board.set_next_ship('A6 B6')272 # board.set_next_ship('F6 F6')273 # board.set_next_ship('D6 D6')274 # board.set_next_ship('E4 C4')275 # board.set_next_ship('F6 D6')276 # board.set_next_ship('D1 B1')277 # board.set_next_ship('A5 A3')278 # if not board.check_can_mount_next_ship():279 # print('не возможно поÑÑавиÑÑ ÑледÑÑÑий коÑаблÑ')280 try:281 # board.set_next_ship('A1')282 board.set_ships_random()283 board.make_shot_and_hit('A1')284 board.make_shot_and_hit('B2')285 board.make_shot_and_hit('C3')286 board.make_shot_and_hit('D4')287 except BoardShipsError as msg:288 print(msg)289 # pass...
user_mount.py
Source:user_mount.py
1# -*- coding: utf-8 -*-2#3# Copyright 2013, Junta de Andalucia4# http://www.juntadeandalucia.es/5#6# Authors:7# Jose Luis Salvador <salvador.joseluis@gmail.com>8#9# All rights reserved - EUPL License V 1.110# https://joinup.ec.europa.eu/software/page/eupl/licence-eupl11#12from gecoscc.api.gpoconversors import GPOConversor13class UserMount(GPOConversor):14 policy = None15 def __init__(self, db):16 super(UserMount, self).__init__(db)17 self.policy = self.db.policies.find_one({'slug': 'user_mount_res'})18 def convert(self, xmlgpo):19 if self.policy is None:20 return None21 result = [{22 'policies': [],23 'guids': [],24 }]25 # Get value from GPO26 can_mount = False27 nodes = self.getNodesFromPath(xmlgpo, ['User', 'ExtensionData', 'Extension', 'Policy']) # Get value from policy28 for node in nodes:29 if 'Name' in node and node['Name'] == 'Quitar "Conectar a unidad de red" y "Desconectar de unidad de red"' and node['State'] is not None:30 can_mount = False if node['State'] != 'Enabled' else True31 break32 nodes = self.getNodesFromPath(xmlgpo, ['Computer', 'ExtensionData', 'Extension', 'RegistrySettings', 'Registry', 'Properties']) # Get value from Computer Registry33 for node in nodes:34 if '@name' in node and node['@name'] == 'NoNetConnectDisconnect' and node['@value'] is not None:35 can_mount = False if int(node['@value'], 16) == 1 else True36 break37 nodes = self.getNodesFromPath(xmlgpo, ['User', 'ExtensionData', 'Extension', 'RegistrySettings', 'Registry', 'Properties']) # Get value from User Registry38 for node in nodes:39 if '@name' in node and node['@name'] == 'NoNetConnectDisconnect' and node['@value'] is not None:40 can_mount = False if int(node['@value'], 16) == 1 else True41 break42 # Generate GECOSCC Policies43 result[0]['policies'] = [44 {45 str(self.policy['_id']): {46 'can_mount': can_mount47 }48 }49 ]50 # Get user GUID from SID51 nodes = self.getNodesFromPath(xmlgpo, ['SecurityDescriptor', 'Permissions', 'TrusteePermissions', 'Trustee', 'SID'])52 for node in nodes:53 guid = self.get_guid_from_sid(node['#text'])54 if guid is not None:55 result[0]['guids'].append(guid)...
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!!