incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexander Shorin <kxe...@gmail.com>
Subject Re: writing show/list functions in python
Date Fri, 14 Dec 2012 06:35:30 GMT
For object-like design document definition, see issue 186:
http://code.google.com/p/couchdb-python/issues/detail?id=186

and here we go:

class MyDDoc(Document):
   @ViewFunction.map('foo')
   def by_bar(doc):
        for bar in doc['foo']:
            yield bar, None
    by_bar.reduce('_count') # use built-in reduce

   @ShowFunction.define('foo')
   def bar(doc, req):
        return {'json': doc['foo']}

   @ValidateDocUpdateFunction.define('foo')
   def vdu(newdoc, olddoc, userctx, secobj):
       assert 'foo' in newdoc, 'foo missed'

sync_docs(db, [MyDDoc]) # creates and stores _design/foo ddoc

But it will be useless without query server that support of all these functions.

--
,,,^..^,,,


On Fri, Dec 14, 2012 at 2:31 AM, Holger Krekel <holger.krekel@gmail.com> wrote:
> Thanks for the info, Alexander.
>
> On a sidenote, instead of deep directory structures, i am using this style
> of declaring my map/reduce functions in Python:
>
>     @couch.view("access")
>     class packagenames:
>         @staticmethod
>         def map(doc):
>             return doc["_id"]
>         @staticmethod
>         def reduce(keys, values, rereduce):
>             return 1
>
> The decorator just sets an attribute and returns the unmodified class.
> Later i scan the module, do a little bit of introspection and push the
> functions to _design/access/_view/packagenames.  The @staticmethod is
> purely there to allow easy testing (just call packagenames.map(doc) etc. on
> an example doc.  If there is interest, i can see to release that code
> sometime.
>
> cheers,
> holger
> --
> http://holgerkrekel.net
>
>
>
> On Thu, Dec 13, 2012 at 6:24 PM, Alexander Shorin <kxepal@gmail.com> wrote:
>
>> Hi Holger!
>>
>> Take a look on:
>> http://code.google.com/p/couchdb-python/issues/detail?id=146
>> it contains up-to-dated query server with support of all design
>> function in Python. With latest patch you'll find short rst guide
>> about how to start writing Python ddocs and what their differences
>> from Javascript ones.
>> --
>> ,,,^..^,,,
>>
>>
>> On Thu, Dec 13, 2012 at 6:59 PM, Holger Krekel <holger.krekel@gmail.com>
>> wrote:
>> > Besides view functions, is it possible to write show/list functions in
>> > python?
>> >
>> > I saw and old issue and patch about this at
>> > http://code.google.com/p/couchdb-python/issues/detail?id=78
>> >
>> > But not anything in the docs.  If it's not available is this planned?
>>

Mime
View raw message