Tag Archives: python

Python object question about __setattr__ and __getattr__.

Okay, I’m making a mock “object-like” object out of a dict. Details should be pretty much irrelevant. The behavior that I want is like this: o = ObjectLike() # Here, we construct and assign arbitrary nested objects of # type … Continue reading

Posted in General | Tagged , , | 2 Comments

Announcing availability of Minimongo, a Micro-ORM for MongoDB in Python

Minimongo is an extremely lightweight, schemaless ORM for Python & MongoDB. Minimongo delegates queries directly to pymongo. Minimongo is schemaless.  (No Schema validation at all.) Minimongo manages database connections for you. Minimongo offers many other conveniences on top of pymongo, but … Continue reading

Posted in General | Tagged , , , , | Leave a comment

Simple pymongo dereference that understands other databases.

Here’s the code for a Python dereference function using pymongo that understands cross-database references properly: import pymongo def dereference(ref): dbname = ref.database collname = ref.collection print ref return pymongo.Connection()[dbname][collname].find_one({‘_id’: ref.id})

Posted in General | Tagged , , , | Leave a comment

Identity mapper & reducer for MongoDB

This code for doing an identity map & identity reduce in MongoDB. Mostly, this is useful for debugging and testing. This code is written in Python, and using the pymongo driver. from pymongo.code import Code connection = pymongo.Connection() # You’ll … Continue reading

Posted in General | Tagged , , , | Leave a comment

Simple script to copy id3 tags from one file to another.

I use lame for batch transcodes of mp3 files, for when I want to put them on a CD-R, or on my phone.  I transcode them down to a much lower bitrate than what I store on my server. But, … Continue reading

Posted in General | Tagged , , , , | Leave a comment

Both production & development settings using zc.buildout & djangorecipe

It took me a while to figure this out, so I’m writing it down. What I wanted was to have my django.wsgi script use the production settings and to have bin/django use the development settings.  Sounds easy, right?  It’s not … Continue reading

Posted in General | Tagged , , , , , | Leave a comment

Python: Unable to find vcvarsall.bat

If you’re running easy_install, pip, zc.buildout, virtualenv, or a similar tool on Windows, and it says: unable to find vcvarsall.bat The issue is that you need a compiler installed on your system.  Many people seem to recommend MinGW, but I’ve … Continue reading

Posted in General | Tagged , , , , , , , | 17 Comments

Django development setup on Windows

Django development on Windows sounds easy, but there are a bunch of steps to get it all working right. Python.  Use Python 2.7 from python.org http://python.org/download/.  I recommend avoiding the 3rd party Python distributions (ActiveState, Enthought, etc.) and sticking with … Continue reading

Posted in General | Tagged , , , , , , | Leave a comment

Python datetime quiz

Without looking at the documentation (or running the code), describe the outputs of the following 4 function calls: from datetime import datetime datetime.datetime.now() datetime.datetime.today() datetime.datetime.now().date() datetime.datetime.today().date() Post your answers as a comment.

Posted in General | Tagged , | 3 Comments

Pitfalls of Python’s ‘is’ operator

I saw this post on Friendfeed, and thought I’d reproduce the code here, because it’s a really interesting Python snippet, and to me, basically says never, ever use the ‘is’ operator.  Here’s the snippet that I just reproduced on Python … Continue reading

Posted in General | Tagged , , , , | 6 Comments