defsetval(name:str, path:str, value:str)-> Optional[bool]: if name.find("__")>=0: returnFalse for word in __forbidden_name__: if name==word: returnFalse for word in __forbidden_path__: if path.find(word)>=0: returnFalse obj=globals()[name] try: pydash.set_(obj,path,value) except: returnFalse returnTrue
defset_(obj: T, path: PathT, value: t.Any) -> T: """ Sets the value of an object described by `path`. If any part of the object path doesn't exist, it will be created. Args: obj: Object to modify. path: Target path to set value to. value: Value to set. Returns: Modified `obj`. Warning: `obj` is modified in place. Example: >>> set_({}, "a.b.c", 1) {'a': {'b': {'c': 1}}} >>> set_({}, "a.0.c", 1) {'a': {'0': {'c': 1}}} >>> set_([1, 2], "[2][0]", 1) [1, 2, [1]] >>> set_({}, "a.b[0].c", 1) {'a': {'b': [{'c': 1}]}} .. versionadded:: 2.2.0 .. versionchanged:: 3.3.0 Added :func:`set_` as main definition and :func:`deep_set` as alias. .. versionchanged:: 4.0.0 - Modify `obj` in place. - Support creating default path values as ``list`` or ``dict`` based on whether key or index substrings are used. - Remove alias ``deep_set``. """ return set_with(obj, path, value)
deftemplate(*args, **kwargs): """ Get a rendered template as a string iterator. You can use a name, a filename or a template string as first parameter. Template rendering arguments can be passed as dictionaries or directly (as keyword arguments). """ tpl = args[0] if args elseNone for dictarg in args[1:]: kwargs.update(dictarg) adapter = kwargs.pop('template_adapter', SimpleTemplate) lookup = kwargs.pop('template_lookup', TEMPLATE_PATH) tplid = (id(lookup), tpl) if tplid notin TEMPLATES or DEBUG: settings = kwargs.pop('template_settings', {}) ifisinstance(tpl, adapter): TEMPLATES[tplid] = tpl if settings: TEMPLATES[tplid].prepare(**settings) elif"\n"in tpl or"{"in tpl or"%"in tpl or'$'in tpl: TEMPLATES[tplid] = adapter(source=tpl, lookup=lookup, **settings) else: TEMPLATES[tplid] = adapter(name=tpl, lookup=lookup, **settings) ifnot TEMPLATES[tplid]: abort(500, 'Template (%s) not found' % tpl) return TEMPLATES[tplid].render(kwargs)
这个方法的作用就是初始化模板的字符处理逻辑,支持 HTML 转义或直接输出原始 HTML,也就是解析这个传入的template变成html,然后回到template结尾。进入render方法。
1 2 3 4 5 6 7 8 9
defrender(self, *args, **kwargs): """ Render the template using keyword arguments as local variables. """ env = {} stdout = [] for dictarg in args: env.update(dictarg) env.update(kwargs) self.execute(stdout, env) return''.join(stdout)