Over the last couple of days I’ve been learning some Python to write the Web Applications 1.0 ((X)HTML5) Tracker Tool. I quite enjoy the whitespace significant scripting. Dive Into Python was used a lot as starting point and Google queries with Python in them as well. Besides starting the file with
#!/usr/bin/python and doing
chmod 755 I had to put the following in a
<Files specification-diff> SetHandler cgi-script </Files>
Some function I use for input checking:
def isInt(s): try: ret = int(float(s)) return True except ValueError: return False def toInt(s): return int(float(s))
Here’s another one which returns one of the integers in a (large) string (thanks to Mark for doing all the work):
def getNumber(s, n): import re return re.split("\D+", s)[n]
When the tracker tool is a bit more stable (suggestions welcome) I’ll release the source code in some way. The Subversion bits are down by invoking the command line, for what it’s worth.
I'm pretty convinced that Python is the way to go for server side web programming these days. PHP just doesn't cut it. I am curious as to why you're doing
int(float(s)) instead of just
I also started learning Python some time ago, also from Dive into Python. Really like the language (and the book).
Unsolicited advice -- move the import statement out of the method. Otherwise, AFAIK, you will re-import it every time that method is called.
In fact, it's preferred Pythonic style to put all import statements at the top of the file.
Such tips are much appreciated, and encouraged! Changed that in the deployed code.
I am curious as to why you're doing
int(float(s))instead of just
<input type="number">s are floats, and so browsers may send numbers with exponents.
Good choice! Promoting maintainability and simplicity is what makes Python such a nice language.
I recommend reading the official Python style guide. Following accepted conventions helps with code readability, and that guide is chock full of useful tips. I also recommend using new-style classes. They provide more options for customization and allow you to subclass built-in types.
As far as web applications go, I recommend making a WSGI application. It's an official Python standard that defines how web applications and application servers interact. A lot of useful components utilize the specification.
And a small nitpick: use
#!/usr/bin/env python. This way your script doesn't rely on python being in
My small tip I recently came upon and I find myself using a lot.
Make a quick search (on 'p') in your browser for the following string:
(or however you set this up in Opera)
Then type "p re" in your address bar.
If you're using python for web-based applications, you should definitely use mod_python. I'm sure you will love it, just take the time to read about it...
The more I use Python the more I like it. Be sure to check out Django if you haven't already.