Choosing a Python Web Framework

If you follow this blog at all, you know that I’m a PHP programmer.  Specifically, I like to work the LAMP stack.  Lately though I’ve been getting the itch to grow my Python skills and learn a new web framework.  After some searching about on the internet, I came up with two Python web frameworks that look promising: Django and Pylons.

Django and Pylons are both Python web frameworks that encourage rapid development of web sites using the Model-View-Controller design pattern.  They both are similar, but also have enough differences to warrant some discussion before choosing one over the other.

From my research and experience, Django is good because it’s easy.  It’s easy to set up a site and the administrative interface is built up for you.  The only issues people have is that it doesn’t allow a great deal of customization, and sometimes Django hides what is going on from you.  To some people this is desirable, to others it isn’t.

People seem to like Pylons because it’s extremely flexible.  For your models you can use SQL Alchemy or some other ORM, while for your template engine you can use Mako or any other kind that you’d like.

Mainly because I like to have flexibility, I think I’ll start out with Pylons.  If you’re interested in learning more about the differences between the two frameworks check out this and this.


Are we really worth 3.5 million?

A news (I use this term loosely) article recently came across TechCrunch about a Google engineer being offered $3.5 million in restricted stock to not leave for Facebook.  Now, I was alway taught to not undervalue myself, and I completely agree with that line of thought.  However, 3.5 million is ridiculous.  How many startups could you fund with that kind of money?  How many lives could be saved through research, aid, or vaccines?   How much good could be done with $3.5 million?

I understand that this may just be a rumor (it is TechCrunch after all), but it brings up a good point.  As programmers we’ve been taught that our skills are irreplaceable, but I don’t believe that.  Yes we have a hard job, and being good at what we do is even harder, but we’re not worth $3.5 million.  $100,000 for a good programmer, sure.  $500,000 for a “rock star”, maybe. But never $3.5 million.

Google is afraid of losing talent, but this seems like a knee-jerk reaction.  There will always be brilliant engineers who want to work for Google, so I say let him/her go.  Put that $3.5 million to use somewhere it can make a difference, not in the pocket of someone who likely already has a ton of cash.  Besides, if this person is leaving for Facebook, they are probably burnt out or looking for a new challenge anyways.  It happens, and no amount of money will change the fact that they are probably going to leave after their contract is up anyways.


Getting Started with Google App Engine [Part 1]

For longest time I’ve done web development exclusively in PHP.  Lately however I’ve been looking for something a bit different to play with.  I already know Python (not well of course, but that’s changing), so I thought I’d look into web app development using that.  The most obvious way to develop web apps with Python is with a framework like Django or Pylons, but I was interested in scalability too.  Actually, I was interested in EASY scalability.  This is where Google App Engine and their WebApp framework steps in.

What is Google App Engine?

Google App Engine (GAE) is a platform for building highly scalable web applications on Google’s infrastructure.  So what does that mean for you?  It means you can use Python or Java to create web sites hosted on Google’s servers.  The main benefit of using GAE is scalability.  Google’s infrastructure is ridiculously huge, and having access to that means basically unlimited scalability.  You also get to access this all for free, and after you reach the free quota limits, you pay only for what you use.  For more information on quotas for the free service, click here.

Getting Started

Now that you’re interested, you probably want to try it out.  Before you can do that though, you need to create a GAE account and download the development environment.  Development environments are available for Windows, Mac, and Linux.  Setting up the environment is fairly straight forward, and all directions from this point forward should work for you regardless of your platform.  More information about setting up the development environment is available here.

A Simple “Hello World”

Keeping with the spirit of programmers everywhere, I’m going to start off with a simple “Hello World” program.  The first thing you need to do is create a directory called helloworld in the GAE directory.  After that, create 2 files in the new directory called and app.yaml.  Add the following to those files.

print 'Content-Type: text/html'
print ''
print 'Hello world!'


application: helloworld
version: 1
runtime: python
api_version: 1
-	url: /.*

So let’s explain things a bit.  The first file, is straight forward.  The first line sets the content type to HTML, prints a new line, and then prints “Hello world!”.  The second file app.yaml, is a little more complicated.  Here’s the breakdown:

  • application – The name of the folder containing the application.
  • version – The version of the app you plan to upload to GAE.  Increment this every time you are going to upload to GAE and it will keep track of your different versions.
  • runtime – This is the language you are writing the app in.  Python is what we’re using, but Java (lowercase) is also an option.
  • api_version – The GAE api version that we are using.  1 is usually a good choice here.
  • handlers – This essentially maps the url path to a python file.  Once you start the web server, by going to any URL you will be routed through the script.  If you wanted http://localhost/hello to go through, the you would change “url” to “/hello”.

Once those files are in place, running the app is easy.  Let’s assume that the helloworld script is in a directory called helloworld within my Google App Engine directory.  With that assumption, you run:

google_appengine/ helloworld/

Next Time…

In the next part of this series, I’ll talk about getting started with submitting forms, storing information in the data store, and using templates.

PHP Programming

PHP Bitwise Operations

A bitwise operation is an operation that works on the individual bits of a number.  Yes, that means the binary(base 2) representation of a number.  These bitwise operations work by exploiting properties of binary numbers to their advantage.  For instance, if the binary representation of a number ends with a 1, it’s odd.

Base 2 Refresher

For those of us who don’t manipulate bits every day (including myself), I thought that a quick refresher on binary representation would be a good idea.  First, let’s create a table.

4 3 2 1 0
0 0 0 0 1

Each column in the table represents a power of 2.  The first column (from the right) represents 20, the second 21, and so on.  Notice that I have a 1 in the row below the 0 column.  That stands for 20, and 20 is equal to 1.  Therefore, the number represented above (00001) is equal to 1.  On to the next table.

4 3 2 1 0
0 0 1 1 1
Now the base-2 number in the bottom row is 00111.  This is equal to 22 + 21 + 20, or 4 + 2 + 1, which is equal to 7.  As you can see, using binary representation is pretty easy.  On to our final example.
4 3 2 1 0
1 1 0 0 1
This example a bit (har, har) trickier.  The base-2 number in the bottom row is 11001, which is  24 + 23 +20(16 + 8 + 1) = 25.  Notice if there are zeros in the columns, we just ignore them completely.
And that’s it!  Base-2 representation of numbers is easy, and critical for every programmer to understand.  Now on to some tricks.

Trick 1 : Odd / Even

One of the most useful bitwise tricks for web developers is for determining if a number is even or odd.

for($i = 0; $i < 10; $i++) {
     if($i & 1) {
          echo "This is odd.";
     } else {
          echo "This is even.";
So how does help your web development?  Alternating rows.  If you deal with data in a tabular format, you nearly always need to alternate row color on a table.  Using the bitwise “AND” operator is a good way to accomplish this.

Trick 2 : Multiplication and Division

Using the left-shift (<<) and right-shift (>>) operators, we can easily divide and multiple by powers of two.  These operators aren’t limited to powers of two, but it makes things easier to understand.
//Example 1
$x = 2;
echo $x &lt;&lt; 1; //4
echo $x &lt;&lt; 2; //8
//Example 2
$x = 16;
echo $x &gt;&gt; 1; //8
echo $x &gt;&gt; 2; //4

In example 1, we left shift the number 2 one place.  By left shifting 1 place, it’s the same as multiplying that number by 2.  By left shifting 2 places, it’s the same multiplying the number by 2 twice.

Example 2 is doing bitwise division.  Right shifting by 1 place is the same as dividing the number by 2.  Right shifting by 2 places, is the same is the same as dividing the number by 2 twice.


There are most definitely a ton of other uses for bitwise operators, however these are the ones I use the most.  If you happen to have a favorite, please let everyone know about it in the comments.

For a more in depth introduction to bitwise operations please check out and