简体   繁体   English

构建cherrypy应用程序有哪些最佳实践?

[英]What are some best practices for structuring cherrypy apps?

I'm writing a cherrypy app and I was wondering what the best way is for structuring my handlers and code for larger applications? 我正在写一个樱桃应用程序,我想知道构建我的处理程序和代码的最佳方法是什么?

I realize assignment is simple trough cherrypy.root, but what are some practices for writing the handlers and assigning them? 我认为通过cherrypy.root进行分配是简单的,但是编写处理程序并分配它们的一些做法是什么?

(Allow me to prove my confusion!) My initial thought is to write a standard handler class that infers a template to run based on the current URL or class/method combination. (请允许我证明我的困惑!)我最初的想法是编写一个标准的处理程序类,根据当前的URL或类/方法组合推断出要运行的模板。 Then I would assign one instance of that handler multiple times to the path to create pages. 然后我会多次将该处理程序的一个实例分配给创建页面的路径。 I don't see this working however as the recursive references wouldn't work quite right. 我不认为这有效,因为递归引用不会正常工作。

So, given the fact that I'm already drawing blanks on how my own source code should look, I'd love some pointers and examples! 所以,鉴于我已经在绘制自己的源代码应该如何看待的事实,我会喜欢一些指针和示例!

Feel free to ask some detailed questions for me to clarify. 随意提出一些详细的问题让我澄清一下。 While there is plenty of cherrypy tutorial material out there, it tends to only scratch the surface. 虽然那里有大量的樱桃教程材料,但它往往只会划伤表面。

CherryPy deliberately doesn't require you to subclass from a framework-provided base class so that you are free to design your own inheritance mechanism, or, more importantly, use none at all. CherryPy故意不要求您从框架提供的基类继承,以便您可以自由设计自己的继承机制,或者更重要的是,根本不使用任何基类。 You are certainly free to define your own base class and inherit from it; 您当然可以自由定义自己的基类并继承它; in this way, you can standardize handler construction and configuration via the __init__ method of your class, and via class-level variables and methods. 通过这种方式,您可以通过类的__init__方法以及类级变量和方法来标准化处理程序构造和配置。

However, the preferred approach is different. 但是,首选方法是不同的。 For most web applications, you don't really want to vary the actual construction logic of your handlers, nor do you care much about class-level variables or methods; 对于大多数Web应用程序,您实际上并不想改变处理程序的实际构造逻辑,也不关心类级变量或方法; instead, you want reusable variables and methods per URI or per subtree of URI's or per site, not per class. 相反,您需要每个URI或URI或每个站点的每个子树的可重用变量和方法,而不是每个类。 You tend to vary one set of handlers from another set more by instance configuration (handler metadata) and instance methods (handler logic). 您倾向于通过实例配置(处理程序元数据)和实例方法(处理程序逻辑)更改另一组处理程序中的一组处理程序。 Traditional class-based inheritance can do this, but it's a bit of a blunt instrument for that kind of customization. 传统的基于类的继承可以做到这一点,但对于这种自定义来说,这是一种钝器。

CherryPy, therefore, is designed to provide this sort of per-resource-set customization that class-based inheritance doesn't do well. 因此,CherryPy旨在提供这种基于类的继承不能很好地进行的每资源集定制。 It provides this through 1) the design of its configuration system, which allows you to bind metadata to a single URI, a subtree of URI's, a subtree of handlers, or a whole site with the same syntax (see http://docs.cherrypy.org/dev/intro/concepts/config.html for an overview), and 2) the hooks and tools system, which allows you to bind logic to a single URI, a subtree of URI's, a subtree of handlers, or a whole site. 它通过1)配置系统的设计提供了这一点,它允许您将元数据绑定到单个URI,URI的子树,处理程序的子树或具有相同语法的整个站点(请参阅http:// docs。 cherrypy.org/dev/intro/concepts/config.html概述),以及2)钩子和工具系统,它允许您将逻辑绑定到单个URI,URI的子树,处理程序的子树或者整个网站。 See http://docs.cherrypy.org/dev/intro/concepts/tools.html 请参见http://docs.cherrypy.org/dev/intro/concepts/tools.html

So, practically: do use normal attributes on cherrypy.root to build your tree of handlers: 所以,实际上:在cherrypy.root上使用普通属性来构建处理程序树:

def make_app():
    root = Root()
    root.foo = Foo()
    root.bars = BarCollection()
    return root

However, don't make Root, Foo and Bar inherit from a common base class. 但是,不要让Root,Foo和Bar从公共基类继承。 Instead, write independent Tools to do things like "infer templates". 相反,编写独立的工具来执行“推断模板”之类的操作。 That is, instead of: 也就是说,而不是:

from cherrypy import expose

class Foo(MyAppBase):
    @expose()
    def index(self, a, b, c):
        ...

root.foo = Foo(template='foo.html')

write: 写:

from cherrypy import expose, tools

class Foo(object):
    @tools.render(template='foo.html')
    @expose()
    def index(self, a, b, c):
        ...

root.foo = Foo()

...where 'tools.render' is a CherryPy Tool you have written to look up and apply the given template. ...'tools.render'是您编写的CherryPy工具,用于查找并应用给定的模板。 This approach will allow you to override the arguments to the Tool in your config file and avoid having to repackage or patch your code: 此方法将允许您覆盖配置文件中工具的参数,并避免重新打包或修补代码:

[/foo/]
tools.render.template = 'foo2.html'

This question is wildly subjective - but I'll give it a shot. 这个问题非常主观 - 但我会试一试。

  • First of all, always keep database and data code separate to the web code. 首先,始终将数据库和数据代码与Web代码分开。 What I do is have lots of small files with one class each in a DB/ folder which are all joined together into a Base.py file, eg: 我所做的是有很多小文件,每个文件在DB/文件夹中都有一个类,它们都连接在一起成为一个Base.py文件,例如:

    \nWeb/ 网络/\nBase.py - The main "base" class, which includes the classes in other Base.py  - 主“基”类,包括其他类 \nweb files, starts the web server in __init__ Web文件,在__init__启动Web服务器\nUsers.py - The class which includes methods generally from "DB/Users.py" Users.py  - 包含通常来自“DB / Users.py”的方法的类 \nwhich checks permissions etc before returning (you may 在返回之前检查权限等(你可以 \nwish to add DB-level security later though) 希望稍后添加DB级安全性)\n... ...\nDB/ D B/\nBase.py - The main base DB class, includes the other DB classes. Base.py  - 主基础DB类,包括其他DB类。 Creates 创建 \nnew SQLAlchemy/whatever instances and create database schemas if 新的SQLAlchemy /任何实例和创建数据库模式if \nthey don't etc. May pay to have database-wide methods 他们没有等。可能需要支付数据库范围的方法\nhere to keep creating connections etc in one place if you 如果你在这里继续在一个地方创建连接等 \ndecide to change databases later 决定稍后更改数据库 \nUsers.py - The user/password etc DB storage class file Users.py  - 用户/密码等DB存储类文件\n... ...\nTemplates/ 模板/\n(HTML templates go here) (HTML模板在这里)\nStatic/ 静态的/\n(Static images/CSS/javscript etc go here) (静态图像/ CSS / javscript等到这里)\n
    Don't forget the __init__.py in each module directory of course so python can find the modules in subdirectories 当然,不要忘记每个模块目录中的__init__.py ,这样python就可以在子目录中找到模块

  • It doesn't always matter what methods you use for structuring code in my opinion, but be consistent. 在我看来,用于构造代码的方法并不总是重要,但要保持一致。 I write a document up with all my conventions with my justifications for using them and try to follow them up to the point it makes sense to, but as always a foolish consistency is the hobgoblin of small minds , as quoting the python style docs :-) 我用我的所有惯例编写了一份文件,并提出了使用它们的理由,并尝试将它们跟进到它有意义的地步,但一如既往a foolish consistency is the hobgoblin of small minds ,引用python风格的文档 : - )

  • Try to use classes rather than straight functions. 尝试使用类而不是直接函数。 It mightn't matter for small projects, but for anything non-trivial things can become difficult. 对于小型项目来说可能并不重要,但对于任何重要的事情都可能变得困难。 It's my preference to have many files with a specific purpose and only a single class in a single file except where it makes sense to have multiple 我喜欢有许多具有特定用途的文件,并且在一个文件中只有一个类,除非有多个文件有意义

  • This one is probably controversial - I usually name my classes Class and just reference it by the module name. 这可能是有争议的 - 我通常将我的类命名为Class并仅通过模块名称引用它。 I'll give an example of Base.py: 我将举一个Base.py的例子:
     import Users class Base(Users.Class):  def __init__ (self):  Users.Class. __init__ (self) 
    This helps to reduce problems when other modules reference each other when importing, as from Users import Users will conflict if Users.py has from Base import x so I always reference by module name. 这有助于减少导入时其他模块相互引用时出现的问题,因为如果Users.py具有from Base import x ,则from Users import Users将发生冲突,因此我始终按模块名称引用。 This is just a personal preference though, so do what you want :-P 这只是个人偏好,所以你想做什么:-P

Hopefully you should get an idea from this post though. 希望你能从这篇文章中得到一个想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 构建 FastAPI 项目的最佳实践是什么? - What are the best practices for structuring a FastAPI project? 显示断言iOS元素时有哪些最佳实践? - What Are Some Best Practices When Asserting iOS Elements Are Displayed? 不确定如何设计django应用程序 - 最佳实践 - Uncertain how to design django apps - best practices jax 混淆 - 最佳实践是什么? - jax confusion - what are the best practices? 确保Django问题滚动重启的最佳实践是什么? - What are some of the best practices to do rolling restarts for Django servers to ensure minimum problems? 减少与 ml 相关的 docker 映像大小的一些最佳实践是什么? - What are some of the best practices to reduce the size of ml-related docker image? 使用非 python 文件构建 Python 项目的最佳实践,例如 DLL、.Z4A8A08F09D337B737F5364 - Best practices for structuring Python project with non python files e.g. DLL, .c, .cpp? 构建我的django第三方应用程序的最佳方法是什么 - What is best way to structuring my django third party app 在Django项目中构造模板的最佳实践是什么? - What is the best practice in structuring templates in your Django project? 调用外部api的最佳做法是什么? - What are the best practices for calling an external api?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM