在Python中加载CouchDB设计文档的推荐方法? [英] Recommended approach for loading CouchDB design documents in Python?

查看:87
本文介绍了在Python中加载CouchDB设计文档的推荐方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚起床,但是我想在一个新的Python项目中使用它,我也想使用python编写设计文档(视图)。我已经配置了Couch使用沙发视图服务器,并且可以通过在Futon中输入一些简单的map / reduce函数来确认这项工作。

I'm very new to couch, but I'm trying to use it on a new Python project, and I'd like to use python to write the design documents (views), also. I've already configured Couch to use the couchpy view server, and I can confirm this works by entering some simple map/reduce functions into Futon.

关于使用Python的couchdb模块时如何加载/同步设计文档的任何官方建议?

我了解我可以发布设计文档以将其安装到其中Couch,但是我的问题实际上是关于最佳实践的。在开发环境和生产环境中,我都需要某种部署策略。我的直觉是创建一个目录并将所有设计文档存储在该目录中,然后编写某种同步脚本,将每个脚本上载到沙发上(可能只是盲目地覆盖已经存在的文件)。 这是个好主意吗?

I understand that I can post design documents to "install" them into Couch, but my question is really around best practices. I need some kind of strategy for deploying, both in development environments and in production environments. My intuition is to create a directory and store all of my design documents there, then write some kind of sync script that will upload each one into couch (probably just blindly overwriting what's already there). Is this a good idea?

用Python编写视图仅用5个句子,实际上只是说明了如何安装沙发。在项目的Google代码网站中,提到了一个听起来很不错的couchdb.design模块。可能会有所帮助,但没有文档(我可以找到)。该模块的源代码表明它完成了我感兴趣的大部分操作,但是并没有真正加载文件。我认为我应该进行某种模块发现,但是我听说这不是Python语言。忠告?

The documentation for "Writing views in Python" is 5 sentences, and really just explains how to install couchpy. On the project's google code site, there is mention of a couchdb.design module that sounds like it might help, but there's no documentation (that I can find). The source code for that module indicates that it does most of what I'm interested in, but it stops short of actually loading files. I think I should do some kind of module discovery, but I've heard that's non-Pythonic. Advice?

编辑:

特别是,将我的map / reduce函数存储在字符串文字中的想法似乎完全hacky 。我想在真实的模块中,真实的包中,真实的单元测试中编写真实的python代码。

In particular, the idea of storing my map/reduce functions inside string literals seems completely hacky. I'd like to write real python code, in a real module, in a real package, with real unit tests. Periodically, I'd like to synchronize my "couch views" package with a couchdb instance.

推荐答案

这是一种合理的方法。首先,我将Couchdb.design.ViewDefinition子类化。 (为简便起见,删除了注释和pydocs。)

Here's an approach that seems reasonable. First, I subclass couchdb.design.ViewDefinition. (Comments and pydocs removed for brevity.)

import couchdb.design
import inflection

DESIGN_NAME="version"

class CurrentVersion(couchdb.design.ViewDefinition):
    def __init__(self):

        map_fun = self.__class__.map

        if hasattr(self.__class__, "reduce"):
            reduce_fun = self.__class__.reduce
        else:
            reduce_fun = None

        super_args = (DESIGN_NAME,
                      inflection.underscore(self.__class__.__name__),
                      map_fun,
                      reduce_fun,
                      'python')

        super(CurrentVersion, self).__init__(*super_args)

    @staticmethod
    def map(doc):
        if 'version_key' in doc and 'created_ts' in doc:
            yield (doc['version_key'], [doc['_id'], doc['created_ts']])

    @staticmethod
    def reduce(keys, values, rereduce):
        max_index = 0

        for index, value in enumerate(values):
            if value[1] > values[max_index][1]:
                max_index = index

        return values[max_index]

现在,如果我想同步:

import couchdb.design
from couchview.version import CurrentVersion

db = get_couch_db() # omitted for brevity
couchdb.design.ViewDefinition.sync_many(db, [CurrentVersion()], remove_missing=True)

这种方法的好处是:


  1. 组织。所有设计/视图都分别作为模块/类存在于单个包中。

  2. 真实代码。我的文本编辑器将突出显示语法。我可以针对我的map / reduce函数编写单元测试。

ViewDefinition子类也可以用于查询。

The ViewDefinition subclass can also be used for querying.

current_version_view = couchview.version.CurrentVersion()
result = current_version_view(self.db, key=version_key)

它尚未准备好投入生产,但是与在字符串文字中存储map / reduce函数相比,我认为这还差了一大步。

It's still not ready for production, but I think this is a big step closer compared to storing map/reduce functions inside string literals.

编辑:我最终在该主题上写了几篇博客文章,因为我找不到其他建议来源:

I eventually wrote a couple blog posts on this topic, since I couldn't find any other sources of advice:

  • http://markhaase.com/2012/06/23/couchdb-views-in-python/
  • http://markhaase.com/2012/07/01/unit-tests-for-python-couchdb-views/

这篇关于在Python中加载CouchDB设计文档的推荐方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆