Sunday, December 16, 2012

Very Simple Database in Python

Experimenting with big lookup tables for my weather code lookup server. Instead of using a big configparse file, I want to try a small database.

Python's dbm bindings are included in the default install of Ubuntu. It's light and easy to use.

#!/usr/bin/env python3
import dbm.gnu                # python3-gdbm package
zipcodes = '/tmp/testdb'

# Create a new database with one entry
# Schema: Key is Zipcode
# Value is Observation_Station_Code, Radar_Station_Code, Forecast_Zone
zipc =, 'c')
zipc['53207'] = b'kmke,mkx,wiz066'

# Close and reopen the database
zipd =, 'r')

# List of database keys
keys = zipd.keys()

# Retrieve and print one entry


It works very well and is very fast. It's not easy to view or edit the database with other applications, since it is binary (not text).

No comments: