母网应用程序框架

mother-webapps-framework的Python项目详细描述


母亲是一个用python编写的基于twisted的框架。 它的目的是使web应用程序的开发变得简单和快速。

它提供以下功能:
  • 简单的URL映射到代码,
  • 处理多内容类型(HTML、JSON,…)
  • 与触角orm集成,
  • 允许使用模板工具(目前仅支持mako)

示例:

UUID = '7c772000-8f12-4594-9730-9e4de53d55d1'

from mother          import routing
from mother.callable import callback, Callable
from mother.template import Static, Template

@callback(url=routing.ROOT)
def root(**kwargs):
       return """
               <html>
                       <head>
                               <title>Welcome to Mother Sample Application</title>
                       </head>
                       <body>
                               This is the homepage of <b>Mother Sample Application</b>.<br/>
                               <br>
                               <em>You are welcome to watch my source code to see how to build your first
                               <strong>Mother</strong> application</em>.
                       </body>
               </html>
       """

@callback(url='/foo', content_type='text/plain')
def foo(**kwargs):
       return 'foo:: plain text'

@callback(url='/foo', content_type='application/json')
def foo(**kwargs):
       return ['foo', 'json tree']

@callback(url='/foo', content_type='text/html')
def foo(**kwargs):
       return '<html><body><b>foo</b>:: html content</body></html>'

@callback
def bar(age, **args):
       try:
               age = int(age)
       except:
               return routing.HTTP_404('age MUST be integer')

       return 'the captain is %d years old' % age

class Captain(Callable):
       def __init__(self, **kwargs):
               super(Captain, self).__init__(**kwargs)
               self._age = 54

       # special methods GET, POST, PUT, DELETE are directly mapped to '/sample/captain' url
       def GET(self, **kwargs):
               return 'Captain::GET'

       # You can also expose non-special class/instance methods with the callback modified
       # Here we learn a new @callback option, named 'method'
       #
       @callback
       def age(self, **kwargs):
               return self._age

       # Here we discover a new @callback argument, named 'method'
       # taking either a string among 'GET', 'POST','PUT','DELETE' or a list of these
       #
       # This argument set HTTP method(s) callback is accessible through (default is GET)
       @callback(url='/setage', method='POST')
       def age_post(self, age, **kwargs):
               try:
                       self._age = int(age)
               except:
                       return routing.HTTP_404('age MUST be integer')

               return routing.HTTP_200('')

@callback
def hello(**kwargs):
       return 'hello'

URLS = {
       # expose function
       '/hel-lo'                                                       : hello,
       # expose static content (directory)
       '/static'             : Static('static-content/'),
       # expose template file
       # NOTES
       #   . template files MUST be stored in a templates/ sub-directory
       #   . Mako is the only available template engine at present
       '/template'                                             : Template('sample.html', title='template sample title',
                                      content="""You\'re viewing a template sample page, rendered with <em>Mako</em>""")
}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何创建带有图标的按钮?   java我想实现定制的Android应用程序物理键盘键,我需要修改qwerty吗。kl文件或如何添加新的键盘映射?   java Toast并没有显示在Android应用程序中,但它首先被显示,然后在两天后停止   java如何检查URLConnection是否已处于连接状态?   java无法访问其他类中的方法(Android Studio)   java无法获取访问字符串中字符的方法   使用jdax/dex2jar反编译后的java类   java如何使用Junit获取数据块报告   如何在java/j2ee中以编程方式为新闻网站生成RSS?   java如何使用已声明的事实类型动态重新加载规则?   java单例模式使用   javaq:Mockito使用@Mock和@Autowired   java在Raspberry Pi上将RXTX集成到OSGi包时未解决的需求