Tag: Python
Python as a PHP replacement?
by Seth on Nov.30, 2009, under Python, Web Development
I recently sat down to coffee with a new acquaintance of mine who spends much of his time implementing F/OSS projects at non-profit organizations, and who had just stepped into a lead web developer position using PHP. After sharing pleasantries we began trading stories and talking about each of our “tools of the trade.” When I mentioned that I used to do most of my web development in PHP, but have spent the past year or so trying to move as completely as possible to Python, his response was: “Huh, I have never really thought of Python as a PHP replacement.”
Now, this guy hasn’t exactly been living under a rock for the past 10 years—his resume was quite impressive and included projects in a number of different programming languages; But as you can imagine, I was rather surprised by his response, and it made me wonder: Has the Python community really been that bad at promoting the strengths of Python for web development? Or, does the nirvana experienced by switching from a language like PHP to Python just make us so at peace with the world that we forget the hordes of developers still stuck with C-style syntax? Either way, it got me thinking about a few of the reasons why I decided to switch from PHP to Python; and why I not only see Python as an excellent PHP replacement, but am surprised it is such a “best-kept secret” for web development.
There are already plenty of pages out there discussing Python vs. PHP as a language, so I probably won’t get too technical here. I also want to try avoid turning this into a “Python is better than PHP because…” rant (for more on that, please see the end of this post), so I will simply share with you a few of the main reasons why I decided to replace PHP with Python as my primary language for web development:
Reorganizing data with list & dict comprehensions
by Seth on Oct.30, 2009, under Python
While writing scripts, I frequently run into the issue of needing to re-arrange sets of data into a more “process friendly” format. A common issue I encounter is needing to turn a list (array) into a dictionary (associative array) or vice versa. More often than not, I find myself needing to be able to access list elements by a key, but since they aren’t setup in a dictionary I have to pull out a looping technique to reorganize the data for this to be possible.
Take the following set of data for example:
[[1, 'John Smith', 'admin'], [2, 'Jane Doe', 'superuser'], [3, 'Sam Jones', 'user']]
What we have here is a few rows of user data. In this example, the data is in a Python list (which in PHP would be an array).
In PHP, this would look something like (using print_r):
Array ( [0] => Array ( [0] => 1 [1] => John Smith [2] => admin ) [1] => Array ( [0] => 2 [1] => Jane Doe # (...etc...)
So, what if I found myself writing some code that needed to be able to access each record by its first value, which in this case would be the user_id? (continue reading…)
The great web technology shootout – Round 1: A quick glance at the landscape
by Seth on Sep.12, 2009, under F/OSS, Web Development
[Update: Some of the results here ended up being less than accurate. Please see Round 3 for an explanation (and updated tests).]
Recently I went on a benchmarking spree and decided to throw ApacheBench at a bunch of the different web development technology platforms I interact with on a day-to-day basis. The results were interesting enough to me that I decided I’d take a post to share them here.
Disclaimer: The following test results should be taken with a massive grain of salt. If you know anything about benchmarking, you will know that the slightest adjustments have the potential to change things drastically. While I have tried to perform each test as fairly and accurately as possible, it would be foolish to consider these results as scientific in any way. It should also be noted that my goal here was not to see how fast each technology performs at its most optimized configuration, but rather what a minimal out-of-the-box experience looks like.
Test platform info:
- The hardware was an Intel Core2Quad Q9300, 2.5Ghz, 6MB Cache, 1333FSB, 2GB DDR RAM.
- The OS was CentOS v5.3 32-bit with a standard Apache Webserver setup.
- ApacheBench was used with only the -n and -c flags (1000 requests for the PHP frameworks, 5000 requests for everything else).
- Each ApacheBench test was run 5-10 times, with the “optimum average” chosen as the numbers represented here.
- The PHP tests were done using the standard Apache PHP module.
- The mod_wsgi tests were done in daemon mode set to 2 processes/15 threads.
- The SQL tests were done with mysqli ($mysql->query()) on PHP, and SQLAlchemy (conn.execute()) on Python fetching and printing 5 rows of data from a sample database.
Apache v2.2.3
We will start with the raw Apache benchmark.
For this test, Apache loaded a simple HTML file with random text:
Document Length: 6537 bytes Requests per second: 8356.23 [#/sec] (mean)
As expected, Apache is lightning fast.
Ok, so now that we’ve set the high water mark, let’s take a look at some popular web technology platforms… (continue reading…)