Introduction

biblib is a (pure) python library that provides several useful classes, methods and functions to work with BibTeX bibliographic data within your python scripts.

The idea is to enabling you to prepare your own tools, which are specifically tailored to your own literature investigation scheme, comfortably.

Nomenclature

We stay here with the naming convention as used on bibtex.org. The different types of bibliographic resources (like e.g. article or book) are called Entry Types. The reffernce string, which is used to cite a resource like \cite{citation-key}, is called citation-key (aka Bibtexkey). And the various properties an entry can possess, are called Tags (aka Fields or Field Types).

Here an example of a BibTeX entry:

@EntryType{citation-key,
  TagName = {TagContents},
  ...
}

Concept

The basic concept is to use BibDB objects to handle entries. There exist different kinds of BibDB classes for different source/target types like strings, files and more to read from or to write to.

All Entry data (tag values) will be converted to unicode on read. On write it will be either converted to ASCII (with non-ascii charcters encoded in latex notation) or UTF-8.

_images/storebibdb.png

Fig. 1: Schematic sketch showing the basic concept for biblib.

Naturally one can manipulate all properties (tags, aka fields) of an entry object. An entry object can be retrieved from a database, as well as added to this. And of course the database object offers the obvious set of methods to set, get, and delete entries. Additionally, both objects (BibDB, Entry) offer methods which are of interest in the context of bibliographic data, or in particular of BibTeX.

Usage example

Lets assume we have the following BibTeX sample file bibtex.bib:

% This file was created with JabRef 2.10b2.
% Encoding: UTF8

@Article{JCP-127-234509,
  Title                    = {Homogeneous nucleation and growth in supersaturated zinc vapor investigated by molecular dynamics simulation},
  Author                   = {F. R\"{o}mer and T. Kraska},
  Journal                  = {Journal of Chemical Physics},
  Year                     = {2007},
  Number                   = {23},
  Pages                    = {234509},
  Volume                   = {127},
  Doi                      = {10.1063/1.2805063},
}

The following code will read the file into a db, fetch an entry by its DOI and save the db to a new file:

import biblib

# open file as filedb in read only mode
fileDb = biblib.FileBibDB('bibtex.bib', mode='r')

# open file as db read/write mode,
# no LaTeX encoding of unicode character
newFileDb = biblib.FileBibDB('new.bib', encode=False)

# add fileDb entries to newFileDb
newFileDb.merge_bibdb(fileDb)

# access an entry object refered by its cite-key
entry = newFileDb['JCP-127-234509']
entry.get_tag('year')
> 2007

# init doi db
doiDb = biblib.DoiBibDB()

# retrieve bibliographic meta data by DOI
entry = doiDb['10.1088/0959-5309/43/5/301']

# add new entries to database
newFileDb.add_entry(entry)
# or this way to set a specific cite-key
# newFileDb['MY_CITE_KEY'] = entry1

If you don’t use biblib in a bigger context (i.e. from the command line) you can use the helper functions for the fast and easy way:

from biblib import db_from_file, entry_from_doi, db_to_file

db = db_from_file('bibtex.bib')
entry = entry_from_doi('10.1088/0959-5309/43/5/301')
db.add_entry(entry)
db_to_file(db, 'new.bib', encode=False)

Now, new.bib looks like:

% Encoding: UTF8

@Article{Lennard_Jones_1931,
    Author = {J E Lennard-Jones},
    Doi = {10.1088/0959-5309/43/5/301},
    Journal = {Proc. Phys. Soc.},
    Month = {sep},
    Number = {5},
    Pages = {461-482},
    Publisher = {{IOP}Publishing},
    Title = {Cohesion},
    Url = {http://dx.doi.org/10.1088/0959-5309/43/5/301},
    Volume = {43},
    Year = {1931}
}

@Article{JCP-127-234509,
    Author = {F. Römer and T. Kraska},
    Doi = {10.1063/1.2805063},
    Journal = {Journal of Chemical Physics},
    Number = {23},
    Pages = {234509},
    Title = {Homogeneous nucleation and growth in supersaturated zinc vapor investigated by molecular dynamics simulation},
    Volume = {127},
    Year = {2007}
}