Author Archive
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:
Dead-simple pagination wizardry with TurboGears 2
by Seth on Nov.03, 2009, under Web Development
I recently had to implement pagination in a TG 2 project that I’m almost finished with. Although I had previously glanced at the paginate webhelper module, I first turned to the updated TurboGears documentation to see what was “officially” written up about this subject.
A lot of work has been going into the TG docs in preparation for the upcoming v2.1 launch, and one little gem that was recently added was the Pagination Quickstart (thanks Lukas). While this sent me down the right path towards pagination perfection, there were a few things that came up that I felt like deserved further explanation:
- The @paginate() decorator is an extremely simple wrapper for the paginate webhelper module.
- When paginating SQLAlchemy queries, paginate runs a duplicate count() query to figure out its current position in the data collection. While the magic of this is nice, the possible performance issues with duplicating complex SQL queries on a high-traffic website could be undesirable.
- It’s common to have controller methods dynamically switch to a “feed template” when doing AJAX pagination. Don’t worry, paginate + override_template makes this a piece of cake!
The @paginate() decorator makes pagination a breeze
The @paginate decorator is a wrapper for the paginate module that can be added to your controller methods. Simply specify your data collection’s name as the first argument of the decorator, and pagination is “automagically” setup for you.
An example controller method with pagination would look like this:
# dead-simple pagination in TurboGears 2 @expose('myproject.templates.posts') @paginate("posts") def posts(self): posts = DBSession.query(Posts) return dict(posts=posts)
Really? That was easy!
The documentation on this decorator seems quite clear to me, so instead of repeating its content here I will simply give you a link for further reading.
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…)