Very Simple Database in Python
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.
Pythons dbm bindings are included in the default install of Ubuntu. Its 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 = dbm.gnu.open(zipcodes, c)
zipc[53207] = bkmke,mkx,wiz066
# Close and reopen the database
zipc.close()
zipd = dbm.gnu.open(zipcodes, r)
# List of database keys
keys = zipd.keys()
# Retrieve and print one entry
print(zipd[keys[0]].decode(utf-8).split(,))
zipd.close()
It works very well and is very fast. Its not easy to view or edit the database with other applications, since it is binary (not text).
download file now