Skip to main content

Interface to BBDB, the Insidious Big Brother Database.

Project description


PyBBDB may sound like a rude noise, but it is actually a Python interface to the Insidious Big Brother Database (BBDB), an address book used with GNU Emacs. You can find out more about BBDB on the Emacs Wiki. The PyBBDB source repo is hosted at Bitbucket. Releases can be found on PyPI.


This module only handles BBDB version 2. There is a version 3 in development on Savannah, but there has been no public release as yet.


To install and run it:
pyparsing, voluptuous and six
To run unit tests:


The usual incantation will install things:

pip install pybbdb


Creating a BBDB database

To create a new database is as simple as you might expect:

>>> from bbdb import BBDB
>>> db = BBDB()

The database starts with no records. To add a new one, use the add_record() method, specifying the first and last names, and any other attributes you want to set:

>>> fred = db.add_record("Fred", "Flintstone")
>>> fred
<Record: Fred Flintstone>

>>> barney = db.add_record("Barney", "Rubble")
>>> db
<BBDB: 2 records>

You can use the returned record object to set other attributes:

>>> fred.set_company("Slate Rock & Gravel")

A Record is a subclass of OrderedDict, so you can set or modify attributes in this style:

>>> fred["company"] = "Slate Rock & Gravel"

As a convenience, there are properties for each of the valid attributes:

>>> fred.firstname

'Slate Rock & Gravel'

There’s also a composite name property:

'Fred Flintstone'

Some BBDB attributes consist of lists of things, and there are add_*() methods for these:

>>> fred.add_net("")
>>> fred.add_net("")
['', '']

>>> fred.add_aka("Freddie")
>>> fred.aka

Telephone records consist of a location tag and a phone number string:

>>> fred.add_phone("Work", "555-6789")
>>> fred.add_phone("Home", "555-1234")
SortedDict([('Home', '555-1234'), ('Work', '555-6789')])

Records can have multiple addresses, each indexed by a location tag. Each address in turn has several attributes:

>>> home = fred.add_address("Home")
>>> home.add_location("Cave 2a", "345 Cavestone Road")
>>> home.set_city("Bedrock")
>>> home.set_state("Hanna Barbera")
>>> home.set_zipcode("12345")
>>> home.set_country("USA")

>>> home
<Address: Cave 2a, 345 Cavestone Road, Bedrock, Hanna Barbera, 12345, USA>

>>> home.location
['Cave 2a', '345 Cavestone Road']

>>> home.zipcode

Finally, each entry can have an arbitrary dictionary of user-defined fields:

>>> fred.add_field("spouse", "Wilma")
>>> fred.add_field("kids", "Pebbles, Bam-Bam")
>>> fred.add_field("catchphrase", '"Yabba dabba doo!"')
>>> fred.fields
SortedDict([('catchphrase', '"Yabba dabba doo!"'), ('kids', 'Pebbles, Bam-Bam'), ('spouse', 'Wilma')])

Field values can also have newlines:

>>> barney.add_field("pets", "brontosaurus\npterodactyl")

Reading and writing BBDB files

The write() method will write the database to a stream (default stdout) in a format suitable for use by GNU Emacs:

>>> db.write()                        # doctest: +ELLIPSIS +REPORT_UDIFF
;; -*-coding: utf-8-emacs;-*-
;;; file-version: 6
;;; user-fields: (catchphrase kids pets spouse)
["Barney" "Rubble" nil nil nil nil nil ((pets . "brontosaurus\npterodactyl")) nil]
["Fred" "Flintstone" ("Freddie") "Slate Rock & Gravel" (["Home" "555-1234"] ...

The convenience write_file() method will put that in a file:

>>> db.write_file("examples/bbdb.el")

You can read a database from file using the fromfile() static method:

>>> newdb = BBDB.fromfile("examples/bbdb.el")
>>> newdb
<BBDB: 2 records>

>>> newdb == db

The read() and read_file() methods of a BBDB database can be used import records from other databases.

Exporting to other formats

Since all BBDB objects are subclasses of OrderedDict, you can easily serialize it to other formats. For example, JSON:

>>> import sys
>>> import json
>>> json.dump(db, sys.stdout, indent=4)  # doctest: +NORMALIZE_WHITESPACE +REPORT_UDIFF
    "coding": "utf-8-emacs",
    "fileversion": 6,
    "records": [
            "firstname": "Barney",
            "lastname": "Rubble",
            "company": "",
            "aka": [],
            "phone": {},
            "address": {},
            "net": [],
            "fields": {
                "pets": "brontosaurus\\npterodactyl"
            "firstname": "Fred",
            "lastname": "Flintstone",
            "company": "Slate Rock & Gravel",
            "aka": [
            "phone": {
                "Home": "555-1234",
                "Work": "555-6789"
            "address": {
                "Home": {
                    "location": [
                        "Cave 2a",
                        "345 Cavestone Road"
                    "city": "Bedrock",
                    "state": "Hanna Barbera",
                    "zipcode": "12345",
                    "country": "USA"
            "net": [
            "fields": {
                "catchphrase": "\"Yabba dabba doo!\"",
                "kids": "Pebbles, Bam-Bam",
                "spouse": "Wilma"

You can create a BBDB database from an appropriately-structured dict using the fromdict method:

>>> serialized = json.dumps(db)
>>> data = json.loads(serialized)
>>> newdb = BBDB.fromdict(data)
>>> newdb == db

Release history

Version 0.4 (10 February 2017)

  • Use pytest for unit tests.
  • Bugfix: add support for newlines in fields.
  • Bugfix: allow last name to be nil.

Version 0.3 (22 July 2015)

  • Bugfix: get things working properly with Python 3.

Version 0.2 (2 July 2015)

  • Add validation of data using voluptuous.
  • Add a bunch of demo converter programs.
  • Add tox test support.
  • Add Python 3 support.
  • Bugfix: convert records from file to correct type.

Version 0.1 (11 June 2015)

  • Initial release.


Report any problems, bugs, etc, to me (Glenn Hutchings) at Patches will also be welcome!

Project details

Download files

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

Files for pybbdb, version 0.4
Filename, size File type Python version Upload date Hashes
Filename, size pybbdb-0.4-py2.py3-none-any.whl (14.4 kB) File type Wheel Python version 2.7 Upload date Hashes View
Filename, size pybbdb-0.4.tar.gz (25.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page