亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python Web框架Pylons中使用MongoDB的例子

 更新時(shí)間:2013年12月03日 16:51:05   作者:  
這篇文章主要介紹了Python Web框架Pylons中使用MongoDB 的例子,大家參考使用

Pylons 經(jīng)過漫長的開發(fā),終于放出了 1.0 版本。對(duì)于正規(guī)的產(chǎn)品開發(fā)來說,1.0 版本的意義很大,這表明 Pylons 的 API 終于穩(wěn)定下來了。

Pylons 雖是山寨 Rails 而生,但作為一個(gè)純 Python 的 Web 框架,它有一個(gè)鮮明的特點(diǎn):可定制性強(qiáng)??蚣苊恳粚佣紱]重新發(fā)明輪子,而是盡量整合現(xiàn)有的 Python 庫。在 MVC 的 Model 層,Pylons 默認(rèn)支持 SQLAlchemy?,F(xiàn)在 NoSQL 很火 MongoDB 很熱。在 Pylons 中應(yīng)用 MongoDB 也很簡單。下面是一個(gè)簡單的示例。

在 PROJECT/model/__init__.py 中定義 MongoDB 初始化函數(shù)和映射對(duì)象:

復(fù)制代碼 代碼如下:

from ming import Session

from ming import schema
from ming.orm import MappedClass
from ming.orm import FieldProperty, ForeignIdProperty, RelationProperty
from ming.orm import ThreadLocalORMSession

session = None

def init_single_model(model_class):
    model_class.__mongometa__.session = session

class Page(MappedClass):
    class __mongometa__:
        session = session
        name = 'pages'

    _id = FieldProperty(schema.ObjectId)
    title = FieldProperty(str)
    content = FieldProperty(str)

def init_model(engine):
    global session
    session = ThreadLocalORMSession(doc_session=Session(engine))
    init_single_model(Page)
    MappedClass.compile_all()

在 PROJECT/config/environment.py 中進(jìn)行初始化:

復(fù)制代碼 代碼如下:

from ..model import init_model
from ming.datastore import DataStore

def load_environment(global_conf, app_conf):

    ...

    # Create the Mako TemplateLookup, with the default auto-escaping
    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=paths['templates'],
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8', default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    # Setup the mongodb database engine
    init_model(DataStore(config['database.uri']))

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)

    return config

最后在 development.ini 中加入 MongoDB 的配置項(xiàng):

復(fù)制代碼 代碼如下:

[app:main]
database.uri = mongodb://localhost:27017/test

如果需要在程序安裝時(shí)初始化一些數(shù)據(jù), 可以在 PROJECT/websetup.py 中加入

復(fù)制代碼 代碼如下:

"""Setup the wukong application"""
import logging

import pylons.test

from .config.environment import load_environment
from . import model

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup wukong here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

        log.info("Adding demo data.")
        page = model.Page(title='demo', content='This is for demo.')
        model.session.flush()
        log.info("Successfully set up.")

這里使用了 Ming 庫來連接 MongoDB 并做簡單的 ORM。Ming 庫是對(duì) PyMongo 的 ORM 包裝庫。它是 SourceForge 用 TurboGears 和 MongoDB 對(duì)網(wǎng)站進(jìn)行重構(gòu)的副產(chǎn)物。使用起來有點(diǎn)象 SQLAlchemy ORM 。在上面的示例中,也可以把 Ming 替換成 MongoKit 或其它 MongoDB 的 ORM 庫,甚至直接用 PyMongo 也無不可。
有種感覺,MongoDB 會(huì)火。

相關(guān)文章

最新評(píng)論