Saltycrane logo

Sofeng's Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

Free Computer Science courses online


I found out there is a video lecture series to go along with my new book The Algorithm Design Manual. The audio level is really low, but I think it will complement my book reading nicely. There are also lecture notes and homework assignments. It also turns out MIT has a huge collection of free courses online. Not all of them have video though. I listed some interesting Computer Science related courses with video below. After more searching, I found UC Berkeley also has a number of free courses online, including four Computer Science courses with video. The final source ...

Read more...

Find the N longest lines in a file with Python


Here's an interview question I got recently from a Python company:

Write a program to read a multiple line text file and write the N longest lines to a new file. Where N and the file to be read are specified on the command line. Working solutions first but we are really looking for optimized solutions.

Here's my solution:

import sys

def main(filename=sys.argv[1], 
         N=int(sys.argv[2])):
    """ Finds the N longest lines in filename and writes to filename + ".new"
    """
    lines = open(filename).readlines()
    lines.sort(cmp=lambda x,y: cmp(len(y), len ...
Read more...

Notes on using pip and virtualenv with Django


I have been using a symlinking method to install Python packages up to this point. To better handle dependencies and multiple versions I have wanted to switch over to pip and virtualenv. Pip is a better alternative to Easy Install and virtualenv is a tool to create isolated Python environments. I have wanted to use pip and virtualenv for a long time now. Finally, today, I took my first steps and created an environment with the Python packages required for this blog. My notes are below. (I am running Ubuntu Intrepid and Python 2.5.)

A lot of my notes ...

Read more...

Converting time zones for datetime objects in Python


Install pytz

I am using pytz, which is a time zone definitions package. You can install it using Easy Install. On Ubuntu, do this:

sudo easy_install --upgrade pytz

Add time zone information to a naive datetime object

from datetime import datetime
from pytz import timezone

date_str = "2009-05-05 22:28:15"
datetime_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
datetime_obj_utc = datetime_obj.replace(tzinfo=timezone('UTC'))
print datetime_obj_utc.strftime("%Y-%m-%d %H:%M:%S %Z%z")

Results:

2009-05-05 22:28:15 UTC+0000

Convert time zones

from datetime import datetime
from pytz import timezone

fmt = "%Y-%m-%d %H ...
Read more...

Notes on using nginx with mod_python and Django


Here are my notes on setting up nginx as a reverse proxy with Apache, mod_python, and Django on Ubuntu Intrepid. Nginx is used to serve my static media files while all other requests are passed on to the Apache/mod_python/Django web server.

I realize mod_wsgi has become the preferred way to deploy Django, but I'm a little behind the times and am still using mod_python. I hope to switch to mod_wsgi soon.

I have been using Amazon's CloudFront service for delivering my static media files. As far as I can tell, it has worked well. My main ...

Read more...
Created with Django | Hosted by Slicehost