Moving from PHP to Python. Is it Possible

zeph zephjc at gmail.com
Fri Dec 11 10:11:12 EST 2009


Hi Sancar,

1) PHP does some really nasty things in how it treats globals, and you
will have to break yourself of those sorts of habits -- Python offers
much cleaner alternatives, like grouping similar functionality into
modules which can be imported; the import functionality in python is
pretty flexible.
Python won't pack a ton of garbage into a giant, global
'dictionary' (what python calls associative arrays).  There are
modules (bundled of code) which you can import to handle your needs.

2) Check out the traceback[1] module for retrieving the traceback info
for logging to a file, and cgitb[2] modules for printing the traceback
as HTML to your browser.

3) http://docs.python.org/ is a great place to start - it has a lot of
well written and generally thorough documentation with examples

4) It's better to collect all your eventual output into a string that
you print - there are examples at [3]. You can import from other
modules as needed (even conditionally), grow your string for output,
then finally print it like (this example was adapted from one found on
[3]):

output =  '<html><head>'
output += '<title>My Page</title>'
output += '</head><body>'
output += '<h1>Powers of two</h1>\n<ol>'
for n in range(1,11):
  output += '<li>'+str(2**n)+'</li>'

output += '</ol></body></html>'
print output


You can copy-paste this right into your Python interactive shell to
see the output. Note: += is inline string concatenation.


5) You can get form state from the cgi module[4] and FormStorage
object, and you can get server environment data from the os.environ[5]
dictionary;

Good luck and keep on learning! :-)

- zeph


References:
1: http://docs.python.org/library/traceback.html
2: http://docs.python.org/library/cgitb.html
3: http://gnosis.cx/publish/programming/feature_5min_python.html
4: http://docs.python.org/library/cgi.html
5: http://docs.python.org/library/os.html#process-parameters



More information about the Python-list mailing list