Created
February 1, 2012 00:41
-
-
Save lukecampbell/1714232 to your computer and use it in GitHub Desktop.
An example of populating and viewing couchdb in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- title: Cheap cholesterol | |
content: | | |
What's the point of being free if you're alone? | |
author: | |
name: Luke Campbell | |
email: [email protected] | |
updated: 8:13 PM | |
- title: Water | |
content: | | |
Does anyone want water or saltines? | |
author: | |
name: Luke Campbell | |
email: [email protected] | |
updated: Nowish | |
comments: | |
- author: | |
name: Liz Lemon | |
content: spaghetti | |
- author: | |
name: Jack | |
content: Ghetto Mating Call | |
- title: Leave me alone | |
content: | | |
I for one think that this is a great time | |
this is a cheap high proof brandy. | |
I love miss lemon! | |
author: | |
name: Luke Campbell | |
email: [email protected] | |
comments: | |
- author: | |
name: Colleen | |
content: I love your posts! | |
- title: Jack Donavan | |
author: | |
name: Jackie Boy | |
content: | | |
For some reason, everytime I hear that song I get arroused. | |
comments: | |
- author: | |
name: Luke Campbell | |
content: Why are you posting here? | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import couchdb, time, yaml | |
couch = couchdb.Server() | |
db = couch['dm_datastore'] | |
def load_blogs(file='blogs.yaml'): | |
a = yaml.load(open(file)) | |
ret = [] | |
for post in a: | |
ret.append(Post(**post)) | |
if 'comments' in post: | |
for comment in post['comments']: | |
b = Comment(**comment) | |
ret.append(b) | |
return ret | |
class Author(dict): | |
def __init__(self, name='', email=''): | |
self['name'] = name | |
self['email'] = email | |
def __setattr__(self, key, value): | |
self[key] = value | |
def __getattr__(self, key): | |
return self[key] | |
class Post(dict): | |
def __init__(self, title='', content='', author={}, updated='', **kwargs): | |
self['title'] = title | |
self['content'] = content | |
self['author'] = author | |
self['updated'] = updated | |
self['type_'] = 'BlogPost' | |
def __setattr__(self, key, value): | |
self[key] = value | |
def __getattr__(self, key): | |
return self[key] | |
class Comment(dict): | |
def __init__(self, ref_id='', content='', author={},updated='' , **kwargs): | |
self['ref_id']=ref_id | |
self['content'] = content | |
self['author'] = author | |
self['updated'] = updated | |
self['type_'] = 'BlogComment' | |
def __setattr__(self, key, value): | |
self[key] = value | |
def __getattr__(self, key): | |
return self[key] | |
def wipe(db): | |
for entry in db: | |
db.delete(db[entry]) | |
def populate(db): | |
blog = load_blogs() | |
post = '' | |
for entry in blog: | |
if entry['type_']=='BlogPost': | |
post, _ = db.save(entry) | |
if entry['type_']=='BlogComment': | |
entry['ref_id'] = post | |
db.save(entry) | |
def init_views(db): | |
a = { | |
"_id": "_design/posts", | |
"language": "javascript", | |
"views": { | |
"basic": { | |
"map": "function(doc) {\n\tif(doc.type_ == \"BlogPost\") {\n\t\temit([doc._id,0], doc);\n\t} else if(doc.type_ == \"BlogComment\") {\n\t\temit([doc.ref_id,1], doc);\n\t}\n}" | |
}, | |
"by_author": { | |
"map": "function(doc) { \n\tif (doc.type_ == \"BlogPost\") {\n\t\temit(doc.author.name);\n\t}\n}" | |
} | |
} | |
} | |
if not '_design/posts' in db: | |
db.save(a) | |
wipe(db) | |
init_views(db) | |
populate(db) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment