[Tutor] Considering translating bash script to Python to learn

Alan Gauld alan.gauld at btinternet.com
Tue May 12 19:01:28 CEST 2009


"Dotan Cohen" <dotancohen at gmail.com> wrote

>I am considering translating a homegrown bash script to Python to
> learn the language. 

Thats rarely a good approach. While you can replace bash 
with Python you will just wind up calling a bunch of external 
programs and thats what shell scripts are best at. The only 
time its worthwhile is where the bash code is structurally 
complex with lots of loops and conditionals and local state.


> rearranges, then sends the whole thing to the printer. However, I seem
> to depend on many bash-specific functions (os programs) and I cannot
> find Python equivalents. Here are some lines of the script, and the
> relevant questions:

The key is what you say here. The core code depends on os 
programs (not bash functions!). Python can probably replace 
some of those os programs but why bother unless you need to 
retire them or are paying money for them?

> gnome-web-print --mode=print --files $FILE /tmp/weeklyCalendar.pdf
> Here, I am converting an HTML file to PDF. I rely on the external
> program gnome-web-print to do the conversion. Does Python have a
> native way to handle this, or would I just wind up calling
> gnome-web-print from within python?

You could write a program to do this in python but it would be 
bigger than your entire bash script replacement.

> pdftops -f 2 -l 2 $HOME/.bin/todo/todo.odf.pdf /tmp/weeklyTodo.ps
> Here I am exporting the second page of a PDF file as a PS file.
> Actually, the file in question is a hybrid ODF-PDF file and if Python
> has a way of exporting a specific page of a ODF file that is fine too.
> I could export to PDF as well, the important bit is too isolate a
> single page of either an ODF or PDF file and have it as either a PS or
> PDF.

Same here although ReportLab (a non standard lib Python module) may 
have some helper functions for converting PDF to ps.

> gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite
> -sOutputFile=/tmp/weeklyCalendarPrintMe.pdf
> $HOME/.bin/todo/weeklyCalendar-blank.pdf /tmp/weeklyCalendar.pdf
> /tmp/weeklyTodo.ps $HOME/.bin/todo/weeklyCalendar-blank.pdf
> Here, I am combining several PDF and PS files as a single PDf file.
> Can Python do this?

Again you probably could since PDF and PS are both text like formats
internally but it would be quite a lot of work.

> lpr -P printer -o number-up=2 /tmp/weeklyCalendarPrintMe.pdf
> Here I am sending the final file to the printer, and having it print 2
> pages on each page. Again, is this something that Python can handle
> internally?

This is so OS and local environment specific that Python 
usually just delegates this to lpr in my experience!

> Thanks for the help. I know that Python is not specifically designed
> to manipulate PDF files, but as the script is getting more complex and
> performs logic in other sections (not shown here) I would like to take
> advantage of other Python properties.

For PDF you should look at ReportLab. It is really designed for 
creating PDFs from non PDF data (eg out of a database or with 
graphics etc). Combined with a html parser such as Beautiful Soup
that aspect may be worth converting to Python.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list