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ì)象:
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)行初始化:
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):
[app:main]
database.uri = mongodb://localhost:27017/test
如果需要在程序安裝時(shí)初始化一些數(shù)據(jù), 可以在 PROJECT/websetup.py 中加入
"""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ì)火。
- golang基于websocket實(shí)現(xiàn)的簡易聊天室程序
- golang網(wǎng)絡(luò)socket粘包問題的解決方法
- 剖析Go編寫的Socket服務(wù)器模塊解耦及基礎(chǔ)模塊的設(shè)計(jì)
- Go語言基于Socket編寫服務(wù)器端與客戶端通信的實(shí)例
- Go語言實(shí)現(xiàn)socket實(shí)例
- 利用Go語言初步搭建一個(gè)web應(yīng)用的教程
- Go語言實(shí)現(xiàn)的web爬蟲實(shí)例
- Go語言實(shí)現(xiàn)簡單的一個(gè)靜態(tài)WEB服務(wù)器
- go的websocket實(shí)現(xiàn)原理與用法詳解
相關(guān)文章
Python3 串口接收與發(fā)送16進(jìn)制數(shù)據(jù)包的實(shí)例
今天小編就為大家分享一篇Python3 串口接收與發(fā)送16進(jìn)制數(shù)據(jù)包的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06python輕松辦公將100個(gè)Excel中符合條件的數(shù)據(jù)匯總到1個(gè)Excel里
這篇文章主要為大家介紹了python輕松辦公將100個(gè)Excel中符合條件的數(shù)據(jù)匯總到1個(gè)Excel里示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03使用numpy實(shí)現(xiàn)topk函數(shù)操作(并排序)
這篇文章主要介紹了使用numpy實(shí)現(xiàn)topk函數(shù)操作(并排序),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Gradio機(jī)器學(xué)習(xí)模型快速部署工具quickstart
這篇文章主要為大家介紹了Gradio機(jī)器學(xué)習(xí)模型快速部署工具quickstart,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04使用Python構(gòu)造hive insert語句說明
這篇文章主要介紹了使用Python構(gòu)造hive insert語句說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06跟老齊學(xué)Python之關(guān)于類的初步認(rèn)識(shí)
這篇文章主要介紹了Python中關(guān)于類的一些術(shù)語解釋,雖然有些枯燥,但是要了解類的話,這些內(nèi)容是必須的2014-10-10使用Python的pencolor函數(shù)實(shí)現(xiàn)漸變色功能
這篇文章主要介紹了使用Python的pencolor函數(shù)實(shí)現(xiàn)漸變色功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03