You Are Here Home > November 2008

November 2008

Let’s make Python web friendly

Here is a great language, so powerful and fun to write code with yet it’s not really being used for developing web applications.

There is no easy way to run your Python scripts like PHP, you will either have to run them as CGI and there is a lot of pain associated with that if you know what I mean, sometimes you only get “Internal Server Error” and you have to pull your hair out until you find out what the problem is.

The other thing you can do is to use something like mod_python, which is almost impossible for an average user to install and it imposes some restrictions on you. For example, you have to have certain methods with certain names and mod_python will call those for you.

I’m in the process of starting to make a module for Apache (and the Lighty and then IIS hopefully) that has three great qualities that will help getting Python out there for web developers to try:

1 – It will install very, very, very easily on all platforms, it will be ready to go, no pain. The server admin just runs install and it’s there, ready to use.

2 – It doesn’t do anything fancy, nothing fancy, it only hands over the Python script to the Python interpreter and writes the output to Apache, that’s it.

3 – You can put your Python scripts anywhere you want just like PHP.

The goals of this project:

1 – To make Python readily available for anyone to try, without a lot of pain, as easy as PHP.

2 – To have this new Python module installed on all the hosting packages.

3 – To have this new Python module be part of all Linux distributions.

4 – To have a lot of web developers finally using it and enjoying it.

5 – To start the development of a lot of open source web apps written in Python.

If you want to help me with this, leave a comment, you don’t have to be a C wizard.
Otherwise, please help me spread the word in any way you want.

Thanks!

Let’s make Python web friendly
Comments (15)   Filed under: Python   Posted by: Codehead

Python: understanding/reading list comprehensions

List comprehensions in Python can be tricky and hard to look at but once you know the secret, it will become very clear, for example, say we have a list:

a = [1, 2, 3, 4, 5]

And we want to make a second list out of this “b” with values of “a” plus 2, so we want to get this:

b = [3, 4, 5, 6, 7]

Now, the way to do this without using any fancy feature would be:

b = []
for item in a:
    b.append(item + 2)

To turn this into a list comprehension we will start like this:

b = [for item in a]

Now, we will have to add our “item + 2″ to the left side of the for loop so:

b = [item + 2 for item in a]

If you didn’t understand what happened, don’t worry, just keep reading.
Now, let’s look at another example, this time, we will try to understand a list comprehension that’s already written, a very simple one:

a = [1, 2, 3, 4, 5]
b = [x*x*x for x in a]

To read this, start form right to left:

for x in a: x*x*x

Internally, this will be translated to something like:

b = []
for x in a:
    b.append(x*x*x)

So the secret is to read from right to left.

If there should be a condition, it will come after the for loop so to increment all the items that are greater than 4 by 2:

b = [item + 2 for item in a if item > 4]

I hope this helps :)

Python: understanding/reading list comprehensions
Comments (0)   Filed under: Python   Posted by: Codehead

Print without a new line or space in Python

Here is another little trick in Python; the print statement always inserts a line break after it prints your text, for example:

print "Hello"
print "Python"
print "!"

Will print:
Hello
Python
!

Now, you can fix the issue with the line break by inserting a comma after the print statement:

print "Hello",
print "Python",
print "!"

This will print:
Hello Python !

But the problem is that it will still insert a space after it prints the text, so to fix this you will have to use sys.stdout.write like this:

import sys
sys.stdout.write("Hello ")
sys.stdout.write("Python")
sys.stdout.write("!")

This will print:
Hello Python!

Have fun with Python :)

Print without a new line or space in Python
Comments (18)   Filed under: Python   Posted by: Codehead

__FILE__ equivalent in Python; get the path to current file in Python

Update:
There is __file__ in Python.

You could do this as suggested by some but I don’t know why:

import inspect
this_file = inspect.currentframe().f_code.co_filename

I hope this helps :)

__FILE__ equivalent in Python; get the path to current file in Python
Comments (7)   Filed under: Python   Posted by: Codehead

Dynamically adding methods to objects in Python

The trick is that you have to add the function to the class not the instance to see how, run your interpreter and write these:

class test:
	def __init__(self):
		self.yes = "Hello Python!"
 
def a(self):
	print self.yes
 
t = test()
t.b = a
t.b()

When this runs, you get:

Traceback (most recent call last):
File “ “, line 1, in
t.b()
TypeError: b() takes exactly 1 argument (0 given)

To fix this, you will have to assign the function a to the class itself (test) not the instance (t) like:

test.b = a
t = test()
t.b()

Python is fun :)

Dynamically adding methods to objects in Python
Comments (0)   Filed under: Python   Posted by: Codehead
« Newer Posts