Best Python code snippet using avocado_python
lookup.py
Source:lookup.py  
...44        :param relativeto: if present, the given ``uri`` is assumed to45         be relative to this URI.46        """47        raise NotImplementedError()48    def filename_to_uri(self, uri, filename):49        """Convert the given ``filename`` to a URI relative to50           this :class:`.TemplateCollection`."""51        return uri52    def adjust_uri(self, uri, filename):53        """Adjust the given ``uri`` based on the calling ``filename``.54        When this method is called from the runtime, the55        ``filename`` parameter is taken directly to the ``filename``56        attribute of the calling template. Therefore a custom57        :class:`.TemplateCollection` subclass can place any string58        identifier desired in the ``filename`` parameter of the59        :class:`.Template` objects it constructs and have them come back60        here.61        """62        return uri63class TemplateLookup(TemplateCollection):64    """Represent a collection of templates that locates template source files65    from the local filesystem.66    The primary argument is the ``directories`` argument, the list of67    directories to search:68    .. sourcecode:: python69        lookup = TemplateLookup(["/path/to/templates"])70        some_template = lookup.get_template("/index.html")71    The :class:`.TemplateLookup` can also be given :class:`.Template` objects72    programatically using :meth:`.put_string` or :meth:`.put_template`:73    .. sourcecode:: python74        lookup = TemplateLookup()75        lookup.put_string("base.html", '''76            <html><body>${self.next()}</body></html>77        ''')78        lookup.put_string("hello.html", '''79            <%include file='base.html'/>80            Hello, world !81        ''')82    :param directories: A list of directory names which will be83     searched for a particular template URI. The URI is appended84     to each directory and the filesystem checked.85    :param collection_size: Approximate size of the collection used86     to store templates. If left at its default of ``-1``, the size87     is unbounded, and a plain Python dictionary is used to88     relate URI strings to :class:`.Template` instances.89     Otherwise, a least-recently-used cache object is used which90     will maintain the size of the collection approximately to91     the number given.92    :param filesystem_checks: When at its default value of ``True``,93     each call to :meth:`.TemplateLookup.get_template()` will94     compare the filesystem last modified time to the time in95     which an existing :class:`.Template` object was created.96     This allows the :class:`.TemplateLookup` to regenerate a97     new :class:`.Template` whenever the original source has98     been updated. Set this to ``False`` for a very minor99     performance increase.100    :param modulename_callable: A callable which, when present,101     is passed the path of the source file as well as the102     requested URI, and then returns the full path of the103     generated Python module file. This is used to inject104     alternate schemes for Python module location. If left at105     its default of ``None``, the built in system of generation106     based on ``module_directory`` plus ``uri`` is used.107    All other keyword parameters available for108    :class:`.Template` are mirrored here. When new109    :class:`.Template` objects are created, the keywords110    established with this :class:`.TemplateLookup` are passed on111    to each new :class:`.Template`.112    """113    def __init__(self,114                        directories=None,115                        module_directory=None,116                        filesystem_checks=True,117                        collection_size=-1,118                        format_exceptions=False,119                        error_handler=None,120                        disable_unicode=False,121                        bytestring_passthrough=False,122                        output_encoding=None,123                        encoding_errors='strict',124                        cache_args=None,125                        cache_impl='beaker',126                        cache_enabled=True,127                        cache_type=None,128                        cache_dir=None,129                        cache_url=None,130                        modulename_callable=None,131                        module_writer=None,132                        default_filters=None,133                        buffer_filters=(),134                        strict_undefined=False,135                        imports=None,136                        future_imports=None,137                        enable_loop=True,138                        input_encoding=None,139                        preprocessor=None,140                        lexer_cls=None):141        self.directories = [posixpath.normpath(d) for d in142                            util.to_list(directories, ())143                            ]144        self.module_directory = module_directory145        self.modulename_callable = modulename_callable146        self.filesystem_checks = filesystem_checks147        self.collection_size = collection_size148        if cache_args is None:149            cache_args = {}150        # transfer deprecated cache_* args151        if cache_dir:152            cache_args.setdefault('dir', cache_dir)153        if cache_url:154            cache_args.setdefault('url', cache_url)155        if cache_type:156            cache_args.setdefault('type', cache_type)157        self.template_args = {158            'format_exceptions':format_exceptions,159            'error_handler':error_handler,160            'disable_unicode':disable_unicode,161            'bytestring_passthrough':bytestring_passthrough,162            'output_encoding':output_encoding,163            'cache_impl':cache_impl,164            'encoding_errors':encoding_errors,165            'input_encoding':input_encoding,166            'module_directory':module_directory,167            'module_writer':module_writer,168            'cache_args':cache_args,169            'cache_enabled':cache_enabled,170            'default_filters':default_filters,171            'buffer_filters':buffer_filters,172            'strict_undefined':strict_undefined,173            'imports':imports,174            'future_imports':future_imports,175            'enable_loop':enable_loop,176            'preprocessor':preprocessor,177            'lexer_cls':lexer_cls178        }179        if collection_size == -1:180            self._collection = {}181            self._uri_cache = {}182        else:183            self._collection = util.LRUCache(collection_size)184            self._uri_cache = util.LRUCache(collection_size)185        self._mutex = threading.Lock()186    def get_template(self, uri):187        """Return a :class:`.Template` object corresponding to the given188        ``uri``.189        .. note:: The ``relativeto`` argument is not supported here at the moment.190        """191        try:192            if self.filesystem_checks:193                return self._check(uri, self._collection[uri])194            else:195                return self._collection[uri]196        except KeyError:197            u = re.sub(r'^\/+', '', uri)198            for dir in self.directories:199                srcfile = posixpath.normpath(posixpath.join(dir, u))200                if os.path.isfile(srcfile):201                    return self._load(srcfile, uri)202            else:203                raise exceptions.TopLevelLookupException(204                                    "Cant locate template for uri %r" % uri)205    def adjust_uri(self, uri, relativeto):206        """Adjust the given ``uri`` based on the given relative URI."""207        key = (uri, relativeto)208        if key in self._uri_cache:209            return self._uri_cache[key]210        if uri[0] != '/':211            if relativeto is not None:212                v = self._uri_cache[key] = posixpath.join(213                                            posixpath.dirname(relativeto), uri)214            else:215                v = self._uri_cache[key] = '/' + uri216        else:217            v = self._uri_cache[key] = uri218        return v219    def filename_to_uri(self, filename):220        """Convert the given ``filename`` to a URI relative to221           this :class:`.TemplateCollection`."""222        try:223            return self._uri_cache[filename]224        except KeyError:225            value = self._relativeize(filename)226            self._uri_cache[filename] = value227            return value228    def _relativeize(self, filename):229        """Return the portion of a filename that is 'relative'230           to the directories in this lookup.231        """232        filename = posixpath.normpath(filename)233        for dir in self.directories:...cssnamespacerule.py
Source:cssnamespacerule.py  
...133                    new['wellformed'] = False134                    self._log.error(135                        'CSSNamespaceRule: Unexpected string.', token)136                    return expected137            def _uri(expected, seq, token, tokenizer=None):138                # the namespace URI as URI which is DEPRECATED139                if expected.endswith('uri'):140                    uri = self._uritokenvalue(token)141                    new['uri'] = uri142                    seq.append(new['uri'], 'namespaceURI')143                    return ';'144                else:145                    new['wellformed'] = False146                    self._log.error(147                        'CSSNamespaceRule: Unexpected URI.', token)148                    return expected149            def _char(expected, seq, token, tokenizer=None):150                # final ;151                val = self._tokenvalue(token)...tfidf.py
Source:tfidf.py  
1#2# Licensed to the Apache Software Foundation (ASF) under one or more3# contributor license agreements.  See the NOTICE file distributed with4# this work for additional information regarding copyright ownership.5# The ASF licenses this file to You under the Apache License, Version 2.06# (the "License"); you may not use this file except in compliance with7# the License.  You may obtain 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,13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14# See the License for the specific language governing permissions and15# limitations under the License.16#17"""A TF-IDF workflow (term frequency - inverse document frequency).18For an explanation of the TF-IDF algorithm see the following link:19http://en.wikipedia.org/wiki/Tf-idf20"""21# pytype: skip-file22import argparse23import glob24import math25import re26import apache_beam as beam27from apache_beam.io import ReadFromText28from apache_beam.io import WriteToText29from apache_beam.options.pipeline_options import PipelineOptions30from apache_beam.options.pipeline_options import SetupOptions31from apache_beam.pvalue import AsSingleton32def read_documents(pipeline, uris):33  """Read the documents at the provided uris and returns (uri, line) pairs."""34  pcolls = []35  for uri in uris:36    pcolls.append(37        pipeline38        | 'Read: %s' % uri >> ReadFromText(uri)39        | 'WithKey: %s' % uri >> beam.Map(lambda v, uri: (uri, v), uri))40  return pcolls | 'FlattenReadPColls' >> beam.Flatten()41class TfIdf(beam.PTransform):42  """A transform containing a basic TF-IDF pipeline.43  The input consists of KV objects where the key is the document's URI and44  the value is a piece of the document's content.45  The output is mapping from terms to scores for each document URI.46  """47  def expand(self, uri_to_content):48    # Compute the total number of documents, and prepare a singleton49    # PCollection to use as side input.50    total_documents = (51        uri_to_content52        | 'GetUris 1' >> beam.Keys()53        | 'GetUniqueUris' >> beam.Distinct()54        | 'CountUris' >> beam.combiners.Count.Globally())55    # Create a collection of pairs mapping a URI to each of the words56    # in the document associated with that that URI.57    def split_into_words(uri_line):58      (uri, line) = uri_line59      return [(uri, w.lower()) for w in re.findall(r'[A-Za-z\']+', line)]60    uri_to_words = (61        uri_to_content62        | 'SplitWords' >> beam.FlatMap(split_into_words))63    # Compute a mapping from each word to the total number of documents64    # in which it appears.65    word_to_doc_count = (66        uri_to_words67        | 'GetUniqueWordsPerDoc' >> beam.Distinct()68        | 'GetWords' >> beam.Values()69        | 'CountDocsPerWord' >> beam.combiners.Count.PerElement())70    # Compute a mapping from each URI to the total number of words in the71    # document associated with that URI.72    uri_to_word_total = (73        uri_to_words74        | 'GetUris 2' >> beam.Keys()75        | 'CountWordsInDoc' >> beam.combiners.Count.PerElement())76    # Count, for each (URI, word) pair, the number of occurrences of that word77    # in the document associated with the URI.78    uri_and_word_to_count = (79        uri_to_words80        | 'CountWord-DocPairs' >> beam.combiners.Count.PerElement())81    # Adjust the above collection to a mapping from (URI, word) pairs to counts82    # into an isomorphic mapping from URI to (word, count) pairs, to prepare83    # for a join by the URI key.84    def shift_keys(uri_word_count):85      return (uri_word_count[0][0], (uri_word_count[0][1], uri_word_count[1]))86    uri_to_word_and_count = (87        uri_and_word_to_count88        | 'ShiftKeys' >> beam.Map(shift_keys))89    # Perform a CoGroupByKey (a sort of pre-join) on the prepared90    # uri_to_word_total and uri_to_word_and_count tagged by 'word totals' and91    # 'word counts' strings. This yields a mapping from URI to a dictionary92    # that maps the above mentioned tag strings to an iterable containing the93    # word total for that URI and word and count respectively.94    #95    # A diagram (in which '[]' just means 'iterable'):96    #97    #   URI: {'word totals': [count],  # Total words within this URI's document.98    #         'word counts': [(word, count),  # Counts of specific words99    #                         (word, count),  # within this URI's document.100    #                         ... ]}101    uri_to_word_and_count_and_total = ({102        'word totals': uri_to_word_total, 'word counts': uri_to_word_and_count103    }104                                       | 'CoGroupByUri' >> beam.CoGroupByKey())105    # Compute a mapping from each word to a (URI, term frequency) pair for each106    # URI. A word's term frequency for a document is simply the number of times107    # that word occurs in the document divided by the total number of words in108    # the document.109    def compute_term_frequency(uri_count_and_total):110      (uri, count_and_total) = uri_count_and_total111      word_and_count = count_and_total['word counts']112      # We have an iterable for one element that we want extracted.113      [word_total] = count_and_total['word totals']114      for word, count in word_and_count:115        yield word, (uri, float(count) / word_total)116    word_to_uri_and_tf = (117        uri_to_word_and_count_and_total118        | 'ComputeTermFrequencies' >> beam.FlatMap(compute_term_frequency))119    # Compute a mapping from each word to its document frequency.120    # A word's document frequency in a corpus is the number of121    # documents in which the word appears divided by the total122    # number of documents in the corpus.123    #124    # This calculation uses a side input, a Dataflow-computed auxiliary value125    # presented to each invocation of our MapFn lambda. The second argument to126    # the function (called total---note that the first argument is a tuple)127    # receives the value we listed after the lambda in Map(). Additional side128    # inputs (and ordinary Python values, too) can be provided to MapFns and129    # DoFns in this way.130    def div_word_count_by_total(word_count, total):131      (word, count) = word_count132      return (word, float(count) / total)133    word_to_df = (134        word_to_doc_count135        | 'ComputeDocFrequencies' >> beam.Map(136            div_word_count_by_total, AsSingleton(total_documents)))137    # Join the term frequency and document frequency collections,138    # each keyed on the word.139    word_to_uri_and_tf_and_df = ({140        'tf': word_to_uri_and_tf, 'df': word_to_df141    }142                                 | 'CoGroupWordsByTf-df' >> beam.CoGroupByKey())143    # Compute a mapping from each word to a (URI, TF-IDF) score for each URI.144    # There are a variety of definitions of TF-IDF145    # ("term frequency - inverse document frequency") score; here we use a146    # basic version that is the term frequency divided by the log of the147    # document frequency.148    def compute_tf_idf(word_tf_and_df):149      (word, tf_and_df) = word_tf_and_df150      [docf] = tf_and_df['df']151      for uri, tf in tf_and_df['tf']:152        yield word, (uri, tf * math.log(1 / docf))153    word_to_uri_and_tfidf = (154        word_to_uri_and_tf_and_df155        | 'ComputeTf-idf' >> beam.FlatMap(compute_tf_idf))156    return word_to_uri_and_tfidf157def run(argv=None, save_main_session=True):158  """Main entry point; defines and runs the tfidf pipeline."""159  parser = argparse.ArgumentParser()160  parser.add_argument('--uris', required=True, help='URIs to process.')161  parser.add_argument(162      '--output', required=True, help='Output file to write results to.')163  known_args, pipeline_args = parser.parse_known_args(argv)164  # We use the save_main_session option because one or more DoFn's in this165  # workflow rely on global context (e.g., a module imported at module level).166  pipeline_options = PipelineOptions(pipeline_args)167  pipeline_options.view_as(SetupOptions).save_main_session = save_main_session168  with beam.Pipeline(options=pipeline_options) as p:169    # Read documents specified by the uris command line option.170    pcoll = read_documents(p, glob.glob(known_args.uris))171    # Compute TF-IDF information for each word.172    output = pcoll | TfIdf()173    # Write the output using a "Write" transform that has side effects.174    # pylint: disable=expression-not-assigned175    output | 'write' >> WriteToText(known_args.output)176    # Execute the pipeline and wait until it is completed.177if __name__ == '__main__':...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!!
