Monthly Archives: October 2012

7 databases – python scripts

I have started reading Seven Databases in Seven Weeks by Eric Redmond and Jim R. Wilson. I am thoroughly enjoying the book. All the “free time” I got outside my start-up job and family (wife & a very active 2-yr old  son), is going into this book.

So far I have read and played with PostgreSQL (Ch. 2) and Riak (Ch. 3). The authors have written the PostgreSQL chapter beautifully. Coming from MySQL background, I could appreciate the differences between the two. I googled around a bit to understand some crucial differences between the two. Postgres does look very appealing with all its rich features! Hopefully some day in the future I will use it for some project.

When I started day 2 of the Riak chapter, I saw the authors had used Ruby and the Riak Ruby client. I thought I should learn Ruby, but decided to postpone that for now. Since I already know Python, I thought why not code up the Ruby scripts in Python. Riak has a beautiful Python client as well. So here is the first Python script hotel.py (equivalent of riak/hotel.rb on page 63):

import riak
import random

STYLES = ['single', 'double', 'queen', 'king', 'suite']
MAX_CAPACITY = 8

client = riak.RiakClient(host='127.0.0.1', port='8091')
bucket = client.bucket('rooms')

for floor in range(1, 101):
  current_rooms_block = floor * 100
  print("Making rooms %d - %d..." % (current_rooms_block, 
    current_rooms_block + 99))
  for room in range(100):
    room_key = current_rooms_block + room
    style = STYLES[random.randrange(len(STYLES))]
    capacity = random.randrange(MAX_CAPACITY) + 1
    ro = bucket.new(str(room_key), data={
           'style': style,
           'capacity': capacity,
         })
    ro.store()
  print "Done"
print "All rooms done"

As I keep reading, I will try to code up the other Ruby scripts in Python.

pymongo import problem

I was trying to install the python mongodb driver today on my 32-bit ubuntu 11.10 laptop. (I am using Python 2.7.)

Since I already have easy_install on my system, I simply used:

$ sudo easy_install bson
$ sudo easy_install pymongo
and they installed fine.

But when I tried to import it in python, I got the following error:
>>> import pymongo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pymongo-2.3-py2.7-linux-i686.egg/pymongo/__init__.py", line 61, in <module>
from pymongo.connection import Connection
File "/usr/local/lib/python2.7/dist-packages/pymongo-2.3-py2.7-linux-i686.egg/pymongo/connection.py", line 44, in <module>
from bson.py3compat import b
ImportError: No module named py3compat

One of the troubles is that pymongo has its own bson:

>>> import bson
/usr/local/lib/python2.7/dist-packages/pytz-2012f-py2.7.egg/pytz/__init__.py:35: UserWarning: Module bson was already imported from /usr/local/lib/python2.7/dist-packages/bson-0.3.3-py2.7.egg/bson/__init__.pyc, but /usr/local/lib/python2.7/dist-packages/pymongo-2.3-py2.7-linux-i686.egg is being added to sys.path
from pkg_resources import resource_stream

Then I saw that the pymongo install site recommends using pip. So I first got pip with
$ sudo easy_install pip

And as the first thing, I uninstalled both bson and pymongo using pip:
$ sudo pip uninstall bson
$ sudo pip uninstall pymongo

Then I installed pymongo using pip
$ sudo pip install pymongo

Now I can import pymongo without any problems!