Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install indicted

How to install indicted

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install indicted
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
Windows (64-bit)
Mac OS X (10.5+)
Linux (32-bit)
0.0.2 Available View build log
Linux (64-bit)
0.0.2 Available View build log
 
License
MIT
Imports
Lastest release
version 0.0.2 on Dec 3rd, 2012

indicted - Indexed Document Class

Summary

Find an list element, which is a dict, by a keys value

Usage

Take for example the following data. This structure is being used to reduce relationships between a parent object and sub objects. This allows us to create indexes that help with relationships rather than making multiple calls to a database. This is just an example structure of a document. This specific structure will benifit heavily from a multi-value index on subdata._id.

```javascript {

System Message: WARNING/2 (<string>, line 20); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 20); backlink

Inline interpreted text or phrase reference start-string without end-string.

System Message: ERROR/3 (<string>, line 22)

Unexpected indentation.

"_id" : ObjectId("4f3eb7cac4f804960a859467"), "subdata" : [

System Message: ERROR/3 (<string>, line 24)

Unexpected indentation.
{
"_id" : ObjectId("4f3eb7cac4f804960a859468"), "bits" : 1234

System Message: WARNING/2 (<string>, line 27)

Definition list ends without a blank line; unexpected unindent.

}, {

System Message: ERROR/3 (<string>, line 29)

Unexpected indentation.
"_id" : ObjectId("4f3eb7cac4f804960a859469"), "bits" : 5678

System Message: WARNING/2 (<string>, line 31)

Block quote ends without a blank line; unexpected unindent.

}

System Message: WARNING/2 (<string>, line 32)

Block quote ends without a blank line; unexpected unindent.

]

System Message: WARNING/2 (<string>, line 33)

Block quote ends without a blank line; unexpected unindent.
}

If we access the subdata key in the root of this document we will be given an array when using standard Dict/List types.

```python [{u'_id': ObjectId('4f3eb7cac4f804960a859468'), u'bits': 1234.0},

System Message: WARNING/2 (<string>, line 39); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 39); backlink

Inline interpreted text or phrase reference start-string without end-string.

System Message: ERROR/3 (<string>, line 41)

Unexpected indentation.
{u'_id': ObjectId('4f3eb7cac4f804960a859469'), u'bits': 5678.0}]

System Message: WARNING/2 (<string>, line 42)

Block quote ends without a blank line; unexpected unindent.

```

System Message: WARNING/2 (<string>, line 42); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 42); backlink

Inline interpreted text or phrase reference start-string without end-string.

Ideally we would want to return the list element that matches a specific _id if the list contains dictionaries that have a key matching that ObjectId. This is where indicted and Inist comes in. InDict is a simple Dict wrapper that uses InList for all list type values. As InList is initialized and modified it keeps an internal dictionary of _id references relating to list positions. An extra function called InList.find is used to pull out a referenced object if it finds one. None is returned otherwise.

```python import pprint import pymongo

System Message: WARNING/2 (<string>, line 52); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 52); backlink

Inline interpreted text or phrase reference start-string without end-string.

from bson import ObjectId

from indicted import InDict

class MongoInDict(InDict):
INDEXCLASS = ObjectId INDEXKEY = '_id'

connection = pymongo.Connection(document_class=MongoInDict) database = connection.test

data = database.data.find_one()

pprint.pprint(data['subdata'].find(ObjectId('4f3eb7cac4f804960a859469'))) pprint.pprint(data['subdata'].find(ObjectId('000000000000000000000000'))) ```

System Message: WARNING/2 (<string>, line 69); backlink

Inline literal start-string without end-string.

System Message: WARNING/2 (<string>, line 69); backlink

Inline interpreted text or phrase reference start-string without end-string.

Yields

`python {u'_id': ObjectId('4f3eb7cac4f804960a859469'), u'bits': 5678.0} None `

There is also indicted.OrderedInDict that inherits collections.OrderedDict. This is incredibly useful for maintaining the document key ordering of your documents.

Status

Currently this module only creates references during the initialization of the list. This will be addressed soon.

Todo

  • Dot notation
  • JSON encoding/decoding/pretty printing helpers

Subscribe to package updates

Last updated Dec 3rd, 2012

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.