Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我们如何在Cheetah中预编译基础模板,以便include、extends和import在Weby中正常工作_Python_Inheritance_Web.py - Fatal编程技术网

Python 我们如何在Cheetah中预编译基础模板,以便include、extends和import在Weby中正常工作

Python 我们如何在Cheetah中预编译基础模板,以便include、extends和import在Weby中正常工作,python,inheritance,web.py,Python,Inheritance,Web.py,你们如何在生产中为猎豹服务 伙计们,你们能分享一下如何在生产中预编译和服务cheetah的设置吗 由于我们不在webpy中编译模板,因此会出现上游超时错误。如果你能分享一个好的最佳实践,那会有所帮助 * 杰里米写道: 对于生产站点,我使用Cheetah 使用预编译的模板-它是 模板导入速度非常快 尤其是当python 编译和优化。一点 使用imp模块的魔法需要一段时间 模板名称和基本目录 在特定于站点的配置中配置 并加载该模板,以 护理 根据需要导入指令。我不使用对的内置支持 然而,猎豹。新模板

你们如何在生产中为猎豹服务

伙计们,你们能分享一下如何在生产中预编译和服务cheetah的设置吗

由于我们不在webpy中编译模板,因此会出现上游超时错误。如果你能分享一个好的最佳实践,那会有所帮助

*

杰里米写道: 对于生产站点,我使用Cheetah 使用预编译的模板-它是 模板导入速度非常快 尤其是当python 编译和优化。一点 使用imp模块的魔法需要一段时间 模板名称和基本目录 在特定于站点的配置中配置 并加载该模板,以 护理

根据需要导入指令。我不使用对的内置支持 然而,猎豹。新模板 库也仅导入到 显示调试错误页面


*可能根据需要自动编译:

import sys
import os
from os import path
import logging
from Cheetah.Template import Template
from Cheetah.Compiler import Compiler

log = logging.getLogger(__name__)

_import_save = __import__
def cheetah_import(name, *args, **kw):
  """Import function which search for Cheetah templates.

  When template ``*.tmpl`` is found in ``sys.path`` matching module
  name (and corresponding generated Python module is outdated or
  not existent) it will be compiled prior to actual import.
  """
  name_parts = name.split('.')
  for p in sys.path:
    basename = path.join(p, *name_parts)
    tmpl_path = basename+'.tmpl'
    py_path = basename+'.py'
    if path.exists(tmpl_path):
      log.debug("%s found in %r", name, tmpl_path)
      if not path.exists(py_path) or newer(tmpl_path, py_path):
        log.info("cheetah compile %r -> %r", tmpl_path, py_path)
        output = Compiler(
            file=tmpl_path,
            moduleName=name,
            mainClassName=name_parts[-1],
            )
        open(py_path, 'wb').write(str(output))
      break
  return _import_save(name, *args, **kw)

def newer(new, old):
    """Whether file with path ``new`` is newer then at ``old``."""
    return os.stat(new).st_mtime > os.stat(old).st_mtime

import __builtin__
__builtin__.__import__ = cheetah_import
这很有效

这就是我对你所做的代码

在给定的方法中包括“不工作”。 这就是我犯的错误

try:web.render('mafbase.tmpl', None, True, 'mafbase')
except:pass
from cheetahimport import *
sys.path.append('./templates')
cheetah_import('mafbase')
    localhost pop]$ vi code.py
    [mark@localhost pop]$ ./code.py 9911
    http://0.0.0.0:9911/
    Traceback (most recent call last):
     File "/home/mark/work/common/web/application.py", line 241, in process
     return self.handle()
    File "/home/mark/work/common/web/application.py", line 232, in handle
    return self._delegate(fn, self.fvars, args)
    File "/home/mark/work/common/web/application.py", line 411, in _delegate
    return handle_class(cls)
    File "/home/mark/work/common/web/application.py", line 386, in handle_class
    return tocall(*args)
    File "user.py", line 264, in proxyfunc
    return func(self, *args, **kw)
    File "/home/mark/work/pop/code.py", line 1801, in GET
    return web.render('subclass.html')
    File "/home/mark/work/common/web/cheetah.py", line 104, in render
    return str(compiled_tmpl)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 982, in __str__
    def __str__(self): return getattr(self, mainMethName)()
    File "templates/mafbase.py", line 713, in respond
    self._handleCheetahInclude("widgetbox.html", trans=trans, includeFrom="file", raw=False)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 1512, in _handleCheetahInclude
    nestedTemplateClass = compiler.compile(source=source,file=file)
    File "/usr/lib/python2.5/site-packages/Cheetah-2.0.1-py2.5-linux-i686.egg/Cheetah/Template.py", line 693, in compile
     fileHash = str(hash(file))+str(os.path.getmtime(file))
   File "/usr/lib/python2.5/posixpath.py", line 143, in getmtime
    return os.stat(filename).st_mtime
   OSError: [Errno 2] No such file or directory: '/home/mark/work/pop/widgetbox.html'