How to use contexts method in Playwright Python

Best Python code snippet using playwright-python

samplers.py

Source:samplers.py Github

copy

Full Screen

...40 def __call__(self, batch_size, **kwargs):41 raise NotImplementedError42 def set_replay(self, replay=None):43 pass44 def _validate_contexts(self, contexts):45 """Validate if contexts have right spec.46 Args:47 contexts: A [batch_size, num_contexts_dim] tensor.48 Raises:49 ValueError: If shape or dtype mismatches that of spec.50 """51 if contexts[0].shape != self._context_spec.shape:52 raise ValueError('contexts has invalid shape %s wrt spec shape %s' %53 (contexts[0].shape, self._context_spec.shape))54 if contexts.dtype != self._context_spec.dtype:55 raise ValueError('contexts has invalid dtype %s wrt spec dtype %s' %56 (contexts.dtype, self._context_spec.dtype))57@gin.configurable58class ZeroSampler(BaseSampler):59 """Zero sampler."""60 def __call__(self, batch_size, **kwargs):61 """Sample a batch of context.62 Args:63 batch_size: Batch size.64 Returns:65 Two [batch_size, num_context_dims] tensors.66 """67 contexts = tf.zeros(68 dtype=self._context_spec.dtype,69 shape=[70 batch_size,71 ] + self._context_spec.shape.as_list())72 return contexts, contexts73@gin.configurable74class BinarySampler(BaseSampler):75 """Binary sampler."""76 def __init__(self, probs=0.5, *args, **kwargs):77 """Constructor."""78 super(BinarySampler, self).__init__(*args, **kwargs)79 self._probs = probs80 def __call__(self, batch_size, **kwargs):81 """Sample a batch of context."""82 spec = self._context_spec83 contexts = tf.random_uniform(84 shape=[85 batch_size,86 ] + spec.shape.as_list(), dtype=tf.float32)87 contexts = tf.cast(tf.greater(contexts, self._probs), dtype=spec.dtype)88 return contexts, contexts89@gin.configurable90class RandomSampler(BaseSampler):91 """Random sampler."""92 def __call__(self, batch_size, **kwargs):93 """Sample a batch of context.94 Args:95 batch_size: Batch size.96 Returns:97 Two [batch_size, num_context_dims] tensors.98 """99 spec = self._context_spec100 context_range = self._context_range101 if isinstance(context_range[0], (int, float)):102 contexts = tf.random_uniform(103 shape=[104 batch_size,105 ] + spec.shape.as_list(),106 minval=context_range[0],107 maxval=context_range[1],108 dtype=spec.dtype)109 elif isinstance(context_range[0], (list, tuple, np.ndarray)):110 assert len(spec.shape.as_list()) == 1111 assert spec.shape.as_list()[0] == len(context_range[0])112 assert spec.shape.as_list()[0] == len(context_range[1])113 contexts = tf.concat(114 [115 tf.random_uniform(116 shape=[117 batch_size, 1,118 ] + spec.shape.as_list()[1:],119 minval=context_range[0][i],120 maxval=context_range[1][i],121 dtype=spec.dtype) for i in range(spec.shape.as_list()[0])122 ],123 axis=1)124 else: raise NotImplementedError(context_range)125 self._validate_contexts(contexts)126 state, next_state = kwargs['state'], kwargs['next_state']127 if state is not None and next_state is not None:128 pass129 #contexts = tf.concat(130 # [tf.random_normal(tf.shape(state[:, :self._k]), dtype=tf.float64) +131 # tf.random_shuffle(state[:, :self._k]),132 # contexts[:, self._k:]], 1)133 return contexts, contexts134@gin.configurable135class ScheduledSampler(BaseSampler):136 """Scheduled sampler."""137 def __init__(self,138 scope='default',139 values=None,140 scheduler='cycle',141 scheduler_params=None,142 *args, **kwargs):143 """Construct sampler.144 Args:145 scope: Scope name.146 values: A list of numbers or [num_context_dim] Numpy arrays147 representing the values to cycle.148 scheduler: scheduler type.149 scheduler_params: scheduler parameters.150 *args: arguments.151 **kwargs: keyword arguments.152 """153 super(ScheduledSampler, self).__init__(*args, **kwargs)154 self._scope = scope155 self._values = values156 self._scheduler = scheduler157 self._scheduler_params = scheduler_params or {}158 assert self._values is not None and len(159 self._values), 'must provide non-empty values.'160 self._n = len(self._values)161 # TODO(shanegu): move variable creation outside. resolve tf.cond problem.162 self._count = 0163 self._i = tf.Variable(164 tf.zeros(shape=(), dtype=tf.int32),165 name='%s-scheduled_sampler_%d' % (self._scope, self._count))166 self._values = tf.constant(self._values, dtype=self._context_spec.dtype)167 def __call__(self, batch_size, **kwargs):168 """Sample a batch of context.169 Args:170 batch_size: Batch size.171 Returns:172 Two [batch_size, num_context_dims] tensors.173 """174 spec = self._context_spec175 next_op = self._next(self._i)176 with tf.control_dependencies([next_op]):177 value = self._values[self._i]178 if value.get_shape().as_list():179 values = tf.tile(180 tf.expand_dims(value, 0), (batch_size,) + (1,) * spec.shape.ndims)181 else:182 values = value + tf.zeros(183 shape=[184 batch_size,185 ] + spec.shape.as_list(), dtype=spec.dtype)186 self._validate_contexts(values)187 self._count += 1188 return values, values189 def _next(self, i):190 """Return op that increments pointer to next value.191 Args:192 i: A tensorflow integer variable.193 Returns:194 Op that increments pointer.195 """196 if self._scheduler == 'cycle':197 inc = ('inc' in self._scheduler_params and198 self._scheduler_params['inc']) or 1199 return tf.assign(i, tf.mod(i+inc, self._n))200 else:201 raise NotImplementedError(self._scheduler)202@gin.configurable203class ReplaySampler(BaseSampler):204 """Replay sampler."""205 def __init__(self,206 prefetch_queue_capacity=2,207 override_indices=None,208 state_indices=None,209 *args,210 **kwargs):211 """Construct sampler.212 Args:213 prefetch_queue_capacity: Capacity for prefetch queue.214 override_indices: Override indices.215 state_indices: Select certain indices from state dimension.216 *args: arguments.217 **kwargs: keyword arguments.218 """219 super(ReplaySampler, self).__init__(*args, **kwargs)220 self._prefetch_queue_capacity = prefetch_queue_capacity221 self._override_indices = override_indices222 self._state_indices = state_indices223 def set_replay(self, replay):224 """Set replay.225 Args:226 replay: A replay buffer.227 """228 self._replay = replay229 def __call__(self, batch_size, **kwargs):230 """Sample a batch of context.231 Args:232 batch_size: Batch size.233 Returns:234 Two [batch_size, num_context_dims] tensors.235 """236 batch = self._replay.GetRandomBatch(batch_size)237 next_states = batch[4]238 if self._prefetch_queue_capacity > 0:239 batch_queue = slim.prefetch_queue.prefetch_queue(240 [next_states],241 capacity=self._prefetch_queue_capacity,242 name='%s/batch_context_queue' % self._scope)243 next_states = batch_queue.dequeue()244 if self._override_indices is not None:245 assert self._context_range is not None and isinstance(246 self._context_range[0], (int, long, float))247 next_states = tf.concat(248 [249 tf.random_uniform(250 shape=next_states[:, :1].shape,251 minval=self._context_range[0],252 maxval=self._context_range[1],253 dtype=next_states.dtype)254 if i in self._override_indices else next_states[:, i:i + 1]255 for i in range(self._context_spec.shape.as_list()[0])256 ],257 axis=1)258 if self._state_indices is not None:259 next_states = tf.concat(260 [261 next_states[:, i:i + 1]262 for i in range(self._context_spec.shape.as_list()[0])263 ],264 axis=1)265 self._validate_contexts(next_states)266 return next_states, next_states267@gin.configurable268class TimeSampler(BaseSampler):269 """Time Sampler."""270 def __init__(self, minval=0, maxval=1, timestep=-1, *args, **kwargs):271 """Construct sampler.272 Args:273 minval: Min value integer.274 maxval: Max value integer.275 timestep: Time step between states and next_states.276 *args: arguments.277 **kwargs: keyword arguments.278 """279 super(TimeSampler, self).__init__(*args, **kwargs)280 assert self._context_spec.shape.as_list() == [1]281 self._minval = minval282 self._maxval = maxval283 self._timestep = timestep284 def __call__(self, batch_size, **kwargs):285 """Sample a batch of context.286 Args:287 batch_size: Batch size.288 Returns:289 Two [batch_size, num_context_dims] tensors.290 """291 if self._maxval == self._minval:292 contexts = tf.constant(293 self._maxval, shape=[batch_size, 1], dtype=tf.int32)294 else:295 contexts = tf.random_uniform(296 shape=[batch_size, 1],297 dtype=tf.int32,298 maxval=self._maxval,299 minval=self._minval)300 next_contexts = tf.maximum(contexts + self._timestep, 0)301 return tf.cast(302 contexts, dtype=self._context_spec.dtype), tf.cast(303 next_contexts, dtype=self._context_spec.dtype)304@gin.configurable305class ConstantSampler(BaseSampler):306 """Constant sampler."""307 def __init__(self, value=None, *args, **kwargs):308 """Construct sampler.309 Args:310 value: A list or Numpy array for values of the constant.311 *args: arguments.312 **kwargs: keyword arguments.313 """314 super(ConstantSampler, self).__init__(*args, **kwargs)315 self._value = value316 def __call__(self, batch_size, **kwargs):317 """Sample a batch of context.318 Args:319 batch_size: Batch size.320 Returns:321 Two [batch_size, num_context_dims] tensors.322 """323 spec = self._context_spec324 value_ = tf.constant(self._value, shape=spec.shape, dtype=spec.dtype)325 values = tf.tile(326 tf.expand_dims(value_, 0), (batch_size,) + (1,) * spec.shape.ndims)327 self._validate_contexts(values)328 return values, values329@gin.configurable330class DirectionSampler(RandomSampler):331 """Direction sampler."""332 def __call__(self, batch_size, **kwargs):333 """Sample a batch of context.334 Args:335 batch_size: Batch size.336 Returns:337 Two [batch_size, num_context_dims] tensors.338 """339 spec = self._context_spec340 context_range = self._context_range341 if isinstance(context_range[0], (int, float)):342 contexts = tf.random_uniform(343 shape=[344 batch_size,345 ] + spec.shape.as_list(),346 minval=context_range[0],347 maxval=context_range[1],348 dtype=spec.dtype)349 elif isinstance(context_range[0], (list, tuple, np.ndarray)):350 assert len(spec.shape.as_list()) == 1351 assert spec.shape.as_list()[0] == len(context_range[0])352 assert spec.shape.as_list()[0] == len(context_range[1])353 contexts = tf.concat(354 [355 tf.random_uniform(356 shape=[357 batch_size, 1,358 ] + spec.shape.as_list()[1:],359 minval=context_range[0][i],360 maxval=context_range[1][i],361 dtype=spec.dtype) for i in range(spec.shape.as_list()[0])362 ],363 axis=1)364 else: raise NotImplementedError(context_range)365 self._validate_contexts(contexts)366 if 'sampler_fn' in kwargs:367 other_contexts = kwargs['sampler_fn']()368 else:369 other_contexts = contexts370 state, next_state = kwargs['state'], kwargs['next_state']371 if state is not None and next_state is not None:372 my_context_range = (np.array(context_range[1]) - np.array(context_range[0])) / 2 * np.ones(spec.shape.as_list())373 contexts = tf.concat(374 [0.1 * my_context_range[:self._k] *375 tf.random_normal(tf.shape(state[:, :self._k]), dtype=state.dtype) +376 tf.random_shuffle(state[:, :self._k]) - state[:, :self._k],377 other_contexts[:, self._k:]], 1)378 #contexts = tf.Print(contexts,379 # [contexts, tf.reduce_max(contexts, 0),...

Full Screen

Full Screen

test_html.py

Source:test_html.py Github

copy

Full Screen

1"""2test_html_contexts.py3Copyright 2015 Andres Riancho4This file is part of w3af, http://w3af.org/ .5w3af is free software; you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation version 2 of the License.8w3af is distributed in the hope that it will be useful,9but WITHOUT ANY WARRANTY; without even the implied warranty of10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11GNU General Public License for more details.12You should have received a copy of the GNU General Public License13along with w3af; if not, write to the Free Software14Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA15"""16import os17from w3af.core.data.context.tests.context_test import ContextTest18from w3af.core.data.context.context import get_context19from w3af.core.data.context.context.html import (HtmlTag,20 CSSText,21 HtmlAttr,22 HtmlText,23 ScriptText,24 HtmlComment,25 HtmlTagClose,26 HtmlAttrNoQuote,27 HtmlAttrBackticks,28 HtmlAttrSingleQuote,29 HtmlAttrDoubleQuote)30class TestHTMLContext(ContextTest):31 SAMPLES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)),32 'samples')33 def test_payload_only_payload(self):34 html = 'PAYLOAD'35 contexts = get_context(html, 'PAYLOAD')36 self.assertEqual(len(contexts), 1)37 self.assertIsInstance(contexts[0], HtmlText)38 def test_payload_empty(self):39 html = ''40 self.assertEqual(get_context(html, 'PAYLOAD'), [])41 def test_payload_in_html_text(self):42 html = """43 <html>44 <body>45 PAYLOAD46 </body>47 </html>48 """49 contexts = get_context(html, 'PAYLOAD')50 self.assertEqual(len(contexts), 1)51 self.assertIsInstance(contexts[0], HtmlText)52 def test_payload_in_html_text_with_lower(self):53 html = """54 <html>55 <body>56 %s57 </body>58 </html>59 """60 payload = 'PAYLOAD'61 contexts = get_context(html % payload.lower(), payload)62 self.assertEqual(len(contexts), 1)63 self.assertIsInstance(contexts[0], HtmlText)64 def test_payload_html_inside_comment(self):65 html = """66 <html>67 <!-- <body>PAYLOAD</body> -->68 </html>69 """70 contexts = get_context(html, 'PAYLOAD')71 self.assertEqual(len(contexts), 1)72 self.assertIsInstance(contexts[0], HtmlComment)73 def test_tag_attr_double_quote(self):74 html = """75 <html>76 <tag attr="PAYLOAD" />77 </html>78 """79 contexts = get_context(html, 'PAYLOAD')80 self.assertEqual(len(contexts), 1)81 self.assertIsInstance(contexts[0], HtmlAttrDoubleQuote)82 def test_tag_attr_single_double_quote(self):83 html = """84 <html>85 <tag spam='eggs' attr="PAYLOAD" />86 </html>87 """88 contexts = get_context(html, 'PAYLOAD')89 self.assertEqual(len(contexts), 1)90 self.assertIsInstance(contexts[0], HtmlAttrDoubleQuote)91 def test_payload_a_single_quote(self):92 html = """93 <html>94 <a foo='PAYLOAD'>95 bar96 </a>97 </html>98 """99 contexts = get_context(html, 'PAYLOAD')100 self.assertEqual(len(contexts), 1)101 self.assertIsInstance(contexts[0], HtmlAttrSingleQuote)102 def test_payload_a_single_quote_with_escape(self):103 html = """104 <html>105 <a foo='PAYLOAD&#39;'>106 bar107 </a>108 </html>109 """110 contexts = get_context(html, 'PAYLOAD')111 self.assertEqual(len(contexts), 1)112 self.assertIsInstance(contexts[0], HtmlAttrSingleQuote)113 def test_payload_a_double_quote_with_escape(self):114 html = """115 <html>116 <a foo="PAYLOAD&quot;">117 bar118 </a>119 </html>120 """121 contexts = get_context(html, 'PAYLOAD')122 self.assertEqual(len(contexts), 1)123 self.assertIsInstance(contexts[0], HtmlAttrDoubleQuote)124 def test_payload_backtick(self):125 html = """126 <html>127 <a foo=`PAYLOAD`>128 bar129 </a>130 </html>131 """132 contexts = get_context(html, 'PAYLOAD')133 self.assertEqual(len(contexts), 1)134 self.assertIsInstance(contexts[0], HtmlAttrBackticks)135 def test_payload_attr_value_no_separator(self):136 html = """137 <html>138 <a foo=PAYLOAD bar=loops>139 bar140 </a>141 </html>142 """143 contexts = get_context(html, 'PAYLOAD')144 self.assertEqual(len(contexts), 1)145 self.assertIsInstance(contexts[0], HtmlAttrNoQuote)146 def test_payload_in_html_text_complex(self):147 html = """148 <html>149 <tag>foo</tag>150 PAYLOAD151 <tag>bar</tag>152 </html>153 """154 contexts = get_context(html, 'PAYLOAD')155 self.assertEqual(len(contexts), 1)156 self.assertIsInstance(contexts[0], HtmlText)157 def test_payload_broken_double_open(self):158 html = """159 <html>160 <tag>foo161 PAYLOAD162 <tag>bar</tag>163 </html>164 """165 contexts = get_context(html, 'PAYLOAD')166 self.assertEqual(len(contexts), 1)167 self.assertIsInstance(contexts[0], HtmlText)168 def test_payload_script_broken_double_close(self):169 html = """170 <html>171 <script>foo</script>172 PAYLOAD173 </script>174 </html>175 """176 contexts = get_context(html, 'PAYLOAD')177 self.assertEqual(len(contexts), 1)178 self.assertIsInstance(contexts[0], ScriptText)179 def test_payload_confuse_parser(self):180 html = """181 <html>182 <a attr="</a>">PAYLOAD</a>183 </html>184 """185 contexts = get_context(html, 'PAYLOAD')186 self.assertEqual(len(contexts), 1)187 self.assertIsInstance(contexts[0], HtmlText)188 def test_payload_text_with_quotes(self):189 html = """190 <html>191 <a>Quoting the great Linus Torvalds: "PAYLOAD<"</a>192 </html>193 """194 contexts = get_context(html, 'PAYLOAD')195 self.assertEqual(len(contexts), 1)196 context = contexts[0]197 self.assertIsInstance(context, HtmlText)198 self.assertFalse(context.can_break())199 def test_payload_text_with_start_quote(self):200 html = """201 <html>202 <a>Quoting the great Linus Torvalds: "PAYLOAD<</a>203 </html>204 """205 contexts = get_context(html, '"PAYLOAD<')206 self.assertEqual(len(contexts), 1)207 context = contexts[0]208 self.assertIsInstance(context, HtmlText)209 def test_payload_text_with_end_quote(self):210 html = """211 <html>212 <a>Quoting the great Linus Torvalds: PAYLOAD<"</a>213 </html>214 """215 contexts = get_context(html, 'PAYLOAD<"')216 self.assertEqual(len(contexts), 1)217 context = contexts[0]218 self.assertIsInstance(context, HtmlText)219 def test_payload_tag_name(self):220 html = """221 <PAYLOAD></x>222 </foo>223 """224 contexts = get_context(html, 'PAYLOAD')225 self.assertEqual(len(contexts), 1, contexts)226 self.assertIsInstance(contexts[0], HtmlTag)227 def test_payload_tag_name_close(self):228 html = """229 <foo>230 </PAYLOAD>231 """232 contexts = get_context(html, 'PAYLOAD')233 self.assertEqual(len(contexts), 1, contexts)234 self.assertIsInstance(contexts[0], HtmlTagClose)235 def test_payload_tag_attr_key(self):236 html = """237 <a PAYLOAD="/xyz">foo</a>238 """239 contexts = get_context(html, 'PAYLOAD')240 self.assertEqual(len(contexts), 1, contexts)241 context = contexts[0]242 self.assertIsInstance(context, HtmlAttr)243 def test_django_500_sample(self):244 html = file(os.path.join(self.SAMPLES_DIR, 'django-500.html')).read()245 contexts = get_context(html, 'QUBD5 =')246 self.assertEqual(len(contexts), 9)247 for context in contexts:248 self.assertIsInstance(context, HtmlText)249 def test_payload_html_comment_with_single_quote(self):250 """251 A single quote inside an HTML comment seems to break parsing by252 "extending" the HTML comment context. See the "quote_comment.html"253 file, specifically the section which says:254 "I'm a single quote, and I break stuff."255 Before fixing this bug, if you removed that single quote, this test256 passed.257 """258 html = """259 <!DOCTYPE html>260 <html>261 <!--262 I'm a single quote, and I break stuff.263 -->264 <a href="http://external/abc/PAYLOAD">Check link href</a>265 </html>266 """267 contexts = get_context(html, 'PAYLOAD')268 self.assertEqual(len(contexts), 1, contexts)269 self.assertIsInstance(contexts[0], HtmlAttrDoubleQuote)270 def test_payload_html_comment_with_tag_attr_inside(self):271 html = """272 <!DOCTYPE html>273 <html>274 <!--275 <a href="PAYLOAD"></a>276 -->277 <a href="http://external/abc/PAYLOAD">Check link href</a>278 </html>279 """280 contexts = get_context(html, 'PAYLOAD')281 self.assertEqual(len(contexts), 2, contexts)282 self.assertIsInstance(contexts[0], HtmlComment)283 self.assertIsInstance(contexts[1], HtmlAttrDoubleQuote)284 def test_payload_html_comment_with_tag_text_inside(self):285 html = """286 <!DOCTYPE html>287 <html>288 <!--289 <a href="">PAYLOAD</a>290 -->291 <a href="http://external/abc/PAYLOAD">Check link href</a>292 </html>293 """294 contexts = get_context(html, 'PAYLOAD')295 self.assertEqual(len(contexts), 2, contexts)296 self.assertIsInstance(contexts[0], HtmlComment)297 self.assertIsInstance(contexts[1], HtmlAttrDoubleQuote)298 def test_broken_1(self):299 html = """300 <a PAYLOAD="/xyz301 """302 contexts = get_context(html, 'PAYLOAD')303 self.assertEqual(len(contexts), 0, contexts)304 def test_broken_2(self):305 html = """306 <a PAYLOAD="/xyz" /<307 """308 contexts = get_context(html, 'PAYLOAD')309 self.assertEqual(len(contexts), 0, contexts)310 def test_broken_3(self):311 html = """312 <a PAYLOAD="/xyz"><313 """314 contexts = get_context(html, 'PAYLOAD')315 self.assertEqual(len(contexts), 1, contexts)316 self.assertIsInstance(contexts[0], HtmlAttr)317 def test_broken_4(self):318 html = """319 <a PAYLOAD="/xyz"></320 """321 contexts = get_context(html, 'PAYLOAD')322 self.assertEqual(len(contexts), 1, contexts)323 self.assertIsInstance(contexts[0], HtmlAttr)324 def test_broken_5(self):325 html = """326 <a foo="/xyz"></PAYLOAD327 """328 contexts = get_context(html, 'PAYLOAD')329 self.assertEqual(len(contexts), 0, contexts)330 def test_script_text(self):331 html = """332 <script>foo(); bar(PAYLOAD);</script>333 """334 contexts = get_context(html, 'PAYLOAD')335 self.assertEqual(len(contexts), 1, contexts)336 self.assertIsInstance(contexts[0], ScriptText)337 def test_style_text(self):338 html = """339 <style>foo(); bar(PAYLOAD);</style>340 """341 contexts = get_context(html, 'PAYLOAD')342 self.assertEqual(len(contexts), 1, contexts)343 self.assertIsInstance(contexts[0], CSSText)344 def test_script_text_comment(self):345 html = """346 <script type="text/javascript">347 <!--348 foo(); bar(PAYLOAD);349 //-->350 </script>351 """352 contexts = get_context(html, 'PAYLOAD')353 self.assertEqual(len(contexts), 1, contexts)354 self.assertIsInstance(contexts[0], ScriptText)355 def test_payload_inside_noscript_1(self):356 html = """357 <html>358 <body>359 <noscript>360 PAYLOAD361 </noscript>362 </body>363 </html>364 """365 contexts = get_context(html, 'PAYLOAD')366 self.assertEqual(len(contexts), 0)367 def test_payload_inside_noscript_2(self):368 html = """369 <html>370 <noscript>371 <a onmouseover="PAYLOAD">link</a>372 </noscript>373 </html>374 """375 contexts = get_context(html, 'PAYLOAD')...

Full Screen

Full Screen

stack_context.py

Source:stack_context.py Github

copy

Full Screen

1#!/usr/bin/env python2#3# Copyright 2010 Facebook4#5# Licensed under the Apache License, Version 2.0 (the "License"); you may6# not use this file except in compliance with the License. You may obtain7# a copy of the License at8#9# http://www.apache.org/licenses/LICENSE-2.010#11# Unless required by applicable law or agreed to in writing, software12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the14# License for the specific language governing permissions and limitations15# under the License.16"""`StackContext` allows applications to maintain threadlocal-like state17that follows execution as it moves to other execution contexts.18The motivating examples are to eliminate the need for explicit19``async_callback`` wrappers (as in `tornado.web.RequestHandler`), and to20allow some additional context to be kept for logging.21This is slightly magic, but it's an extension of the idea that an22exception handler is a kind of stack-local state and when that stack23is suspended and resumed in a new context that state needs to be24preserved. `StackContext` shifts the burden of restoring that state25from each call site (e.g. wrapping each `.AsyncHTTPClient` callback26in ``async_callback``) to the mechanisms that transfer control from27one context to another (e.g. `.AsyncHTTPClient` itself, `.IOLoop`,28thread pools, etc).29Example usage::30 @contextlib.contextmanager31 def die_on_error():32 try:33 yield34 except Exception:35 logging.error("exception in asynchronous operation",exc_info=True)36 sys.exit(1)37 with StackContext(die_on_error):38 # Any exception thrown here *or in callback and its descendants*39 # will cause the process to exit instead of spinning endlessly40 # in the ioloop.41 http_client.fetch(url, callback)42 ioloop.start()43Most applications shouldn't have to work with `StackContext` directly.44Here are a few rules of thumb for when it's necessary:45* If you're writing an asynchronous library that doesn't rely on a46 stack_context-aware library like `tornado.ioloop` or `tornado.iostream`47 (for example, if you're writing a thread pool), use48 `.stack_context.wrap()` before any asynchronous operations to capture the49 stack context from where the operation was started.50* If you're writing an asynchronous library that has some shared51 resources (such as a connection pool), create those shared resources52 within a ``with stack_context.NullContext():`` block. This will prevent53 ``StackContexts`` from leaking from one request to another.54* If you want to write something like an exception handler that will55 persist across asynchronous calls, create a new `StackContext` (or56 `ExceptionStackContext`), and make your asynchronous calls in a ``with``57 block that references your `StackContext`.58"""59from __future__ import absolute_import, division, print_function, with_statement60import sys61import threading62from .util import raise_exc_info63class StackContextInconsistentError(Exception):64 pass65class _State(threading.local):66 def __init__(self):67 self.contexts = (tuple(), None)68_state = _State()69class StackContext(object):70 """Establishes the given context as a StackContext that will be transferred.71 Note that the parameter is a callable that returns a context72 manager, not the context itself. That is, where for a73 non-transferable context manager you would say::74 with my_context():75 StackContext takes the function itself rather than its result::76 with StackContext(my_context):77 The result of ``with StackContext() as cb:`` is a deactivation78 callback. Run this callback when the StackContext is no longer79 needed to ensure that it is not propagated any further (note that80 deactivating a context does not affect any instances of that81 context that are currently pending). This is an advanced feature82 and not necessary in most applications.83 """84 def __init__(self, context_factory):85 self.context_factory = context_factory86 self.contexts = []87 self.active = True88 def _deactivate(self):89 self.active = False90 # StackContext protocol91 def enter(self):92 context = self.context_factory()93 self.contexts.append(context)94 context.__enter__()95 def exit(self, type, value, traceback):96 context = self.contexts.pop()97 context.__exit__(type, value, traceback)98 # Note that some of this code is duplicated in ExceptionStackContext99 # below. ExceptionStackContext is more common and doesn't need100 # the full generality of this class.101 def __enter__(self):102 self.old_contexts = _state.contexts103 self.new_contexts = (self.old_contexts[0] + (self,), self)104 _state.contexts = self.new_contexts105 try:106 self.enter()107 except:108 _state.contexts = self.old_contexts109 raise110 return self._deactivate111 def __exit__(self, type, value, traceback):112 try:113 self.exit(type, value, traceback)114 finally:115 final_contexts = _state.contexts116 _state.contexts = self.old_contexts117 # Generator coroutines and with-statements with non-local118 # effects interact badly. Check here for signs of119 # the stack getting out of sync.120 # Note that this check comes after restoring _state.context121 # so that if it fails things are left in a (relatively)122 # consistent state.123 if final_contexts is not self.new_contexts:124 raise StackContextInconsistentError(125 'stack_context inconsistency (may be caused by yield '126 'within a "with StackContext" block)')127 # Break up a reference to itself to allow for faster GC on CPython.128 self.new_contexts = None129class ExceptionStackContext(object):130 """Specialization of StackContext for exception handling.131 The supplied ``exception_handler`` function will be called in the132 event of an uncaught exception in this context. The semantics are133 similar to a try/finally clause, and intended use cases are to log134 an error, close a socket, or similar cleanup actions. The135 ``exc_info`` triple ``(type, value, traceback)`` will be passed to the136 exception_handler function.137 If the exception handler returns true, the exception will be138 consumed and will not be propagated to other exception handlers.139 """140 def __init__(self, exception_handler):141 self.exception_handler = exception_handler142 self.active = True143 def _deactivate(self):144 self.active = False145 def exit(self, type, value, traceback):146 if type is not None:147 return self.exception_handler(type, value, traceback)148 def __enter__(self):149 self.old_contexts = _state.contexts150 self.new_contexts = (self.old_contexts[0], self)151 _state.contexts = self.new_contexts152 return self._deactivate153 def __exit__(self, type, value, traceback):154 try:155 if type is not None:156 return self.exception_handler(type, value, traceback)157 finally:158 final_contexts = _state.contexts159 _state.contexts = self.old_contexts160 if final_contexts is not self.new_contexts:161 raise StackContextInconsistentError(162 'stack_context inconsistency (may be caused by yield '163 'within a "with StackContext" block)')164 # Break up a reference to itself to allow for faster GC on CPython.165 self.new_contexts = None166class NullContext(object):167 """Resets the `StackContext`.168 Useful when creating a shared resource on demand (e.g. an169 `.AsyncHTTPClient`) where the stack that caused the creating is170 not relevant to future operations.171 """172 def __enter__(self):173 self.old_contexts = _state.contexts174 _state.contexts = (tuple(), None)175 def __exit__(self, type, value, traceback):176 _state.contexts = self.old_contexts177def _remove_deactivated(contexts):178 """Remove deactivated handlers from the chain"""179 # Clean ctx handlers180 stack_contexts = tuple([h for h in contexts[0] if h.active])181 # Find new head182 head = contexts[1]183 while head is not None and not head.active:184 head = head.old_contexts[1]185 # Process chain186 ctx = head187 while ctx is not None:188 parent = ctx.old_contexts[1]189 while parent is not None:190 if parent.active:191 break192 ctx.old_contexts = parent.old_contexts193 parent = parent.old_contexts[1]194 ctx = parent195 return (stack_contexts, head)196def wrap(fn):197 """Returns a callable object that will restore the current `StackContext`198 when executed.199 Use this whenever saving a callback to be executed later in a200 different execution context (either in a different thread or201 asynchronously in the same thread).202 """203 # Check if function is already wrapped204 if fn is None or hasattr(fn, '_wrapped'):205 return fn206 # Capture current stack head207 # TODO: Any other better way to store contexts and update them in wrapped function?208 cap_contexts = [_state.contexts]209 if not cap_contexts[0][0] and not cap_contexts[0][1]:210 # Fast path when there are no active contexts.211 def null_wrapper(*args, **kwargs):212 try:213 current_state = _state.contexts214 _state.contexts = cap_contexts[0]215 return fn(*args, **kwargs)216 finally:217 _state.contexts = current_state218 null_wrapper._wrapped = True219 return null_wrapper220 def wrapped(*args, **kwargs):221 ret = None222 try:223 # Capture old state224 current_state = _state.contexts225 # Remove deactivated items226 cap_contexts[0] = contexts = _remove_deactivated(cap_contexts[0])227 # Force new state228 _state.contexts = contexts229 # Current exception230 exc = (None, None, None)231 top = None232 # Apply stack contexts233 last_ctx = 0234 stack = contexts[0]235 # Apply state236 for n in stack:237 try:238 n.enter()239 last_ctx += 1240 except:241 # Exception happened. Record exception info and store top-most handler242 exc = sys.exc_info()243 top = n.old_contexts[1]244 # Execute callback if no exception happened while restoring state245 if top is None:246 try:247 ret = fn(*args, **kwargs)248 except:249 exc = sys.exc_info()250 top = contexts[1]251 # If there was exception, try to handle it by going through the exception chain252 if top is not None:253 exc = _handle_exception(top, exc)254 else:255 # Otherwise take shorter path and run stack contexts in reverse order256 while last_ctx > 0:257 last_ctx -= 1258 c = stack[last_ctx]259 try:260 c.exit(*exc)261 except:262 exc = sys.exc_info()263 top = c.old_contexts[1]264 break265 else:266 top = None267 # If if exception happened while unrolling, take longer exception handler path268 if top is not None:269 exc = _handle_exception(top, exc)270 # If exception was not handled, raise it271 if exc != (None, None, None):272 raise_exc_info(exc)273 finally:274 _state.contexts = current_state275 return ret276 wrapped._wrapped = True277 return wrapped278def _handle_exception(tail, exc):279 while tail is not None:280 try:281 if tail.exit(*exc):282 exc = (None, None, None)283 except:284 exc = sys.exc_info()285 tail = tail.old_contexts[1]286 return exc287def run_with_stack_context(context, func):288 """Run a coroutine ``func`` in the given `StackContext`.289 It is not safe to have a ``yield`` statement within a ``with StackContext``290 block, so it is difficult to use stack context with `.gen.coroutine`.291 This helper function runs the function in the correct context while292 keeping the ``yield`` and ``with`` statements syntactically separate.293 Example::294 @gen.coroutine295 def incorrect():296 with StackContext(ctx):297 # ERROR: this will raise StackContextInconsistentError298 yield other_coroutine()299 @gen.coroutine300 def correct():301 yield run_with_stack_context(StackContext(ctx), other_coroutine)302 .. versionadded:: 3.1303 """304 with context:...

Full Screen

Full Screen

bench_presentation.py

Source:bench_presentation.py Github

copy

Full Screen

1"""Performance tests for the presentation module."""2from pydicom._uid_dict import UID_dictionary3from pydicom.uid import UID4from pynetdicom3 import StorageSOPClassList5from pynetdicom3.presentation import PresentationContext, PresentationService6from pynetdicom3.utils import PresentationContextManager7class TimePresentationContext:8 def setup(self):9 self.contexts = []10 for x in range(500):11 self.contexts.append(12 PresentationContext(1,13 '1.2.840.10008.5.1.4.1.1.2',14 ['1.2.840.10008.1.2',15 '1.2.840.10008.1.2.1',16 '1.2.840.10008.1.2.2'])17 )18 def time_create_single_transfer_syntax(self):19 """Time the creation of 100 presentation contexts with a single ts"""20 for x in range(500):21 PresentationContext(22 1,23 '1.2.840.10008.5.1.4.1.1.2',24 ['1.2.840.10008.1.2']25 )26 def time_create_double_transfer_syntax(self):27 for x in range(500):28 PresentationContext(29 1,30 '1.2.840.10008.5.1.4.1.1.2',31 ['1.2.840.10008.1.2', '1.2.840.10008.1.2.1']32 )33 def time_create_triple_transfer_syntax(self):34 for x in range(500):35 PresentationContext(36 1,37 '1.2.840.10008.5.1.4.1.1.2',38 [39 '1.2.840.10008.1.2',40 '1.2.840.10008.1.2.1',41 '1.2.840.10008.1.2.2'42 ]43 )44 def time_create_from_sop(self):45 """Test the time taken to create a PresentationContext from every46 available standard DICOM UID.47 """48 for uid in UID_dictionary:49 PresentationContext(50 1,51 uid,52 [53 '1.2.840.10008.1.2',54 '1.2.840.10008.1.2.1',55 '1.2.840.10008.1.2.2'56 ]57 )58 def time_create_from_sop_list(self):59 """Test the time taken to create Presentation Contexts using the60 predefined SOP Classes from the pyndx.sop_class module.61 """62 for ii, sop_class in enumerate(StorageSOPClassList):63 PresentationContext(64 ii * 2 + 1,65 sop_class.UID,66 [67 '1.2.840.10008.1.2',68 '1.2.840.10008.1.2.1',69 '1.2.840.10008.1.2.2'70 ]71 )72class TimePresentationAcceptorRoleNegotiation(object):73 """Time presentation context negotiation as acceptor with SCP/SCU Role74 Selection75 """76 def setup(self):77 # Requestor presentation contexts - max 12678 self.requestor_contexts = []79 self.requestor_transfer_syntaxes = ['1.2.840.10008.1.2',80 '1.2.840.10008.1.2.1',81 '1.2.840.10008.1.2.2']82 for ii, sop in enumerate(StorageSOPClassList):83 context = PresentationContext(ii * 2 + 1,84 sop.UID,85 ['1.2.840.10008.1.2',86 '1.2.840.10008.1.2.1',87 '1.2.840.10008.1.2.2'])88 context.SCP = True89 context.SCU = True90 self.requestor_contexts.append(context)91 # Acceptor presentation contexts - no max92 self.acceptor_contexts = []93 self.acceptor_transfer_syntaxes = ['1.2.840.10008.1.2',94 '1.2.840.10008.1.2.1',95 '1.2.840.10008.1.2.2']96 for uid in UID_dictionary:97 context = PresentationContext(1,98 uid,99 ['1.2.840.10008.1.2',100 '1.2.840.10008.1.2.1',101 '1.2.840.10008.1.2.2'])102 context.SCP = True103 context.SCU = False104 self.acceptor_contexts.append(context)105 def time_ps_ac_role(self):106 """Time a presentation service with SCP/SCU role negotiation."""107 presentation = PresentationService()108 presentation.negotiate_as_requestor(self.requestor_contexts,109 self.acceptor_contexts)110 def time_pcm_ac_role(self):111 """Time PresentationContextManager with SCP/SCU role negotiation."""112 pcm = PresentationContextManager()113 pcm.requestor_contexts = self.requestor_contexts114 pcm.acceptor_contexts = self.acceptor_contexts115class TimePresentationRequestorRoleNegotiation(object):116 """Time presentation context negotiation as requestor with SCP/SCU Role117 Selection118 """119 def setup(self):120 # Requestor presentation contexts - max 126121 self.requestor_contexts = []122 self.requestor_transfer_syntaxes = ['1.2.840.10008.1.2',123 '1.2.840.10008.1.2.1',124 '1.2.840.10008.1.2.2']125 for ii, sop in enumerate(StorageSOPClassList):126 context = PresentationContext(ii * 2 + 1,127 sop.UID,128 ['1.2.840.10008.1.2',129 '1.2.840.10008.1.2.1',130 '1.2.840.10008.1.2.2'])131 context.SCP = True132 context.SCU = True133 self.requestor_contexts.append(context)134 # Acceptor presentation contexts - no max135 self.acceptor_contexts = []136 self.acceptor_transfer_syntaxes = ['1.2.840.10008.1.2',137 '1.2.840.10008.1.2.1',138 '1.2.840.10008.1.2.2']139 for uid in UID_dictionary:140 context = PresentationContext(1,141 uid,142 ['1.2.840.10008.1.2'])143 context.Result = 0x00144 context.SCP = True145 context.SCU = True146 self.acceptor_contexts.append(context)147 def time_ps_rq_role(self):148 """Time a presentation service with SCP/SCU role negotiation."""149 presentation = PresentationService()150 presentation.negotiate_as_requestor(self.requestor_contexts,151 self.acceptor_contexts)152 def time_pcm_rq_role(self):153 """Time PresentationContextManager with SCP/SCU role negotiation."""154 pcm = PresentationContextManager()155 pcm.requestor_contexts = self.requestor_contexts156 pcm.acceptor_contexts = self.acceptor_contexts157class TimePresentationAcceptor(object):158 """Time presentation context negotiation as acceptor"""159 def setup(self):160 # Requestor presentation contexts - max 126161 self.requestor_contexts = []162 self.requestor_transfer_syntaxes = ['1.2.840.10008.1.2',163 '1.2.840.10008.1.2.1',164 '1.2.840.10008.1.2.2']165 for ii, sop in enumerate(StorageSOPClassList):166 self.requestor_contexts.append(167 PresentationContext(ii * 2 + 1,168 sop.UID,169 ['1.2.840.10008.1.2',170 '1.2.840.10008.1.2.1',171 '1.2.840.10008.1.2.2'])172 )173 # Acceptor presentation contexts - no max174 self.acceptor_contexts = []175 self.acceptor_transfer_syntaxes = ['1.2.840.10008.1.2',176 '1.2.840.10008.1.2.1',177 '1.2.840.10008.1.2.2']178 for uid in UID_dictionary:179 self.acceptor_contexts.append(180 PresentationContext(1,181 uid,182 ['1.2.840.10008.1.2',183 '1.2.840.10008.1.2.1',184 '1.2.840.10008.1.2.2'])185 )186 def time_ps_ac_basic(self):187 """Time a basic presentation service negotiation"""188 presentation = PresentationService()189 presentation.negotiate_as_acceptor(self.requestor_contexts,190 self.acceptor_contexts)191 def time_pcm_basic(self):192 """Time a basic PresentationContextManager negotiation"""193 pcm = PresentationContextManager()194 pcm.requestor_contexts = self.requestor_contexts195 pcm.acceptor_contexts = self.acceptor_contexts196 accepted = pcm.accepted197class TimePresentationRequestor(object):198 """Time presentation context negotiation as requestor"""199 def setup(self):200 # Requestor presentation contexts - max 126201 self.requestor_contexts = []202 self.requestor_transfer_syntaxes = ['1.2.840.10008.1.2',203 '1.2.840.10008.1.2.1',204 '1.2.840.10008.1.2.2']205 for ii, sop in enumerate(StorageSOPClassList):206 self.requestor_contexts.append(207 PresentationContext(ii * 2 + 1,208 sop.UID,209 ['1.2.840.10008.1.2',210 '1.2.840.10008.1.2.1',211 '1.2.840.10008.1.2.2'])212 )213 # Acceptor presentation contexts - no max214 self.acceptor_contexts = []215 self.acceptor_transfer_syntaxes = ['1.2.840.10008.1.2',216 '1.2.840.10008.1.2.1',217 '1.2.840.10008.1.2.2']218 for uid in UID_dictionary:219 context = PresentationContext(1,220 uid,221 ['1.2.840.10008.1.2'])222 context.Result = 0x00223 self.acceptor_contexts.append(context)224 def time_ps_rq_basic(self):225 """Time a basic presentation service negotiation."""226 presentation = PresentationService()227 presentation.negotiate_as_requestor(self.requestor_contexts,228 self.acceptor_contexts)229 def time_pcm_rq_basic(self):230 """Time a basic PresentationContextManager negotiation"""231 pcm = PresentationContextManager()232 pcm.requestor_contexts = self.requestor_contexts...

Full Screen

Full Screen

Playwright tutorial

LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.

Chapters:

  1. What is Playwright : Playwright is comparatively new but has gained good popularity. Get to know some history of the Playwright with some interesting facts connected with it.
  2. How To Install Playwright : Learn in detail about what basic configuration and dependencies are required for installing Playwright and run a test. Get a step-by-step direction for installing the Playwright automation framework.
  3. Playwright Futuristic Features: Launched in 2020, Playwright gained huge popularity quickly because of some obliging features such as Playwright Test Generator and Inspector, Playwright Reporter, Playwright auto-waiting mechanism and etc. Read up on those features to master Playwright testing.
  4. What is Component Testing: Component testing in Playwright is a unique feature that allows a tester to test a single component of a web application without integrating them with other elements. Learn how to perform Component testing on the Playwright automation framework.
  5. Inputs And Buttons In Playwright: Every website has Input boxes and buttons; learn about testing inputs and buttons with different scenarios and examples.
  6. Functions and Selectors in Playwright: Learn how to launch the Chromium browser with Playwright. Also, gain a better understanding of some important functions like “BrowserContext,” which allows you to run multiple browser sessions, and “newPage” which interacts with a page.
  7. Handling Alerts and Dropdowns in Playwright : Playwright interact with different types of alerts and pop-ups, such as simple, confirmation, and prompt, and different types of dropdowns, such as single selector and multi-selector get your hands-on with handling alerts and dropdown in Playright testing.
  8. Playwright vs Puppeteer: Get to know about the difference between two testing frameworks and how they are different than one another, which browsers they support, and what features they provide.
  9. Run Playwright Tests on LambdaTest: Playwright testing with LambdaTest leverages test performance to the utmost. You can run multiple Playwright tests in Parallel with the LammbdaTest test cloud. Get a step-by-step guide to run your Playwright test on the LambdaTest platform.
  10. Playwright Python Tutorial: Playwright automation framework support all major languages such as Python, JavaScript, TypeScript, .NET and etc. However, there are various advantages to Python end-to-end testing with Playwright because of its versatile utility. Get the hang of Playwright python testing with this chapter.
  11. Playwright End To End Testing Tutorial: Get your hands on with Playwright end-to-end testing and learn to use some exciting features such as TraceViewer, Debugging, Networking, Component testing, Visual testing, and many more.
  12. Playwright Video Tutorial: Watch the video tutorials on Playwright testing from experts and get a consecutive in-depth explanation of Playwright automation testing.

Run Playwright Python 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