How to use name_or_alias method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

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

help_provider.py

Source: help_provider.py Github

copy
1# -*- coding: utf-8 -*-
2# Copyright 2012 Google Inc. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15"""Module defining help types and providers for gsutil commands."""
16
17from __future__ import absolute_import
18
19import collections
20from gslib.exception import CommandException
21
22ALL_HELP_TYPES = ['command_help', 'additional_help']
23
24# Constants enforced by SanityCheck
25MAX_HELP_NAME_LEN = 16
26MIN_ONE_LINE_SUMMARY_LEN = 10
27MAX_ONE_LINE_SUMMARY_LEN = 80 - MAX_HELP_NAME_LEN
28
29DESCRIPTION_PREFIX = """
30<B>DESCRIPTION</B>"""
31
32SYNOPSIS_PREFIX = """
33<B>SYNOPSIS</B>"""
34
35
36class HelpProvider(object):
37  """Interface for providing help."""
38
39  # Each subclass of HelpProvider define a property named 'help_spec' that is
40  # an instance of the following class.
41  HelpSpec = collections.namedtuple('HelpSpec', [
42      # Name of command or auxiliary help info for which this help applies.
43      'help_name',
44      # List of help name aliases.
45      'help_name_aliases',
46      # Type of help.
47      'help_type',
48      # One line summary of this help.
49      'help_one_line_summary',
50      # The full help text.
51      'help_text',
52      # Help text for subcommands of the command's help being specified.
53      'subcommand_help_text',
54  ])
55
56  # Each subclass must override this with an instance of HelpSpec.
57  help_spec = None
58
59
60# This is a static helper instead of a class method because the help loader
61# (gslib.commands.help._LoadHelpMaps()) operates on classes not instances.
62def SanityCheck(help_provider, help_name_map):
63  """Helper for checking that a HelpProvider has minimally adequate content."""
64  # Sanity check the content.
65  assert (len(help_provider.help_spec.help_name) > 1
66          and len(help_provider.help_spec.help_name) < MAX_HELP_NAME_LEN)
67  for hna in help_provider.help_spec.help_name_aliases:
68    assert hna
69  one_line_summary_len = len(help_provider.help_spec.help_one_line_summary)
70  assert (one_line_summary_len > MIN_ONE_LINE_SUMMARY_LEN
71          and one_line_summary_len < MAX_ONE_LINE_SUMMARY_LEN)
72  assert len(help_provider.help_spec.help_text) > 10
73
74  # Ensure there are no dupe help names or aliases across commands.
75  name_check_list = [help_provider.help_spec.help_name]
76  name_check_list.extend(help_provider.help_spec.help_name_aliases)
77  for name_or_alias in name_check_list:
78    if help_name_map.has_key(name_or_alias):
79      raise CommandException(
80          'Duplicate help name/alias "%s" found while loading help from %s. '
81          'That name/alias was already taken by %s' % (
82              name_or_alias, help_provider.__module__,
83              help_name_map[name_or_alias].__module__))
84
85
86def CreateHelpText(synopsis, description):
87  """Helper for adding help text headers given synopsis and description."""
88  return SYNOPSIS_PREFIX + synopsis + DESCRIPTION_PREFIX + description
89
Full Screen

worldstate.py

Source: worldstate.py Github

copy
1# -*- coding: utf-8 -*-
2
3from sdtp.alias_table import AliasTable
4from sdtp.lkp_table import lkp_table
5from sdtp.lp_table import lp_table
6
7import logging
8from sqlalchemy import Integer
9import sys
10import threading
11import time
12
13class WorldState(threading.Thread):
14    def __init__ ( self, controller ):
15        super ( self.__class__, self ).__init__ ( )
16        self.controller = controller
17        self.keep_running = True
18        self.logger = logging.getLogger(__name__)
19
20        self.online_players = []
21        self.online_players_count = 100000
22        self.online_players_changed = False
23        self.latest_day = 0
24        self.latest_hour = 0
25        self.server_empty = False
26        self.zombies_alive = -1
27
28    # Thread control
29    def run ( self ):
30        self.logger.info("Start.")
31        self.register_callbacks ( )
32        while ( self.keep_running ):
33            time.sleep ( 0.1 )
34        self.deregister_callbacks ( )
35        self.logger.debug("world_state.run returning" )
36
37    def stop ( self ):
38        self.keep_running = False
39        self.logger.info("Stop.")
40
41    # Component-specific
42    ####################
43
44    def register_callbacks ( self ):
45        self.controller.dispatcher.register_callback(
46            "gt command output", self.log_time_of_day)
47        self.controller.dispatcher.register_callback(
48            "lkp output", self.parse_lkp_output)
49        self.controller.dispatcher.register_callback(
50            "lp output", self.update_lkp_table )
51        self.controller.dispatcher.register_callback(
52            "lp output", self.update_lp_table )
53        self.controller.dispatcher.register_callback(
54            "le/lp output footer", self.update_online_players_count )
55        self.controller.dispatcher.register_callback(
56            "mem output", self.update_zombies_alive)
57        self.controller.dispatcher.register_callback(
58            "player joined", self.player_connected)
59        self.controller.dispatcher.register_callback(
60            "player left", self.player_disconnected)
61
62    def deregister_callbacks ( self ):
63        self.controller.dispatcher.deregister_callback(
64            "gt command output", self.log_time_of_day)
65        self.controller.dispatcher.deregister_callback(
66            "lkp output", self.parse_lkp_output)
67        self.controller.dispatcher.deregister_callback(
68            "lp output", self.update_lkp_table )
69        self.controller.dispatcher.deregister_callback (
70            "lp output", self.update_lp_table )
71        self.controller.dispatcher.deregister_callback (
72            "le/lp output footer", self.update_online_players_count )
73        self.controller.dispatcher.deregister_callback(
74            "mem output", self.update_zombies_alive)
75        self.controller.dispatcher.deregister_callback(
76            "player joined", self.player_connected)
77        self.controller.dispatcher.deregister_callback(
78            "player left", self.player_disconnected)
79
80    def player_connected(self, match_group):
81        self.logger.info("Player {} connected.".format(match_group[7]))
82        self.online_players_changed = True
83        
84    def player_disconnected(self, match_group):
85        self.logger.info("Player {} disconnected.".format(match_group[7]))
86        self.online_players_changed = True
87        
88    def update_lkp_table ( self, match_group ):
89        self.logger.debug("update_lkp_table for {}.".format(match_group[1]))
90        self.logger.debug("({})".format (match_group))
91        this_steamid = int ( match_group [ 15 ] ),
92        results = self.controller.database.blocking_consult(
93            lkp_table, [ ( lkp_table.steamid, "==", match_group [ 15 ] ) ])
94        self.logger.debug("results = {}.".format ( results ) )
95        if len ( results ) == 0:
96            self.logger.info("New lkp entry: {}".format(match_group[1]))
97            self.controller.database.blocking_add(
98                lkp_table,
99                [ lkp_table (
100                    player_id = match_group [ 0 ],
101                    name = match_group [ 1 ],
102                    longitude = match_group [ 2 ],
103                    height = match_group [ 3 ],
104                    latitude = match_group [ 4 ],
105                    rotation_height = match_group [ 5 ],
106                    rotation_longitude = match_group [ 6 ],
107                    rotation_latitude = match_group [ 7 ],
108                    remote = match_group [ 8 ],
109                    health = match_group [ 9 ],
110                    deaths = match_group [ 10 ],
111                    zombies = match_group [ 11 ],
112                    players = match_group [ 12 ],
113                    score = match_group [ 13 ],
114                    level = match_group [ 14 ],
115                    steamid = match_group [ 15 ],
116                    ip = match_group [ 16 ],
117                    ping = match_group [ 17 ] ) ] )
118        else:
119            self.logger.debug("Update lkp entry." )
120            entry = results [ 0 ]
121            self.logger.debug("entry before: {}".format(entry))
122            entry [ "name" ] = match_group [ 1 ]
123            entry [ "longitude" ] = match_group [ 2 ]
124            entry [ "height" ] = match_group [ 3 ]
125            entry [ "latitude" ] = match_group [ 4 ]
126            entry [ "rotation_height" ] = match_group [ 5 ]
127            entry [ "rotation_longitude" ] = match_group [ 6 ]
128            entry [ "rotation_latitude" ] = match_group [ 7 ]
129            entry [ "remote" ] = match_group [ 8 ]
130            entry [ "health" ] = match_group [ 9 ]
131            entry [ "deaths" ] = match_group [ 10 ]
132            entry [ "zombies" ] = match_group [ 11 ]
133            entry [ "players" ] = match_group [ 12 ]
134            entry [ "score" ] = match_group [ 13 ]
135            entry [ "level" ] = match_group [ 14 ]
136            entry["steamid"] = match_group[15]
137            entry [ "ip" ] = match_group [ 16 ]
138            entry [ "ping" ] = match_group [ 17 ]
139            self.logger.debug("entry after: {}".format(entry))
140            self.controller.database.blocking_update(
141                lkp_table,
142                entry )
143            self.logger.debug("added entry." )
144        self.logger.debug("returning." )
145
146    def update_lp_table ( self, match_group ):
147        self.logger.debug("({})".format (match_group))
148        if self.online_players_changed:
149            self.controller.database.blocking_delete(
150                lp_table, [])
151            self.online_players_changed = False
152        this_steamid = int ( match_group [ 15 ] )
153        results = self.controller.database.blocking_consult (
154            lp_table, [ ( lp_table.steamid, "==", match_group [ 15 ] ) ])
155        self.logger.debug("results = {}.".format ( results ) )
156        if len ( results ) == 0:
157            self.logger.debug("New entry." )
158            self.controller.database.blocking_add(
159                lp_table,
160                [ lp_table (
161                    player_id = match_group [ 0 ],
162                    name = match_group [ 1 ],
163                    longitude = match_group [ 2 ],
164                    height = match_group [ 3 ],
165                    latitude = match_group [ 4 ],
166                    rotation_height = match_group [ 5 ],
167                    rotation_longitude = match_group [ 6 ],
168                    rotation_latitude = match_group [ 7 ],
169                    remote = match_group [ 8 ],
170                    health = match_group [ 9 ],
171                    deaths = match_group [ 10 ],
172                    zombies = match_group [ 11 ],
173                    players = match_group [ 12 ],
174                    score = match_group [ 13 ],
175                    level = match_group [ 14 ],
176                    steamid = match_group [ 15 ],
177                    ip = match_group [ 16 ],
178                    ping = match_group [ 17 ] ) ])
179        else:
180            self.logger.debug("Update lp entry." )
181            entry = results [ 0 ]
182            self.logger.debug("entry before: {}".format(entry))
183            entry [ "name" ] = match_group [ 1 ]
184            entry [ "longitude" ] = match_group [ 2 ]
185            entry [ "height" ] = match_group [ 3 ]
186            entry [ "latitude" ] = match_group [ 4 ]
187            entry [ "rotation_height" ] = match_group [ 5 ]
188            entry [ "rotation_longitude" ] = match_group [ 6 ]
189            entry [ "rotation_latitude" ] = match_group [ 7 ]
190            entry [ "remote" ] = match_group [ 8 ]
191            entry [ "health" ] = match_group [ 9 ]
192            entry [ "deaths" ] = match_group [ 10 ]
193            entry [ "zombies" ] = match_group [ 11 ]
194            entry [ "players" ] = match_group [ 12 ]
195            entry [ "score" ] = match_group [ 13 ]
196            entry [ "level" ] = match_group [ 14 ]
197            entry["steamid"] = match_group[15]
198            entry [ "ip" ] = match_group [ 16 ]
199            entry [ "ping" ] = match_group [ 17 ]
200            self.logger.debug("Entry after: {}".format(entry))
201            self.controller.database.blocking_update(
202                lp_table,
203                entry)
204            self.logger.debug("Updated entry." )
205        self.logger.debug("returning." )
206
207    def update_online_players_count(self, match_group):
208        self.logger.debug(match_group)
209        count = int ( match_group [ 0 ] )
210        self.online_players_count = count
211        if count == 0:
212            self.controller.database.blocking_delete(lp_table, [])
213            self.server_empty = True
214            return
215        else:
216            self.server_empty = False
217            
218        answer = self.controller.database.blocking_consult(lp_table, [])
219        if len(answer) != self.online_players_count:
220            self.logger.debug(
221                "Number of online players does not match number of DB entries.")
222            self.online_players_changed = True
223
224    def get_online_players(self):
225        answer = self.controller.database.blocking_consult(
226            lp_table,
227            [])
228        if len(answer) != self.online_players_count:
229            self.logger.warning(
230                "Number of online players {} does not match number of DB " \
231                "entries {}.".format(self.online_players_count, len(answer)))
232        self.online_players = answer
233        self.logger.debug("{} players online now.".format(len(self.online_players)))
234        return self.online_players
235
236    def log_time_of_day(self, match_group):
237        self.day = int(match_group[0])
238        self.hour = int(match_group[1])
239        self.minute = int(match_group[2])
240        if self.day != self.latest_day:
241            self.latest_day = self.day
242            self.controller.dispatcher.call_registered_callbacks(
243                "new day", [self.day, self.hour, self.minute])
244        if self.hour != self.latest_hour:
245            self.latest_hour = self.hour
246            self.logger.info("Day {} {:02}:{:02}".format(
247                self.day, self.hour, self.minute))
248            self.controller.dispatcher.call_registered_callbacks(
249                "new hour", [self.day, self.hour, self.minute])
250
251    def parse_lkp_output(self, match_groups):
252        name = match_groups[0]
253        player_id = match_groups[1]
254        steamid = match_groups[2]
255        ip = match_groups[4]
256        db = self.controller.database.blocking_consult(
257            lkp_table, [(lkp_table.steamid, "==", steamid)])
258        if len(db) != 0:
259            self.logger.debug("lkp output for {} ignored: already on db.".format(
260                name))
261            return
262        self.controller.database.blocking_add(
263            lkp_table, [lkp_table(name = name,
264                                  player_id = player_id,
265                                  steamid = steamid,
266                                  ip = ip)])
267        self.logger.info("Added lkp_table entry for {}.".format(name))
268
269    def update_zombies_alive(self, match_groups):
270        self.zombies_alive = int(match_groups[7])
271        
272    # API
273        
274    def get_player_string(self, name_or_alias):
275        self.logger.debug("Trying to get player for name or alias '{}'.".format(
276            name_or_alias))
277        db = self.controller.database.blocking_consult(
278            lkp_table, [(lkp_table.name, "==", name_or_alias)])
279        if len(db) == 1:
280            self.logger.debug("Player {} found.".format(db[0]["name"]))
281            return db[0]
282        if len(db) > 1:
283            self.logger.error("Multiple players with name {} on db.".format(
284                name_or_alias))
285            return None
286        self.logger.info("No player with name {} on db.".format(name_or_alias))
287        db = self.controller.database.blocking_consult(
288            AliasTable, [(AliasTable.alias, "==", name_or_alias)])
289        if len(db) > 1:
290            self.logger.error("Multiple players with alias {} on db.".format(
291                name_or_alias))
292            return None
293        if len(db) == 0:
294            self.logger.warning("No player with alias {} on db.".format(
295                name_or_alias))
296            return None
297        player = self.controller.database.blocking_consult(
298            lkp_table, [(lkp_table.steamid, "==", db[0]["steamid"])])[0]
299        self.logger.info("Found player {} with alias {}.".format(
300            player["name"], name_or_alias))
301        return player
302
303    def get_player_steamid(self, steamid):
304        self.logger.debug("Trying to find db entry for steamid {}.".format(
305            steamid))
306        db = self.controller.database.blocking_consult(
307            lkp_table, [(lkp_table.steamid, "==", steamid)])
308        if len(db) == 1:
309            self.logger.debug("DB entry for steamid {} found: {}.".format(
310                steamid, db[0]["name"]))
311            return db[0]
312        else:
313            self.logger.warning(
314                "Couldn't find single db entry for steamid {}.".format(
315                    steamid))
316            return None
317
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)