Package couchable :: Module core
[frames] | no frames]

Module core

source code

foo bar

Classes
  UncouchableException
  CouchableDb
Currently, though it is not documented here, the .db parameter is part of the public API of CouchableDb; it is required for use with views, etc.
  CouchableDoc
Base class for types that should be stored as CouchDB documents.
  CouchableAttachment
Base class for types that should be stored as CouchDB attachments.
Functions
 
importstr(module_str, from_=None) source code
 
typestr(type_) source code
 
packer(type_, func_)
This function is still in potential flux.
source code
 
findHandler(cls_or_name, handler_dict) source code
type
registerDocType(type_, preStore_func=<function <lambda> at 0x1015352a8>, postLoad_func=<function <lambda> at 0x101535cf8>)
Returns: The type_ parameter.
source code
 
newid(obj, id_func, noUuid=False, noType=False, sep=':')
Helper function to make document IDs more readable.
source code
byte string
doGzip(data)
Helper function for compressing byte strings.
source code
byte string
doGunzip(data)
Helper function for compressing byte strings.
source code
type
registerAttachmentType(type_, serialize_func=<function <lambda> at 0x10153d1b8>, deserialize_func=<function <lambda> at 0x10153d230>, content_type='application/octet-stream', gzip=False)
Returns: The type_ parameter.
source code
Variables
  FIELD_NAME = 'couchable:'
  __package__ = 'couchable'
Function Details

packer(type_, func_)

source code 

This function is still in potential flux. Writing new (un)packers is delicate; please see the source.

registerDocType(type_, preStore_func=<function <lambda> at 0x1015352a8>, postLoad_func=<function <lambda> at 0x101535cf8>)

source code 
Parameters:
  • type_ (type) - Instances of this type will be stored as top-level CouchDB documents.
  • preStore_func (callable) - A callback of the form lambda obj, cdb: None, called just before storing the object.
  • postLoad_func (callable) - A callback of the form lambda obj, cdb: None, called just after loading the object.
Returns: type
The type_ parameter.

Example: registerDocType(CouchableDoc, lambda obj, cdb: obj.preStore(cdb), lambda obj, cdb: obj.postLoad(cdb))

newid(obj, id_func, noUuid=False, noType=False, sep=':')

source code 

Helper function to make document IDs more readable.

By default, CouchableDb document IDs have the following form:

module.Class:UUID

The intent is that each document ID will be reasonably easy to read and identify at a glance. However, for some document classes, there is a more appropriate way to label each instance. For example, a Person class might want to include first and last name as part of the ID, so that casual examination of the document ID makes it clear which person that ID corresponds to.

newid has no return data; it sets the _id on the object if one is not already present.

Parameters:
  • obj (object) - The object to potentially set an _id on.
  • id_func (callable) - A callable that takes the object and returns a string to include in the _id.
  • noUuid (bool) - A flag that indicates if a UUID should be appended to the ID.
  • noType (bool) - A flag that indicates if type information should be prepended to the ID.
  • sep (string) - The string join the various ID components with. Defaults to ':'.

    Example:

       class ClassA(object):
           def __init__(self, name):
               self.name = name
           # ...
       couchable.registerDocType(ClassA,
               lambda obj, cdb: couchable.newid(obj, lambda x: x.name),
               lambda obj, cdb: None)
    
       couchable.newid(ClassA('foo')) == 'example.ClassA:foo:4094b428-5b45-44fe-bd27-dcb173ec98e8'
    

doGzip(data)

source code 

Helper function for compressing byte strings.

Parameters:
  • data (byte string) - The data to compress.
Returns: byte string
The compressed byte string.

doGunzip(data)

source code 

Helper function for compressing byte strings.

Parameters:
  • data (byte string) - The data to uncompress.
Returns: byte string
The uncompressed byte string.

registerAttachmentType(type_, serialize_func=<function <lambda> at 0x10153d1b8>, deserialize_func=<function <lambda> at 0x10153d230>, content_type='application/octet-stream', gzip=False)

source code 
Parameters:
  • type_ (type) - Instances of this type will be stored as attachments instead of CouchDB documents.
  • serialize_func (callable) - A callback of the form lambda obj: pickle.dumps(obj), called before attaching the object. The callable needs to accept the object and return a byte string.
  • deserialize_func (callable) - A callback of the form lambda data: pickle.loads(data), called after retreiving the attached object. The callable needs to accept a byte string and return the object.
  • content_type (str) - The content type of the attached objected ('application/octet-stream', etc.).
  • gzip (bool) - Indiates if the byte string should be compressed or not.
Returns: type
The type_ parameter.

Example:

   registerAttachmentType(CouchableAttachment,
       lambda obj: CouchableAttachment.pack(obj),
       lambda data: CouchableAttachment.unpack(data),
       'application/octet-stream')