linux python ideas
Fernando Pérez
fperez528 at yahoo.com
Wed Sep 18 19:46:37 EDT 2002
Rob Andrews wrote:
> The idea is not "how to program in Python", which would take more than
> one quick presentation, but a one-off demonstration of ways in which
> Python is particularly useful for such a group.
>
> Any suggestions would be appreciated, since my notes are pretty generic
> so far.
Well, here's another one. It's a short script to fetch bibliographic
information from the SPIRES database in BibTex format, from preprints listed
in the XXX Los Alamos server. I use it constantly to update my references
database with zero effort:
$ getbibtex.py NNNNN >> references.bib
and I'm done. Coupled to Pybliographic (also written in python, incidentally)
I have fabulous reference management with perfect coupling to LyX.
It's specific to my needs, but shows how a simple code snippet can fetch very
useful info off the internet, and could easily be modified for other
purposes.
Cheers,
f.
# code follows:
#!/usr/bin/env python
"""Get BibTex references from preprint archives supported by SPIRES.
Usage:
getbibtex preprint1 [preprint2 ....]
The list of preprints can also be fed in from standard input.
Preprint references can be given as:
- name/number, as in hep-lat/0104015.
This sets the archive name to 'name', and subsequent references can be given
only as numbers. That is:
hep-lat/n1 n2 n3 hep-ph/n4 n5
will fetch n1,n2,n3 from hep-lat and n4,n5 from hep-ph.
- numbers only. In this case, a preset default is used (you can change this
value by editing getbibtex.py). The current default is: `%(def_archive)s`.
All archive names supported by SPIRES will work.
Output is given as a list of bibtex references, so you can simply do:
getbibtex .... > mybiblio.bib
and mybiblio.bib will be a valid BibTex file.
"""
__author__ = "Fernando Perez <fperez at pizero.colorado.edu>"
#---------------------------------------------------------------------------
# Global defaults
def_archive = 'hep-lat'
import re,urllib,sys
#---------------------------------------------------------------------------
# Function definitions
def make_url(archive,number):
"""Make a SPIRES search url given an archive name and preprint number.
make_url(archive,number) -> url string."""
return ('http://www.slac.stanford.edu/spires/find/hep/www?'
'eprint=%s&eprint=%s&format=wwwbriefbibtex'
% (archive,number) )
def build_preprints_list():
"""Build the list of (archive,number) pairs from input parameters."""
if len(sys.argv)>1:
input_list = sys.argv[1:]
else:
input_list = sys.stdin.read().splitlines()
pp_list = []
archive = def_archive
for item in input_list:
if '/' in item:
archive,number = item.split('/')
pp_list.append([archive,number])
else:
pp_list.append([archive,item])
return pp_list
#----------------------------------------------------------------------------
# Main code
# give user help if requested with -h... or --h...
try:
if sys.argv[1].startswith('-h') or sys.argv[1].startswith('--h'):
print __doc__ % globals()
sys.exit()
except IndexError:
pass
bibtex_re = re.compile( r'(@Article{.*})',re.DOTALL)
for archive,number in build_preprints_list():
search_page = urllib.urlopen(make_url(archive,number)).read()
try:
print bibtex_re.search(search_page).group(1)
except:
print >> sys.stderr, 'ERROR: Failed to get results
for:',archive,number
More information about the Python-list
mailing list