Archive for the 'Computing' Category
Utterly bewitched by this dramatic and thrilling panel from an xkcd.com comic strip, I have decided that it is time I learned the Python programming language.

I come to Python as a bioinformatics scientist who is experienced in C, C++, PHP, Perl, and R. For the past two years, I have used Perl for 95% of the software I have written. But there are days when I grow weary of trying to write object-oriented Perl code in a clear and maintainable way. Perl 5 just isn’t a language designed to support object-oriented programming in a natural way. Hence, I thought I would try Python.
This post is Part 1 of what I plan to be a series of posts about the book Learning Python, 3rd Edition, which was written by Mark Lutz and published in October, 2007, by O’Reilly. This part of my review covers “Part I: Getting Started”, the first 62 pages of the book
In the preface, Mr. Lutz states that Learning Python is intended to be an introduction to the Python programming language for programmers and that it deliberately covers only the core Python language without providing guidance on application programming. Mr. Lutz encourages readers to continue on to Programming Python, 3rd Edition (of which he is also the author).
The preface also contains a three-page section, “Preparing for Python 3.0,” which contains a brain-numbing list of features and changes anticipated in Python 3.0. I believe it would have been more appropriate to place this material in an appendix.
Learning Python is organized into eight major parts:
- Getting Started
- Types and Operations
- Statements and Syntax
- Functions
- Modules
- Classes and OOP (Object Oriented Programming)
- Exceptions and Tools
- Appendixes
The book provides a deliberately bottom-up introduction to Python. This means the reader has to be patient, because Mr. Lutz teaches about each piece of the Python language without showing right away how to use the pieces together to write scripts. Consequently, I have found that I need to jump ahead in the book because I want to begin scripting right away.
Each chapter contains a short quiz, with the answers provided with each quiz. Each part of the book ends with a set of exercises, for which the solutions are provided in Appendix B.
In Chapter 1, Mr. Lutz discusses the strengths and weaknesses of Python. The strengths are many and significant:
- Python is designed to produce readable code, which makes the code easy to maintain and reuse.
- Python is designed up front to support object-oriented programming.
- Python code is more succinct than C++ or Java code.
- Python is portable and runs on Linux/Unix, Mac OS X, and Windows.
- Python comes with a large standard library.
- Python works well with other languages.
- Python is free.
The only weakness that Mr. Lutz acknowledges is that Python sometimes doesn’t run as quickly as compiled C and C++ code. A weakness mentioned by many others is that, contrary to other C-like programming languages (C, C++, Java, C#, Perl, and PHP), white space in Python code has syntactic significance. Many programmers object to this, and it does seem to generate problems with parsing the code.
Chapter 1 devotes a lot of space to discussing the philosphy behind Python.
In philosophy, Python adopts a somewhat minimalist approach. This means that although there are usually multiple ways to accomplish a coding task, there is usually just one obvious way…. In the Python way of thinking, explicit is better than implicit, and simple is better than complex.
This discussion provides a sharp contrast to Perl, for which the motto is, “There is more than one way to do it” (TIMTOWTDI). Perl contains many language constructs that make commands implicit rather than explicit, and this can make Perl code difficult to understand.
Mr. Lutz devotes nearly two pages (pp. 16–17) to a sidebar titled “Python is Engineering, Not Art,” in which the author discusses the relative merits of Python and Perl.
The short story is this: you can do everything in Python that you can in Perl, but you can read your code after you do it…. [author’s italics]
The somewhat longer story reflects the backgrounds of the designers of the two languages…. Python’s creator [Guido van Rossum] is a mathematician by training; as such, he produced a language with a high degree of uniformity—its syntax and toolset are remarkably coherent….
By contrast, the creator of the Perl language [Larry Wall] is a linguist, and its design reflects this heritage. There are many ways to accomplish the same tasks in Perl, and language constructs interact in context-sensitive and sometimes quite subtle ways—much like natural language….
But as anyone who has done any substantial code maintenance should be able to attest, freedom of expression is great for art, but lousy for engineering.
I am not going to delve into this debate. Since I have studied both linguistics and mathematics, I can easily see the strengths and weaknesses of both points of view, and this will help me decide when I should use Perl and when I should use Python.
Much of the material in Chapter 1, although interesting, is in my opinion unnecessary for a learning book. The entire section, “Is Python a ‘Scripting Language’?”, seems to me more appropriate for the Programming Python book. In my opinion, a learning book should get to writing code as quickly as possible, with philosophical meditations saved for another book.
Chapter 2 discusses how the Python interpreter works to run Python scripts. The chapter directs the reader to Appendix A, which contains instructions for obtaining and installing Python for Windows, Linux, or Unix. (Mac OS X 10.5 comes with Python 2.5.1 already installed.) The remainder of the chapter provides background information about the Python interpreter and can be omitted by the reader in a hurry.
Chapter 3 describes how to run Python scripts. It begins with teaching the reader how to start Python from the shell prompt in order to use Python’s interactive command line. The chapter patiently explains how to do this for each of the major operating systems and problems the reader needs to watch for. Then the chapter explains how to write a short Python script and invoke the script from the command line or by double-clicking its icon, and it explains in detail how to overcome the difficulties with double-clicking an icon in Windows. I found these details in Chapter 3 very useful, and it is clear that Mr. Lutz has learned from his training courses what difficulties the novice user will encounter when first using Python.
Chapter 3 continues with a brief introduction on importing modules using import and the differences between import and reload. The chapter concludes with an introduction to the IDLE user interface and other integrated development environments (IDEs).
Part I concludes with a set of very good exercises. The exercises get the reader started with using the Python command line, the IDLE IDE, and the Python documentation available from the command line and from the Python web site.
Resources:
April 05 2008 | Bioinformatics and Computing | Comments Off
Web Database Applications with PHP and MySQL, 2nd Edition, was written by Hugh E. Williams and David Lane and published by O’Reilly in May, 2004.
I purchased this book in 2005 when I was doing some consulting for a microarray company in Massachusetts, where I was adapting the BioArray Software Environment (BASE 1.2), which was written in PHP, to their process. I had set the book aside until recently, when I was motivated to review PHP so I could modify the WordPress theme that I use for this blog.
PHP is the programming language usually referred to by the P in the acronym LAMP, which stands for Linux Apache MySQL PHP. PHP was designed from the beginning to work closely with the Apache httpd web server and with the MySQL database management system. PHP code is easily embedded into HTML, and this makes it easy for relative novices to use a three-tier architecture for their web sites.
I found Web Database Applications with PHP and MySQL an excellent introduction to PHP and MySQL for someone who is skilled at programming using another language. The introductory material on PHP was just enough to get me started, and I quickly learned to refer to the PHP documentation (which is also excellent) when I needed enlightenment. The book also provides several chapters that provide a solid introduction to MySQL 4.1.
The last five chapters of the book are devoted to a complete working example of an online wine store. All source code is available at the authors’ web site, http://www.webdatabasebook.com/. This is an invaluable resource that can serve as the basis for many other PHP projects.
In my opinion, this book is not appropriate for someone who is learning to program or use databases for the first time. Good alternatives might be Learning PHP & MySQL, 2nd Edition, by Michele E. Davis and Jon A. Phillips (published August, 2007, by O’Reilly), and Learning SQL, by Alan Beaulieu (published August, 2005, by O’Reilly). (I read Learning SQL recently, and I recommend it highly. Watch for a review at a later date.)
Since the book was published nearly four years ago, some of the material is dated. In 2004, PHP 5.0 was still in beta; PHP has reached 5.2 by now, and support for PHP 4 is about to end. Similarly, the book recommends using MySQL 4.0 or 4.1, whereas today MySQL 5.0 is very stable. The book provides an excellent set of appendices that explain how to install and configure Apache httpd, MySQL, and PHP for Linux, Windows, and Mac OS X. Again, these instructions are now dated, but they should still provide a useful guide.
I found the index somewhat frustrating to use; some items I was interested in are not found there. These include:
@, the error control operator
-
& and =&, operators used for working with references to variables
- reference, the term referring to a variable that is not passed by value
define, the function used to create constants
- magic constants, such as
__FILE__, __LINE__, __FUNCTION__, __CLASS__, and __METHOD__
I don’t use PHP in my daily work; rather, I use Perl. (Python is the next language I plan to learn, but that’s another story.) I like how PHP has combined arrays and hashes into a single array type. I like PHP’s Boolean values, true and false. And I like how easy it is to embed PHP into HTML; it makes creating a web page dynamically more intuitive than Perl CGI does. But I dislike the inconsistent and ugly naming of many standard functions (for example, strtoupper, the PHP equivalent of Perl’s uc), and this seems to be a common complaint about PHP. I also don’t like the large number of global variables and constants that PHP uses.
There is a BioPHP project (PHP for Bioinformatics), at http://biophp.org/, but it is not as well developed as the BioPerl or BioPython projects. The only large bioinformatics project that I’m aware of that uses PHP is the BioArray Software Environment (BASE) 1.2. Version 2.0 of BASE has been completely rewritten in Java.
March 23 2008 | Bioinformatics and Books and Computing | Comments Off
I hope I won’t write too many posts about the mechanics of blogging, but I have spent the entire day studying Cascading Style Sheets: The Definitive Guide, 2nd Edition, by Eric A. Meyer, in an effort to solve some minor infelicities with the WordPress theme I’ve been using.
The theme used for this site is based on Unlimited 1.0, by Sadish Bala. I licensed the Unlimited 1.0 theme for $20 and then began modifying it, mostly by changing the style sheet and providing a new logo image.
Today I discovered that the theme did not handle font-resizing well. For example, a Firefox user on a Mac OS X computer can use command-+ to increase the size of the fonts on a web page. I find that when my eyes get tired, the easiest way to ease them is to enlarge the fonts on the computer screen.
The Unlimited 1.0 theme was designed to use a static width of 900px. When the user enlarges fonts on a static-width page, the structure of the page begins to break down as the browser attempts to render the larger text in an unchanging region. The solution to this problem is to build flexibility into the web page so that when the user enlarges the font, the page is also enlarged to make more room. The way this is done is to specify widths of div elements using em units instead of px units. One em unit corresponds to the height of the letters of the font, so when the size of the font increases, the length of one em also increases, and as a consequence the width of the div element also increases.
I changed the width of the entire page to 70em, with the left column set to 52em total width and the right column to 18em total width. I also fixed minor problems with the menu and footer so that they would resize themselves, too.
If you care to experiment with a standards-compliant browser such as Firefox 2.0 or Safari 3.0, you will see that the web pages for my blog now resize nicely when you make the fonts larger or smaller.
I am also finicky about compliance with XHTML standards, and I discovered that the Links widget provided with WordPress creates multiple li elements with the same ID when you have more than one category in your blogroll. I replaced that widget with the LinkBlock plugin written by M. Holger. The LinkBlock plugin allows you to create up to nine blogroll-style widgets in your sidebar. I needed this because I have a blogroll, a podcast roll, and a website roll. The LinkBlock plugin handles the IDs correctly so that an XHTML validator does not complain.
Everyone knows that there are three generic font families that can be used in CSS, serif, sans-serif, and monospace. What I learned today is that there are two more generic font families, cursive and fantasy. This is what these look like:
font-family: cursive
Lorem ipsum, dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam…
font-family: fantasy
Lorem ipsum, dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam…
A note: CSS, The Definitive Guide, 3rd Edition, by Eric A. Meyer, was published in November, 2006. My experience with buying O’Reilly books is that as soon as I buy one, a new edition is released one or two months later.
Note added on March 4, 2008:
On my Mac OS X computer, the paragraph styled with the cursive font is displayed using Apple Chancery by both Firefox 2.0.0.12 and Safari 3.0.4. (Apple Chancery is the font I used to create the logo for this site.) The paragraph styled with the fantasy font is displayed using Papyrus.
Under Windows XP, the results are different. Firefox 2.0.0.12 uses Comic Sans MS for cursive and Calibri for fantasy. Internet Explorer 6.0.2900 also uses Comic Sans MS for cursive but Algerian, an all upper-case font, for fantasy.
I expect other variations for browsers under Windows Vista and Linux.
March 03 2008 | Computing | Comments Off