It's getting late so I'll keep this short.
Lately, I've been working a bit with CouchDB and trying to fully grasp the whole schema-free thing. I'm actually fond of Python objects, so I really wanted a way to work with arbitrary database fields as if they were object properties.
Here's my intermediate class, CouchObject.
The example here is particularly interesting because Item is actually two kinds of items: files and folders. They share many fields such as name, directory, and user_id. However, files have some additional fields: content_type and size. By using CouchObject, I can be cool and only store the fields necessary for the particular kind of Item in the CouchDB document.
I could also later add another type of object (really, I only have one right now) and all I'd need to do is define different names for all_fields.
Obviously it's not perfect, but it's 2 AM here and I wanted to share a little about what I've been doing in my free time. Feedback welcome.
P.S. I'm a little scared about using exec and especially without knowing what type of stuff is being passed in the dictionary. Eeek.
Update: Obviously I should not write code very late at night. I'm (*gasp*) actually a morning person. Thanks to everyone who pointed out that yes, exec is horrible when get/setattr would have done just fine. I've updated the code link accordingly. Thanks.
I'm going to publish your comments anyways so I hope that's okay. The quantity of responses is quite scary in itself since I don't track how many people read my blog.
After the whole fuss about rounding numbers, I was considering changing the blog post to make myself look a bit better. I decided against this for several reasons:
1. My blog should reflect me and I do a lot of stupid things. For example, I regularly spill food on myself while eating. You would think that after 26 years of eating food, I'd be able to do that correctly. Obviously not.
2. It wasn't a big change. Really, like 3 lines of code. I had the right idea, even at 2 AM. I also knew I was probably doing something wrong. So yeah, not that bad.
3. It's good to be wrong sometimes. We all make mistakes. So now I'm declaring, yes, I wrote some stupid code and it's okay. It's okay for us all to write stupid code sometimes.*
* My blog was originally titled "Leah Culver's Stupid Blog" before I decided "Leah Culver" was more professional. I think I might change it back.
Update (part 2): I forgot to mention that the couchdb lib I'm using is couchdb-python.