`

web.py 0.3 新手指南 - RESTful doctesting using app.request

阅读更多
!/usr/bin/env python
"""
RESTful web.py testing

usage: python webapp.py 8080 [--test]

>>> req = app.request('/mathematicians', method='POST')
>>> req.status
'400 Bad Request'

>>> name = {'first': 'Beno\xc3\xaet', 'last': 'Mandelbrot'}
>>> data = urllib.urlencode(name)
>>> req = app.request('/mathematicians', method='POST', data=data)
>>> req.status
'201 Created'
>>> created_path = req.headers['Location']
>>> created_path
'/mathematicians/b-mandelbrot'
>>> fn = '<h1 class=fn>{0} {1}</h1>'.format(name['first'], name['last'])
>>> assert fn in app.request(created_path).data

"""

import doctest
import urllib
import sys

import web


paths = (
  '/mathematicians(/)?', 'Mathematicians',
  '/mathematicians/([a-z])-([a-z]{2,})', 'Mathematician'
)
app = web.application(paths, globals())

dbname = {True: 'test', False: 'production'}[sys.argv[-1] == '--test']
db = {} # db = web.database(..., db='math_{0}'.format(dbname))


class Mathematicians:

  def GET(self, slash=False):
    """list all mathematicians and form to create new one"""
    if slash:
        raise web.seeother('/mathematicians')
    mathematicians = db.items() # db.select(...)
    return web.template.Template("""$def with (mathematicians)
      <!doctype html>
      <html>
      <head>
        <meta charset=utf-8>
        <title>Mathematicians</title>
      </head>
      <body>
        <h1>Mathematicians</h1>
        $if mathematicians:
          <ul class=blogroll>
            $for path, name in mathematicians:
              <li class=vcard><a class="fn url"
              href=/mathematicians/$path>$name.first $name.last</a></li>
          </ul>
        <form action=/mathematicians method=post>
          <label>First <input name=first type=text></label>
          <label>Last <input name=last type=text></label>
          <input type=submit value=Add>
        </form>
      </body>
      </html>""")(mathematicians)

  def POST(self, _):
    """create new mathematician"""
    name = web.input('first', 'last')
    key = '{0}-{1}'.format(name.first[0].lower(), name.last.lower())
    name.first, name.last = name.first.capitalize(), name.last.capitalize()
    db[key] = name # db.insert(...)
    path = '/mathematicians/{0}'.format(key)
    web.ctx.status = '201 Created'
    web.header('Location', path)
    return web.template.Template("""$def with (path, name)
      <!doctype html>
      <html>
      <head>
        <meta charset=utf-8>
        <title>Profile Created</title>
      </head>
      <body>
        <p>Profile created for <a href=$path>$name.first $name.last</a>.</p>
      </body>
      </html>""")(path, name)


class Mathematician:

  def GET(self, first_initial, last_name):
    """display mathematician"""
    key = '{0}-{1}'.format(first_initial, last_name)
    try:
        mathematician = db[key] # db.select(...)
    except KeyError:
        raise web.notfound()
    return web.template.Template("""$def with (name)
      <!doctype html>
      <html>
      <head>
        <meta charset=utf-8>
        <title>$name.first $name.last</title>
      </head>
      <body class=vcard>
        <p><a href=/mathematicians rel=up>Mathematicians</a> &#x25B8;</p>
        <h1 class=fn>$name.first $name.last</h1>
      </body>
      </html>""")(mathematician)


if __name__ == "__main__":
  if sys.argv[-1] == '--test':
    doctest.testmod()
  else:
    app.run()

 

分享到:
评论

相关推荐

    instrumented-restful-fast-request-pro-2023.1.7.1 可用版

    instrumented-restful-fast-request-pro-2023.1.7.1 可用版,亲测,可用。

    Python库 | deteefapi-0.3-py2.py3-none-any.whl

    《Python库deteeffapi-0.3-py2.py3-none-any.whl:深入了解与应用》 在Python的世界里,库是开发者的重要工具,它们提供了丰富的功能,简化了编程工作。今天我们要深入探讨的是一款名为"deteeffapi"的Python库,它...

    Python库 | financeager_flask-0.3.3.0-py3-none-any.whl

    "Python库 | financeager_flask-0.3.3.0-py3-none-any.whl" 是一个专门针对金融领域的Flask应用扩展,版本号为0.3.3.0,适用于Python 3.x环境。Flask是一个轻量级的Web服务程序,它基于Werkzeug WSGI工具包和Jinja2...

    Flask-1.1.1-py2.py3-none-any.whl

    Flask是一个轻量级的Web服务程序,由Pallets团队开发,广泛用于构建RESTful API和服务端应用。这个版本是1.1.1,意味着它是该框架的稳定版本,可能包含了错误修复和性能优化。 描述提到的“该资源为Flask-1.1.1-py2...

    API_callers-0.2.0-py2.py3-none-any.whl.zip

    标题 "API_callers-0.2.0-py2.py3-none-any.whl.zip" 提供的信息表明,这是一个与 API 调用相关的 Python 库的分发包,版本为 0.2.0。".whl" 文件是 Python 的 Wheel 格式,它是 Python 包的一种二进制分发格式,...

    A_Api_Server-1.1.1-py2.py3-none-any.whl.zip

    标题中的"A_Api_Server-1.1.1-py2.py3-none-any.whl.zip"是一个软件包文件,它被设计用于Python环境。这个文件的命名遵循了Python的wheel(whl)格式,这是一种预编译的Python包格式,旨在简化安装过程。"A_Api_...

    PyPI 官网下载 | flasgger-0.9.5-py2.py3-none-any.whl

    《PyPI官网下载:探索Flasgger-0.9.5-py2.py3-none-any.whl》 PyPI(Python Package Index)是Python开发者的重要资源库,它提供了大量预编译的Python软件包,方便用户下载和安装。在Python的世界里,Flasgger是一...

    Python库 | weppy-1.0.7-py2.py3-none-any.whl

    它包含了路由、模板渲染、中间件、表单处理等基本功能,允许开发者快速构建RESTful API或完整的Web应用程序。Weppy的特色在于其模块化的设计,使得开发者可以根据需要选择并组合不同的组件,同时保持代码的清晰和可...

    Python库 | django_nimbus_api-0.11.9-py2.py3-none-any.whl

    **Python库 django_nimbus_api-0.11.9-py2.py3-none-any.whl** `django_nimbus_api` 是一个专为Python设计的后端开发库,它与流行的Web框架Django集成,用于构建API接口。这个库的版本是0.11.9,表明它已经经过了多...

    PyPI 官网下载 | rest_toolkit-0.9-py2.py3-none-any.whl

    **PyPI官网下载 | rest_toolkit-0.9-py2.py3-none-any.whl** 在Python编程领域,PyPI(Python Package Index)是官方的第三方软件包仓库,它为Python开发者提供了一个集中发布和获取Python库的地方。`rest_toolkit-...

    PyPI 官网下载 | styler_rest_framework-1.3.0-py2.py3-none-any.whl

    标题中的"PyPI 官网下载 | styler_rest_framework-1.3.0-py2.py3-none-any.whl"表明这是一个从Python Package Index(PyPI)官方源下载的软件包,名为`styler_rest_framework`,版本为1.3.0。PyPI是Python社区最常用...

    Python库 | falcon_helpers-0.11.4-py2.py3-none-any.whl

    `falcon`是一个轻量级、高性能的Web服务API框架,专门用于构建RESTful API。它以其简洁的API设计和对速度的追求而受到开发者的喜爱。`falcon_helpers`作为`falcon`的辅助库,旨在为`falcon`框架提供额外的功能和便利...

    PyPI 官网下载 | apispec_webframeworks-0.5.1-py2.py3-none-any.whl

    **PyPI 官网下载 | apispec_webframeworks-0.5.1-py2.py3-none-any.whl** PyPI(Python Package Index)是Python社区的官方软件包仓库,它为开发者提供了一个平台,可以发布、分享和安装Python模块。apispec_...

    Python库 | muffin_rest-0.0.31-py2.py3-none-any.whl

    "muffin_rest-0.0.31-py2.py3-none-any.whl" 是一个针对Python开发的库,名为Muffin-REST。这个压缩包文件是Python的wheel格式,它是一种预编译的Python软件包,使得安装过程更为便捷。Python 2和Python 3的兼容性...

    Python库 | pyramid_oereb-1.5.1.dev20190531-py2.py3-none-any.whl

    标题中的"pyramid_oereb-1.5.1.dev20190531-py2.py3-none-any.whl"是一个Python库的发行版本,它属于Python的轮子(wheel)格式,这是一种预编译的Python软件包分发格式。这个版本号(1.5.1.dev20190531)表明它是...

    python-web.py包

    Python的Web.py是一个轻量级、灵活且强大的Web框架,它允许开发者用Python代码直接构建Web应用程序。在尝试使用`pip install web.py`命令安装时,可能会遇到一些问题,这通常与网络连接、库依赖冲突或者版本不兼容...

    PyPI 官网下载 | transmute_core-1.4.1-py2.py3-none-any.whl

    《PyPI官网下载:transmute_core-1.4.1-py2.py3-none-any.whl详解》 PyPI(Python Package Index),是Python开发者最常用的一个资源库,它提供了大量的Python软件包供用户下载和使用。在Python的开发过程中,我们...

    Python库 | oarepo-3.3.59-py2.py3-none-any.whl

    在给定的标题"Python库 | oarepo-3.3.59-py2.py3-none-any.whl"中,我们关注的核心是`oarepo`,这是一个Python库的特定版本——3.3.59。`.whl`文件是Python的二进制分发格式,类似于其他软件平台的安装包,可以直接...

    Python库 | webargs-0.5.0-py2.py3-none-any.whl

    1. **请求解析器**:webargs 提供了多种内置的解析器,如 FlaskParser、DjangoParser 和 RequestParser,可以方便地与各种 web 框架结合使用。它们会自动从请求中提取参数并返回一个解析后的对象。 2. **参数定义**...

    PyPI 官网下载 | django_rest_authemail-1.5.3-py2.py3-none-any.whl

    标题"PyPI 官网下载 | django_rest_authemail-1.5.3-py2.py3-none-any.whl"表明这是一个在Python的包索引平台PyPI上可以找到的软件包,名为`django_rest_authemail`,版本号为1.5.3。这个`.whl`文件是Python的轮子...

Global site tag (gtag.js) - Google Analytics