Best Python code snippet using playwright-python
lattice_test.py
Source:lattice_test.py  
...116        lattice.nodes[3].word = 'D'117        lattice.nodes[4].word = 'E'118        lattice.initial_node = lattice.nodes[0]119        lattice.final_node = lattice.nodes[4]120        lattice._add_link(lattice.nodes[0], lattice.nodes[1])121        lattice._add_link(lattice.nodes[0], lattice.nodes[2])122        lattice._add_link(lattice.nodes[1], lattice.nodes[3])123        lattice._add_link(lattice.nodes[2], lattice.nodes[3])124        lattice._add_link(lattice.nodes[3], lattice.nodes[4])125        lattice._move_words_to_links()126        self.assertEqual(lattice.links[0].word, 'B')127        self.assertEqual(lattice.links[1].word, 'C')128        self.assertEqual(lattice.links[2].word, 'D')129        self.assertEqual(lattice.links[3].word, 'D')130        self.assertEqual(lattice.links[4].word, 'E')131        for node in lattice.nodes:132            self.assertFalse(hasattr(node, 'word'))133    def test_sorted_nodes(self):134        lattice = Lattice()135        lattice.nodes = [Lattice.Node(id) for id in range(9)]136        lattice.nodes[0].time = 0.0137        lattice.nodes[2].time = 1.0138        lattice.nodes[4].time = 2.0139        lattice.nodes[3].time = 3.0140        lattice.nodes[5].time = 4.0141        lattice.nodes[1].time = 4.0142        lattice.nodes[6].time = 5.0143        lattice.nodes[7].time = None144        lattice.nodes[8].time = -1.0145        lattice._add_link(lattice.nodes[0], lattice.nodes[2])146        lattice._add_link(lattice.nodes[0], lattice.nodes[4])147        lattice._add_link(lattice.nodes[2], lattice.nodes[3])148        lattice._add_link(lattice.nodes[4], lattice.nodes[3])149        lattice._add_link(lattice.nodes[2], lattice.nodes[5])150        lattice._add_link(lattice.nodes[3], lattice.nodes[5])151        lattice._add_link(lattice.nodes[5], lattice.nodes[1])152        lattice._add_link(lattice.nodes[5], lattice.nodes[6])153        lattice._add_link(lattice.nodes[5], lattice.nodes[7])154        lattice._add_link(lattice.nodes[1], lattice.nodes[8])155        lattice._add_link(lattice.nodes[6], lattice.nodes[8])156        lattice._add_link(lattice.nodes[7], lattice.nodes[8])157        lattice.initial_node = lattice.nodes[0]158        lattice.final_node = lattice.nodes[8]159        sorted_nodes = lattice.sorted_nodes()160        self.assertEqual(sorted_nodes[0].id, 0)161        self.assertEqual(sorted_nodes[1].id, 2)162        self.assertEqual(sorted_nodes[2].id, 4)163        self.assertEqual(sorted_nodes[3].id, 3)164        self.assertEqual(sorted_nodes[4].id, 5)165        # Topologically equal nodes will be sorted in ascending time. The nodes166        # that don't have time will go last.167        self.assertEqual(sorted_nodes[5].id, 1)168        self.assertEqual(sorted_nodes[6].id, 6)169        self.assertEqual(sorted_nodes[7].id, 7)170        self.assertEqual(sorted_nodes[8].id, 8)...coap_retargeting_handler.py
Source:coap_retargeting_handler.py  
...47            for scl in result.resource.sclCollection:48                path = scl.path49                link = scl.link50                # add scl51                self._add_link(link, path)52        self.api.handle_request_indication(req).then(handle_scls)53        self._started()54    def _handle_connector_created(self, connector):55        connector.register_route(NOTIFY_ROUTE, self._handle_notify)56    def _handle_m2mpoc_created(self, instance, req_ind=None):57        self._add_link(instance.contactInfo, instance.path)58    def _handle_m2mpoc_deleted(self, instance, req_ind):59        self._remove_link(req_ind.path)60        pass61    def _handle_m2mpoc_updated(self, instance, req_ind=None):62        self._add_link(instance.contactInfo, instance.path)63        pass64    def _handle_scl_created(self, instance, req_ind=None):65        self._add_link(instance.link, instance.path)66    def _handle_scl_deleted(self, instance, req_ind):67        self._remove_link(req_ind.path)68        pass69    def _handle_scl_updated(self, instance, req_ind=None):70        self._remove_link(req_ind.path)71        self._add_link(instance.link, instance.path)72        pass73    def _handle_cse_created(self, instance, req_ind=None):74        self.logger.debug("_handle_cse_created(instance=%s, req_ind=%s)", instance, req_ind)75        self._add_link(instance.pointOfAccess, instance.path, is_etsi=False)76    def _handle_cse_deleted(self, instance, req_ind):77        self.logger.debug("_handle_cse_deleted(req_ind=%s)", req_ind)78        self._remove_link(req_ind.path)79        pass80    def _handle_cse_updated(self, instance, req_ind=None):81        self.logger.debug("_handle_cse_updated(instance=%s, req_ind=%s)", instance, req_ind)82        self._remove_link(req_ind.path)83        self._add_link(instance.pointOfAccess, instance.path, is_etsi=False)84        pass85    def _handle_notify(self, request):86        request_indication = map_request_to_request_indication(request)87        try:88            data = request_indication.resource.read()89        except AttributeError:90            data = request_indication.resource91        notify = NotifyRequestIndication(92            path=request_indication.path[len(_notify_path):],93            resource=data,94            content_type=request_indication.content_type95        )96        return self.api.send_request_indication(notify) \97            .then(lambda r: map_response_confirmation_to_response(request, r),98                  lambda e: map_error_response_confirmation_to_response(request,99                                                                        e))100    def _handle_etsi_retargeting(self, request):101        result = self.api.send_request(request)102        return result103    def _handle_onem2m_retargeting(self, request):104        return self.api.send_request(request)105    def _add_link(self, link, path, is_etsi=True):106        """107        Adds the link to the list of known URIs, and registers a retargeting108        handler.109        :param link: link to other scl110        :type link: str111        """112        def handle_link(link):113            link = self.__link_to_coap(link)114            if path in self._known_uris:115                if self._known_uris[path] == link:116                    self.logger.debug("SCL link of %s (%s) is already known", path,117                                      link)118                else:119                    # same path, different link. remove old link, add new one120                    self.logger.debug("Updating link of %s: %s -> %s", path,121                                      self._known_uris[path], link)122                    self._remove_link(path)123                    self._add_link(link, path)124            elif link in self._known_uris.values():125                # same link, different path, let's find which path occupies it126                for key, value in self._known_uris.iteritems():127                    if value == link:128                        self.logger.debug(129                            "SCL link of %s (%s) is already used by %s", path, link,130                            key)131                        break132            else:133                self.logger.debug("Adding link of %s (%s)", path, link)134                self._known_uris[path] = link135                if is_etsi:136                    self.api.register_retargeting_handler(link, self._handle_etsi_retargeting)137                else:...retargeting_handler.py
Source:retargeting_handler.py  
...39            for scl in result.resource.sclCollection:40                path = scl.path41                link = scl.link42                # add scl43                self._add_link(link, path)44        self.api.handle_request_indication(req).then(handle_scls)45        self._started()46    def _handle_connector_created(self, connector):47        connector.register_route(NOTIFY_ROUTE, self._handle_notify)48    def _handle_m2mpoc_created(self, instance, req_ind=None):49        self._add_link(instance.contactInfo, instance.path)50    def _handle_m2mpoc_deleted(self, instance, req_ind):51        self._remove_link(req_ind.path)52        pass53    def _handle_m2mpoc_updated(self, instance, req_ind=None):54        self._add_link(instance.contactInfo, instance.path)55        pass56    def _handle_scl_created(self, instance, req_ind=None):57        self._add_link(instance.link, instance.path)58    def _handle_scl_deleted(self, instance, req_ind):59        self._remove_link(req_ind.path)60        pass61    def _handle_scl_updated(self, instance, req_ind=None):62        self._add_link(instance.link, instance.path)63        pass64    def _handle_notify(self, request):65        request_indication = map_request_to_request_indication(request)66        try:67            data = request_indication.resource.read()68        except AttributeError:69            data = request_indication.resource70        notify = NotifyRequestIndication(71            path=unquote_plus(request_indication.path[len(_notify_path):]),72            resource=data,73            content_type=request_indication.content_type74        )75        return self.api.send_request_indication(notify).then(76            lambda r: map_response_confirmation_to_response(request, r),77            lambda e: map_error_response_confirmation_to_response(request, e))78    def _handle_retargeting(self, request):79        # TODO: move to plugin80        request_indication = map_request_to_request_indication(request)81        self.logger.debug("Retargeting to: %s (%s)", request_indication.path,82                          request_indication.requestingEntity)83        if request_indication.method == "create":84            try:85                typename, data = decode_content(request_indication)86            except SCLBadRequest:87                pass88            else:89                if typename == "subscription":90                    try:91                        contact = data["contact"]92                    except KeyError as e:93                        raise SCLMissingValue(e)94                    scheme, _, _ = contact.partition("://")95                    contact = urljoin(self.api.get_mid_uri(scheme),96                                      _notify_path + quote_plus(contact))97                    self.logger.debug("Rewrote subscription contact: %s",98                                      contact)99                    data["contact"] = contact100                    request_indication.set_resource(typename, data)101        elif request_indication.method in ("update", "notify"):102            try:103                request_indication.resource = \104                    request_indication.resource.read()105            except AttributeError:106                pass107        request_indication.via.append(self.scl_id)108        # TODO: put this back in109        # if request_indication.correlationID:110        #    return self._handle_correlationID(request_indication)111        return self.api.send_request_indication(request_indication).then(112            lambda r: map_response_confirmation_to_response(request, r),113            lambda e: map_error_response_confirmation_to_response(request, e))114    def _add_link(self, link, path):115        """116        Adds the link to the list of known URIs, and registers a retargeting117        handler.118        :param link: link to other scl119        :type link: str120        """121        if path in self._known_uris:122            if self._known_uris[path] == link:123                self.logger.debug("SCL link of %s (%s) is already known", path,124                                  link)125            else:126                # same path, different link. remove old link, add new one127                self.logger.debug("Updating link of %s: %s -> %s", path,128                                  self._known_uris[path], link)129                self._remove_link(path)130                self._add_link(link, path)131        elif link in self._known_uris.values():132            # same link, different path, let's find which path occupies it133            for key, value in self._known_uris.iteritems():134                if value == link:135                    self.logger.debug(136                        "SCL link of %s (%s) is already used by %s", path, link,137                        key)138                    break139        else:140            self.logger.debug("Adding link of %s (%s)", path, link)141            self._known_uris[path] = link142            self.api.register_retargeting_handler(link,143                                                  self._handle_retargeting)144    def _remove_link(self, path):...nlp_to_graph.py
Source:nlp_to_graph.py  
...52        for token_i in range(num_tokens-1):53            true_token_i = offset + token_i54            for token_j in range(token_i+1, num_tokens):55                true_token_j = offset + token_j56                self._add_link(57                    true_token_i,58                    true_token_j,59                    self.priors['sentence'],60                    'sentence-{}'.format(sent_i))61        # add special "alpha" semantic tags62        for token_i in range(num_tokens):63            # token_i = relative loc in sentence64            # true_token_i = absolute loc in graph65            true_token_i = offset + token_i66            # add adjacency-1 through adjacency-3 links67            for adj_i in [0, 1, 2]:68                if token_i > adj_i and token_i < num_tokens - (adj_i+1):69                    self._add_link(70                        true_token_i,71                        true_token_i+adj_i+1,72                        self.priors['adjacency-{}'.format(adj_i+1)],73                        'adjacency-{}'.format(adj_i+1))74                    self._add_link(75                        true_token_i,76                        true_token_i-(adj_i+1),77                        self.priors['adjacency-{}'.format(adj_i+1)],78                        'adjacency-{}'.format(adj_i+1))79            # add lemma links80            lemma = lemmas[token_i]81            for lemma_i in self.lemma_hash[lemma]:82                self._add_link(83                    true_token_i,84                    lemma_i,85                    self.priors['lemma'],86                    'lemma')87            self.lemma_hash[lemma].append(true_token_i)88            # add pos links89            pos = pos_tags[token_i]90            for pos_i in self.pos_hash[pos]:91                self._add_link(92                    true_token_i,93                    pos_i,94                    self.priors['pos'],95                    'pos-{}'.format(pos.lower()))96            self.pos_hash[pos].append(true_token_i)97            # add ner links98            ner = ner_tags[token_i]99            if ner != 'O':  # ignore OTHER tags100                for ner_i in self.name_hash[ner]:101                    self._add_link(102                        true_token_i,103                        ner_i,104                        self.priors['name'],105                        'ner-{}'.format(ner.lower()))106                self.name_hash[ner].append(true_token_i)107            # add dep links108            # FIXME: for now I'm treating all dependencies109            # the same -- definitely not okay.110            cur_deps_dict = dep_tags[token_i]['deps']111            for dep_key, dep_vals in cur_deps_dict.iteritems():112                for dep_val in dep_vals:113                    self._add_link(114                        true_token_i,115                        offset + dep_val - 1,  # 1 offset for some reason116                        self.priors['dependency'],117                        'dependency-{}'.format(dep_key))118        print('[{}] Sentence ({}) Added to Graph'.format(str(datetime.now()), sent_i))119        print('[{}] Graph State ({} nodes | {} edges)'.format(120            str(datetime.now()),121            self.graph.num_nodes,122            self.graph.num_edges))123    def _add_link(self, i, j, prior, link_type):124        self.graph.add_edge(i, j, prior, edge_type=link_type)125    def run(self, source_list):126        ''' list of indexes for source nodes '''127        print('[{}] Begin Spreading Activation'.format(str(datetime.now())))128        similar_nodes, dissimilar_nodes = self.graph.spreading_activation(source_list)129        print('[{}] Spreading Activation Converged'.format(str(datetime.now())))130        return similar_nodes, dissimilar_nodes131    def api_run(self, source_list):132        similar_nodes, dissimilar_nodes = self.run(source_list)133        similar_indexs = [node.index for node in similar_nodes]134        similar_activation = [node.activation for node in similar_nodes]135        similar_tokens = [node.value for node in similar_nodes]136        dissimilar_indexs = [node.index for node in dissimilar_nodes]137        dissimilar_activation = [node.activation for node in dissimilar_nodes]...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.
Get 100 minutes of automation test minutes FREE!!
