Python and Whitespace

A friend of mine (who shall remain unnamed) recently blasted Python and the way it uses whitespace, saying that whitespace should not matter in code. He seems to feel that braces are a much better way of denoting a hierarchy, and frankly I think he’s insane for it. Firstly, how many people actually completely leave out whitespace when writing in Java or C# for example? Most IDEs put it in for you, and reading the code would be a nightmare without it. Secondly, have you ever had a complicated series of conditional statements or loops or combination thereof (we’ll give you the benefit of the doubt and assume there wasn’t an easier, better way to write that block) in a language that uses braces? Did you have fun making all the braces balanced, struggling to find where that last one should go? If you did, you may be a masochist, or you may just like the look of braces, and who am I to judge you for that? If you didn’t, give Python a try and enter a blissful world where {} are only used for dictionaries (ok so they may have other uses that I just don’t recall or haven’t learned yet, but that’s not the point). Your code will be just a smidgen easier to read and you’ll never have to balance braces again! Now if only they could do something about parentheses and brackets with lists and tuples…

RegularExpressions + Python

Have you ever needed to parse through large amounts of text looking for a specific pattern? Patterns like “one capital letter followed by three numbers” or “dd/mm/yyyy”? This is known as Pattern Matching. Regular Expressions allow easy syntax for pattern matching, and is an invaluable skill to add to one’s toolkit, no matter what your area of expertise/practice is. Whether you’re writing a Compiler, Form Validator, Text Editor, Django Project, or Language Translator, Regular Expressions will always prove to be invaluable. Here is a very basic overview of some syntax: ‘\d’ represents a digit. ‘\s’ represents whitespace. ‘.’ represents any character. If you have worked with Python for very long, you are probably already familiar with the concept. Take a look at the following code:

1
print(“Rounded = %05d” % (42))

Read the rest of this entry »

QuickRange

We are all familiar with Python’s built-in range() function. Many of us use it all the time, especially in for loops, and especially if you come from a language that uses the

1
for (i=0;i<j;i++){}

syntax, but few of us realize just how inefficient range() is until we try to use it with really large numbers. When you call range(x), the interpreter is actually creating a list of numbers from 0 to x-1. That’s fine for range(10), but what if you have to go to range(1000000000000000)? Go try it, let me know how long it takes to start running your loop. A better solution is to use a generator, as follows:

1
2
3
4
5
6
7
8
9
def quickrange(x):
   i=0
   while i<x:
      yield i
      i+=1
 
for i in quickrange(10):
   print i
#will print 0-9

The benefit of using a generator is that you don’t need to store an arbitrarily long list in memory (which can take up quite a lot of memory if your range is large enough). You also don’t have the delay in starting your loop while Python makes a list of x elements. The sample above is quite simple, of course, and doesn’t fully replicate the abilities of range(), but one could easily add some logic to handle start and (potentially negative) step values.

FizzBuzz in Python

1
2
3
4
5
6
7
8
9
10
11
12
def fizzbuzz():
   for i in range(1,101):
     if not i % 15:
       print "FizzBuzz"
     elif not i % 3:
       print "Fizz"
     elif not i % 5:
       print "Buzz"
     else: 
       print i
if __name__ == '__main__':
   fizzbuzz()

Regular Expressions

I have a dangerous love affair with regular expressions. I am fascinated by them so much that I use them in the most bizarre and inappropriate places (just ask Ken Reitz). I have developed an addiction to regular expressions the likes of which simply cannot be healthy, both for myself and anyone who encounters my code. Maybe I should consider rehab…