Skip to main content

Read and write cdb ("constant database") files

Project description

Overview
========

Cdblib contains classes to read and write cdb ("constant database") files.

Cdb files map string keys to string values with very fast record lookups.
Cdblib also allows for in-memory indexes to enable even faster lookups.

See http://cr.yp.to/cdb.html on D. J. Bernstein's site for the original
specification.

Installation
============

Cdblib requires Python 2.5.

The cdblib package contains a C extension module for speedups, but its use
is not required and makes no difference to the interfaces exposed (except of
course for speed).

To install with the C extension module for speed, run::

# python setup.py install

Alternatively, you can simply copy the file cdblib.py to somewhere in your
PYTHONPATH (but this will not include the speedups from the C extension).

Example Usage
=============

Create a new CDB (or update an existing one):

>>> from cdblib import CDB, CDBWriter
>>> wcdb = CDBWriter("presidents.cdb") # Create a new CDB file.
>>> wcdb["Reagan"] = "1981 - 1988"
>>> wcdb["Bush"] = "1989 - 1992"
>>> wcdb["Clinton"] = "1993 - 2000"
>>> wcdb["Bush"] = "2001 - 2008" # Creates a second record with the key "Bush".
>>> wcdb["Bush"] # Return the first record with key "Bush".
'1989 - 1992'
>>> wcdb.getall("Bush") # Return all records with the key "Bush".
['1989 - 1992', '2001 - 2008']
>>> wcdb["Clinton"]
'1993 - 2000'
>>> wcdb.getall("Clinton")
['1993 - 2000']
>>> wcdb["Gore"] # Oops, the CDB doesn't contain the key "Gore".
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "cdblib.py", line 135, in __getitem__
raise KeyError(key)
KeyError: 'Gore'
>>> wcdb.get("Gore", "Bush") # Return "Bush" if the key "Gore" doesn't exist.
'Bush'
>>> wcdb.close() # Close the CDB file and prevent any more writes.

Open an existing CDB read-only:

>>> rcdb = CDB("presidents.cdb") # Re-open the saved CDB file read-only.
>>> rcdb["Clinton"]
'1993 - 2000'
>>> rcdb.close()
>>> rcdb = CDB("presidents.cdb", load_index=True) # Load the CDB index into memory for faster lookups.

Project details


Release history Release notifications | RSS feed

This version

0.5

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdblib-0.5.zip (9.6 kB view details)

Uploaded Source

File details

Details for the file cdblib-0.5.zip.

File metadata

  • Download URL: cdblib-0.5.zip
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cdblib-0.5.zip
Algorithm Hash digest
SHA256 95abd49ce3030dd138e689fec4037fa9a25e8d9bb70e230499c5cf0601dc3e38
MD5 cabafb9b9aa8e1ec1cd5f7fd5211f076
BLAKE2b-256 c9a9470a1f80f6838c143d2b2e195aea040b0084d131b20fdff3099feea0e02e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page