Best Python code snippet using pyresttest_python
cached.py
Source:cached.py
1"""2Wrapper class that takes a list of template loaders as an argument and attempts3to load templates from them in order, caching the result.4"""5import hashlib6from django.template.base import Template, TemplateDoesNotExist7from django.utils.encoding import force_bytes8from .base import Loader as BaseLoader9class Loader(BaseLoader):10 is_usable = True11 def __init__(self, engine, loaders):12 self.template_cache = {}13 self.find_template_cache = {}14 self.loaders = engine.get_template_loaders(loaders)15 super(Loader, self).__init__(engine)16 def cache_key(self, template_name, template_dirs):17 if template_dirs:18 # If template directories were specified, use a hash to differentiate19 return '-'.join([template_name, hashlib.sha1(force_bytes('|'.join(template_dirs))).hexdigest()])20 else:21 return template_name22 def find_template(self, name, dirs=None):23 """24 Helper method. Lookup the template :param name: in all the configured loaders25 """26 key = self.cache_key(name, dirs)27 try:28 result = self.find_template_cache[key]29 except KeyError:30 result = None31 for loader in self.loaders:32 try:33 template, display_name = loader(name, dirs)34 except TemplateDoesNotExist:35 pass36 else:37 origin = self.engine.make_origin(display_name, loader, name, dirs)38 result = template, origin39 break40 self.find_template_cache[key] = result41 if result:42 return result43 else:44 self.template_cache[key] = TemplateDoesNotExist45 raise TemplateDoesNotExist(name)46 def load_template(self, template_name, template_dirs=None):47 key = self.cache_key(template_name, template_dirs)48 template_tuple = self.template_cache.get(key)49 # A cached previous failure:50 if template_tuple is TemplateDoesNotExist:51 raise TemplateDoesNotExist52 elif template_tuple is None:53 template, origin = self.find_template(template_name, template_dirs)54 if not hasattr(template, 'render'):55 try:56 template = Template(template, origin, template_name, self.engine)57 except TemplateDoesNotExist:58 # If compiling the template we found raises TemplateDoesNotExist,59 # back off to returning the source and display name for the template60 # we were asked to load. This allows for correct identification (later)61 # of the actual template that does not exist.62 self.template_cache[key] = (template, origin)63 self.template_cache[key] = (template, None)64 return self.template_cache[key]65 def reset(self):66 "Empty the template cache."67 self.template_cache.clear()...
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!!