[Tutor] Considering translating bash script to Python to learn

Dotan Cohen dotancohen at gmail.com
Tue May 12 21:57:21 CEST 2009

> 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.

That is why is has been a bash script until now.

> The only time its worthwhile is where the bash code is
> structurally complex with lots of loops and conditionals and local state.

That's where it is going, I am starting to have date-dependant
functions and such. The bash is getting messy.

>> 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?

Because the direction of the script is getting complex. But I still
have my reservations.

>> 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.

I see!

>> 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.

Thanks, I did see that mentioned today while googling.

>> 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.

The intention was to have a library function to use, not to code a
parser myself.

>> 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!

That is logical and what I expected to hear.

>> 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.


Dotan Cohen


More information about the Tutor mailing list