English 中文(简体)
在Python装入CouchDB设计书的建议方法?
原标题:Recommended approach for loading CouchDB design documents in Python?

我非常新到沙发上,但我正在尝试在一个新的 Python 工程中使用它,我也喜欢使用 Python 来写设计文件( 视图 ) 。 我已经配置了库奇来使用沙发视图服务器, 我可以在 Futon 输入一些简单的地图/ 减少功能来确认这项工作 。

是否有关于使用 Python s shaddb 模块时如何装入/同步设计文件的正式建议?

我理解,我可以把设计文件张贴到库奇“安装”它们,但我的问题其实是围绕最佳做法。我需要某种战略来部署,无论是在发展环境和生产环境中。我的直觉是创建一个目录,将我的所有设计文件储存在那里,然后写出某种同步脚本,将每个文件上传到沙发上(也许只是盲目地覆盖了已经存在的东西 ) 。 这是个好主意吗?

http://packages.python.org/CouchDB/views.html/CouchDB/views.html” rel=“noreferrer” Rel=“norefererr” 中的文字视图是五个句子, 并真正解释了如何安装沙发。 在 < a href=> 上, http:// code.google.com/ p/couchdb-python/" rel=“ noreferrer” > 项目 s google 代码站点 , 提到了一个听起来可能有用的模块, 但没有任何文件( 我能找到) 。 该模块的源代码表明它做了我感兴趣的大部分, 但是它停止了实际装入文件的短处 。 我想我应该做某种模块发现, 但我听说过非 Pythonic. 咨询?

编辑 :

特别是, 将我的地图/ 减少功能存储在字符串字典中的想法似乎 < em> 完全黑客 < / em> 。 我想在一个真正的模块中, 在真实的软件包中, 在真实的单元测试中, 写入真正的 python 代码 。 定期, 我喜欢将我的“ 库视图” 软件包与一个沙发实例同步 。

最佳回答

这里的处理方法似乎是合理的。 首先, 我从子类 shaghdb. design. ViewDescription. (评论和音响因简洁被删除 。)

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. Organization. All designs/views exist as modules/classes (respectively) located in a single package.
  2. Real code. My text editor will highlight syntax. I can write unit tests against my map/reduce functions.

视图定义子类也可以用于查询 。

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

与在字符串字典中存储地图/减少功能相比,这距离更近了一大步。

我最后写了几篇关于这个议题的博客文章,

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签