Best Python code snippet using localstack_python
__init__.py
Source:__init__.py  
...25    A dialog template renderer based on the mustache templating language.26    """27    def __init__(self):28        self.templates = {}29    def load_template_file(self, template_name, filename):30        """31        Load a template by file name into the templates cache.32        Args:33            template_name (str): a unique identifier for a group of templates34            filename (str): a fully qualified filename of a mustache template.35        """36        with open(filename, 'r') as f:37            for line in f:38                template_text = line.strip()39                if template_name not in self.templates:40                    self.templates[template_name] = []41                self.templates[template_name].append(template_text)42    def render(self, template_name, context={}, index=None):43        """44        Given a template name, pick a template and render it using the context45        Args:46            template_name (str): the name of a template group.47            context (dict): dictionary representing values to be rendered48            index (int): optional, the specific index in the collection of49                templates50        Returns:51            str: the rendered string52        Raises:53            NotImplementedError: if no template can be found identified by54                template_name55        """56        if template_name not in self.templates:57            raise NotImplementedError("Template not found: %s" % template_name)58        template_functions = self.templates.get(template_name)59        if index is None:60            index = random.randrange(len(template_functions))61        else:62            index %= len(template_functions)63        return pystache.render(template_functions[index], context)64class DialogLoader(object):65    """66    Loads a collection of dialog files into a renderer implementation.67    """68    def __init__(self, renderer_factory=MustacheDialogRenderer):69        self.__renderer = renderer_factory()70    def load(self, dialog_dir):71        """72        Load all dialog files within the specified directory.73        Args:74            dialog_dir (str): directory that contains dialog files75        Returns:76            a loaded instance of a dialog renderer77        """78        if not os.path.exists(dialog_dir) or not os.path.isdir(dialog_dir):79            LOG.warning("No dialog found: " + dialog_dir)80            return self.__renderer81        for f in sorted(82                filter(lambda x: os.path.isfile(83                    os.path.join(dialog_dir, x)), os.listdir(dialog_dir))):84            dialog_entry_name = os.path.splitext(f)[0]85            self.__renderer.load_template_file(86                dialog_entry_name, os.path.join(dialog_dir, f))87        return self.__renderer88def get(phrase, lang=None, context=None):89    """90    Looks up a resource file for the given phrase.  If no file91    is found, the requested phrase is returned as the string.92    This will use the default language for translations.93    Args:94        phrase (str): resource phrase to retrieve/translate95        lang (str): the language to use96        context (dict): values to be inserted into the string97    Returns:98        str: a randomized and/or translated version of the phrase99    """100    if not lang:101        from mycroft.configuration import Configuration102        lang = Configuration.get().get("lang")103    filename = "text/" + lang.lower() + "/" + phrase + ".dialog"104    template = resolve_resource_file(filename)105    if not template:106        LOG.debug("Resource file not found: " + filename)107        return phrase108    stache = MustacheDialogRenderer()109    stache.load_template_file("template", template)110    if not context:111        context = {}...metricsTwo.py
Source:metricsTwo.py  
1import ruamel.yaml 2template_file=open(".github/workflows/metricsTemplate.yml")3load_template_file=ruamel.yaml.round_trip_load(template_file,preserve_quotes=True)4steps_tasks_template_file=load_template_file['jobs']['build']['steps']5FullMetrics_tasks= [6    'checkout',7    'Cache',8    'Set up JDK 1.8' ,9    'Build Gradle',10    'Running Gradle Test',11    'Running Sonarqube scan',12    'Retrieve version',13    'Generate Change logs',14    'Get Change logs from Comment',15    'Tag',16    'Create Release',17    'Delete merged branch',18    ]19Api_tasks=[20    'checkout',21    'Cache',22    'Set up JDK 1.8' ,23    'Build Gradle',24    'Running Gradle Test',25    'Set Up Cloud SDK For Staging',26    'Staging Deployment',27    'Set Up Cloud SDK For Live',28    'Live Deployment'29]30MetricsWorker_tasks=[31    'checkout',32    'Cache',33    'Set up JDK 1.8' ,34    'Build Gradle',35    'Running Gradle Test',36    'Set Up Cloud SDK For Staging',37    'Staging Deployment',38    'Set Up Cloud SDK For Live',39    'Live Deployment'40]41WebApp_tasks=[42    'checkout',43    'Cache',44    'Set up JDK 1.8' ,45    'Cache node modules',46    'Setup node',47    'Build Gradle',48    'Running Gradle Test',49    'Running Npm Build'50    'Set Up Cloud SDK For Staging',51    'Staging Deployment',52    'Set Up Cloud SDK For Live',53    'Live Deployment'54]55required_workflow_files = ['Api','MetricsWorker','WebApp','FullMetrics']56def generateFiles(tasks,Route,steps_tasks,load_template_file):57    new_tasks_names=[]58    for name in tasks:59        for i in range(len(steps_tasks)):60            if ('name' in steps_tasks[i] and steps_tasks[i]['name'] == name):61                new_tasks_names.append(steps_tasks[i])62        63    load_template_file['jobs']['build']['steps']=new_tasks_names64    with open(r'.github/workflows/{}.yml'.format(Route),'w') as file:65        documents = ruamel.yaml.round_trip_dump(load_template_file, file,default_flow_style=False)66#We use generateFiles function to generate the workflow files67generateFiles(Api_tasks,'Api',steps_tasks_template_file,load_template_file)68generateFiles(WebApp_tasks,'WebApp',steps_tasks_template_file,load_template_file)69generateFiles(MetricsWorker_tasks,'MetricsWorker',steps_tasks_template_file,load_template_file)70generateFiles(FullMetrics_tasks,'FullMetrics',steps_tasks_template_file,load_template_file)71def definingRoute(Route,first,second):72    with open('.github/workflows/{}.yml'.format(Route), "r+") as f:73        contents = f.read()74        f.seek(0)75        f.write(contents.replace(first,second))76        f.truncate()77        78#Route configuration for each workflow79for Route in required_workflow_files:...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!!
