I’m currently taking a masters class on dynamic languages with DaBeaz, and our first assignment was to parse a basic text file containing a stock portfolio, like so (here’s a simple text file w/ all of them):
YHOO 50 19.25 AAPL 100 143.41 SCOX 500 4.21...
The values are symbol, shares, and price. We had to produce a nicely formatted, text-based table, and calculate the total value. Not so bad, but we had to do it in 9 different languages. I won’t talk about all of them, rather, just the PHP and Python versions. I’ve been working in PHP for quite awhile now, and and learning quite a bit about Python as DaBeaz wrote a book about it.
Simplest is Best
So, there’s all kinds of ways to perform the task at hand. Since you know there are 3 values on each line, you could “explode” each line into an array, based on the space between each value. You could toss in a regular expression or two. But let’s not get too clever, and try to write the most efficient code possible with the least overhead – what if our portfolio contains thousands of lines?
Here’s the Python solution:
total = 0 print "%10s %10s %10s " % ('Names', 'Shares', 'Price') print "---------- " * 3; for line in open('portfolio.txt', 'r'): vals = line.split() symbol = vals shares = int(vals) price = float(vals) print "%10s %10s %10.2f" % (symbol, shares, price) total += shares * price print "\nTotal value : $%0.2f" % total
One thing that’s really winning me over to Python is the syntax – it’s compact and easy to read. Also note that in Python, we can’t create “on the fly” variables via expressions, which is why we had to declare the “total” variable on the first line, whereas PHP is very loosie-goosie:
Also note that with PHP, we don’t have to convert strings to numbers and floats, it just sort of “magically” knows what we want to do by looking at the context (in this case, perform a math calculation). While I found Python’s “strictness” a bit annoying at first, I’ve come to appreciate it’s explicit behavior as it’s less error-prone. (And I say “strictness” because compared to compiled languages like Java, this isn’t strict at all.) But note PHP’s list function – it’s highly useful, and can be used to iterate database queries as well.
Printf (PHP) and % (Python) are Your Friends
Seeing that all of my work is on the web, I never really used these puppies before. But they sure are useful for formatting text and numbers. The syntax can be a little intimidating, but not when you understand it. Let’s look at this statement in PHP:
printf("%10s %10d %10.2f\n", $name, $shares, $price);
Think of “printf” like printing w/ formatting. All this is saying is to take the stuff to the right of the quotes (our variables), and format it like this:
%10s = print a string, right-aligned, within a 10 space field
%10d = print a digit, right-aligned, within a 10 space field
%10.2f = print a floating point number, right-aligned, within a 10 space field, and round it to 2 decimal points.
(And the “\n” is just a line-break command.)
In Python, you just use a regular print statement, with a “%” between the formatted output (always in quotes, as with PHP), and the variables to sub in:
print "%10s %10s %10.2f" % (symbol, shares, price)
(In Python, a new line is automatically inserted after each print statement.)
And there you have it. In the next article, we’ll take this to the next level, and parse real stock data from a (huge) CSV file – whooot!