Web Hosting Talk







View Full Version : PHP and Python for Web Programming.


OminousZounds
07-29-2008, 01:20 AM
Greetings.

The following is the result of musing I've been doing today, and I thought I would like to see other opinions on this.

When I first began web programming, it was with PHP. Naturally, this is nothing unique. It was easy, and in my opinion, had a low barrier of entry.

Fast forward to nowadays, and I've tried a lot of other programming/scripting languages, for various purposes other than but not excluding web programming. My most loved find, however, would have to be Python.

But I have a problem. I've been using PHP for so long that I've become used to it's way of doing things for web programming, and Python's different handling (not file based..) just confuses me, which is an odd thing for Python to do.

Perhaps I've just grown accustomed to PHP, or maybe Python just has a higher barrier of entry when it comes to programming web solutions with it. Granted, there are tutorials and such, I know this, but I'm curious. Does anyone else find themself feeling the same thing? Or perhaps you started with Python for the web and find PHP to be horrid? (Lord knows it could use some clean up when it comes to namespaces...)

Assuming I'm not the only one who feels this way, do you think it's because PHP has a lower barrier of entry when it comes to this, as opposed to Python, which might be higher for the web?

Or maybe it's something I'm not considering, like that PHP was intended for the web all along, and Python wasn't?

What are your thoughts?

Edevere
07-29-2008, 02:37 AM
Greetings.

The following is the result of musing I've been doing today, and I thought I would like to see other opinions on this.

When I first began web programming, it was with PHP. Naturally, this is nothing unique. It was easy, and in my opinion, had a low barrier of entry.

Fast forward to nowadays, and I've tried a lot of other programming/scripting languages, for various purposes other than but not excluding web programming. My most loved find, however, would have to be Python.

But I have a problem. I've been using PHP for so long that I've become used to it's way of doing things for web programming, and Python's different handling (not file based..) just confuses me, which is an odd thing for Python to do.

Perhaps I've just grown accustomed to PHP, or maybe Python just has a higher barrier of entry when it comes to programming web solutions with it. Granted, there are tutorials and such, I know this, but I'm curious. Does anyone else find themself feeling the same thing? Or perhaps you started with Python for the web and find PHP to be horrid? (Lord knows it could use some clean up when it comes to namespaces...)

Assuming I'm not the only one who feels this way, do you think it's because PHP has a lower barrier of entry when it comes to this, as opposed to Python, which might be higher for the web?

Or maybe it's something I'm not considering, like that PHP was intended for the web all along, and Python wasn't?

What are your thoughts?

This whole 'intended for the web' is so specious...it was intended to be a bunch of Perl scripts to glue some stuff together that happened to be web work, and the lack of design shows.

'Python for the web' is quite crappy if you mean raw cgi or the common PHP thing of writing a bunch of spaghetti code into some filename.php files to slap up a site. BUT, Python has a couple of web frameworks that rival Rails for ease and power, and in terms of those, it's anything but crappy. I mean, when I first tried to use Rails at work it didn't work out, but even in failure I could see immediately that something like this was the best way to approach building a certain kind of website that we'd seen yet. The 'file based' PHP approach is ready to be taken out back and shot. Comparatively Rails and Django are a pure pleasure to work with(and, you have to use Pylons for it, but sqlalchemy is God's gift to ORM). There's no reason to be doing that kind of old development anymore. Even PHP realized this and that's why we have Cake and Symfony.

Stick with it man -- PHP only has a lower barrier of entry for amateurs and toys. Think about serious PHP work -- you end up doing the same thing Python/Ruby frameworks are doing. There's a reason Schnossnagle's Advanced PHP book talks about the 'Active Record' pattern, about using a templating language (even though PHP itself is a template language of sorts), about taking advantage of OO features of PHP, etc. etc.. these are all things that you will get for free with Python/Rails along with the added power that more advanced languages can bring you. And since the barrier of entry is higher, I would guess that the Rails plugins and Django contribs you find will on average be of higher quality than most PHP code you find out there.

When I started learning Erlang, I just couldn't fathom what was going on there. It made no sense, the syntax was arcane, single assignment bugged the **** out of me...I kept at it, and now I don't want to throw the book across the room. It's a start anyway.

OminousZounds
07-29-2008, 02:51 AM
This whole 'intended for the web' is so specious...it was intended to be a bunch of Perl scripts to glue some stuff together that happened to be web work, and the lack of design shows.
Oh I'm well aware of it's rickety beginnings, you'll notice I said nothing for how well it did it's job. And I agree, it's got more than a few annoying things about it, things that limit progress and make me want to collide desk and forehead together. Sometimes repeatedly. :rolleyes:


'Python for the web' is quite crappy if you mean raw cgi or the common PHP thing of writing a bunch of spaghetti code into some filename.php files to slap up a site. BUT, Python has a couple of web frameworks that rival Rails for ease and power, and in terms of those, it's anything but crappy. I mean, when I first tried to use Rails at work it didn't work out, but even in failure I could see immediately that something like this was the best way to approach building a certain kind of website that we'd seen yet. The 'file based' PHP approach is ready to be taken out back and shot. Comparatively Rails and Django are a pure pleasure to work with(and, you have to use Pylons for it, but sqlalchemy is God's gift to ORM). There's no reason to be doing that kind of old development anymore. Even PHP realized this and that's why we have Cake and Symfony.
Mainly I was talking about things like Django, CGI just doesn't seem worth it, in PHP or Python. It's reassuring to see that you like it so much, though. I couldn't expect any less from Python, though. (I'm such a fanboy...)


Stick with it man -- PHP only has a lower barrier of entry for amateurs and toys. Think about serious PHP work -- you end up doing the same thing Python/Ruby frameworks are doing. There's a reason Schnossnagle's Advanced PHP book talks about the 'Active Record' pattern, about using a templating language (even though PHP itself is a template language of sorts), about taking advantage of OO features of PHP, etc. etc.. these are all things that you will get for free with Python/Rails along with the added power that more advanced languages can bring you. And since the barrier of entry is higher, I would guess that the Rails plugins and Django contribs you find will on average be of higher quality than most PHP code you find out there.
Oh, I'm no PHP beginner ;) Though I've not delved as deeply into Django or similar things just yet. Nevertheless, that's an interesting point you have there. The higher barrier of entry meaning that the average snippet one would find being higher quality seems plausible to me, because as we all know, there are =plenty= of horrid PHP examples out there, and the sad thing is, the beginners probably won't even know it.


When I started learning Erlang, I just couldn't fathom what was going on there. It made no sense, the syntax was arcane, single assignment bugged the **** out of me...I kept at it, and now I don't want to throw the book across the room. It's a start anyway.
Heh, well, I can always catch your book and sell it. :stickout:

blueroomhosting
07-29-2008, 07:42 AM
Since I already know the fundamentals, these large frameworks always seemed unattractive to me. I like keeping things simple and flexible.

The fact that a PHP page is just a file I can edit is great, but the language itself leaves a lot to be desired.

When I'm not programming in lisp, it is Python, so for churning out web pages I just wanted a simple, elegant template system (as throwing strings together isn't pretty).

PXL (http://dgym.homeunix.net/mediawiki/index.php?title=PXL) is a hybrid language that looks like Python, and uses Python for control, but generates XML.

Even with no explanation this example probably makes sense...


html:
body:
Some simple multiplication...<br>
>for x in range(5):
${i,x} times ${i,x} is ${i,x*x}<br>

OminousZounds
07-29-2008, 02:58 PM
Since I already know the fundamentals, these large frameworks always seemed unattractive to me. I like keeping things simple and flexible.

The fact that a PHP page is just a file I can edit is great, but the language itself leaves a lot to be desired.

When I'm not programming in lisp, it is Python, so for churning out web pages I just wanted a simple, elegant template system (as throwing strings together isn't pretty).

PXL (http://dgym.homeunix.net/mediawiki/index.php?title=PXL) is a hybrid language that looks like Python, and uses Python for control, but generates XML.

Even with no explanation this example probably makes sense...


html:
body:
Some simple multiplication...<br>
>for x in range(5):
${i,x} times ${i,x} is ${i,x*x}<br>


Holy toledo, PXL is nuts... I see a lot of things going on in the example page you linked that I would consider very un-pythonic. At first glance I don't find that at all appealing, but you are right, it's pretty darned simple.

Burhan
07-29-2008, 05:06 PM
As a 5 year PHP developer, I can tell you that Python is just a different ... planet.

I have developed just about everything in PHP you can think of (save for desktop applications), and yes -- its dead easy to write stuff in it, it is also dead easy to write crap in it, which is part of the problem; because PHP will happily toodle along as if all was lovely in the world, where you app probably has holes the size of jupiter.

After struggling with PHP and its various frameworks (I still remember when Prado won the "best framework" award for PHP5 frameworks), including Prado (which is an emulation of ASP.NET), Symfony, Cake, etc. and finally I guess Zend got a clue and realized that hey, if we don't put a framework together that actually does stuff right -- then no one will, and now we have the lovely Zend Framework.

Even with all my experience with PHP, and my lack of experience with Python (I had to keep the "Learning Python" book open next to my workstation to keep referring to it), Django was simply a "wow" experience for me; and just so you don't think I'm a django fanboy, I have also spent plenty of time with Turbogears and Pylons (pylons has the best ORM you can dream of -- well, other than the daddy of them all hibernate), but django just seemed to get everything together correctly from the start; especially for web development.

So now, it has become my framework of choice -- the time it takes to get a "CMS/blog" site developed from scratch in Django compared to _____ (insert PHP framework) is just astounding.

Things I like about Python:

1. Whitespace matters. At first, this might bug the **** out of you as it did me, but later I realized that its a far better alternative than everyones-own-way of nesting { }

2. Proper OO. Everything in Python is an object (much like Ruby), and everything you develop is either object based, or object oriented. Most of PHP5 programming now is object based but not really object oriented. They are getting there with the excellent development going on in PHP5, but compared to Python its still lacking.

3. Utility of the language. PHP is designed for one thing -- and one thing only. Python, on the other hand -- is a very versatile programming language; which from a programmer's standpoint is a great plus since your time and energy invested in Python can also be carried over into other domains. This is not the case with PHP, as if you are a very good PHP developer, you can write very good web apps. However, if you are a very good python developer, you can write web apps, servers, desktop applications, rendering engines, plugins, etc. etc.

4. Corporate backing. Python is used in production by a lot of very serious players (NASA, Google, ILM) -- this is important in that it ensures that [a] your skills will be in demand and [b] the language/platform has sustainability.


Python really wasn't designed for anything in particular -- its a general purpose programming language; but the frameworks that are out there make developing anything web related (from client apps ... ie PHP's domain) to complete clients and servers (look at Twisted) very easy.

I'm glad I found Python and I feel that learning how the other side approaches a problem helps you to get out of the tunnel vision that you get if you are enamored with a particular technology. I thought there was nothing better than PHP for web development; a mixture of C/C++/PERL with almost universal install base and tons of libraries? What could be more brilliant? ... till I found Python.

My advice is to give the language a few weeks to grow on you and don't just dismiss it after two or three dry runs. It is well worth the investment.

And now, I shall step down from the soapbox and hand it over to....

ytz991
07-30-2008, 05:54 AM
how do you paste code in python from one block into another - under an if ?
you have to prepend spaces before every single line in that block ?

Burhan
07-30-2008, 06:22 AM
print 'Starting loop....'
if 15 < 30:
print '15 is less than 30'
if 15 < 20:
print '15 is also less than 20'

print 'Out of the loop'

OminousZounds
07-30-2008, 06:22 AM
Pfff, nice soapbox handling there, Burhan ;)

ytz991, it depends on your editor. Most editors should be able to copy what's in front of the existing lines of code along with it. However, if you're getting the code from someplace that doesn't prepend with whitespace, then you may have to have an IDE auto-format it, format it manually, or gripe at whoever is in charge of wherever you got that code to display it properly or parse the code properly, whichever the case may be.

Edit: Misread your problem, ytz991, but the answer is mostly the same, if your editor is able to handle python's whitespace rules, and you have the proper number of whitespaces both where you're pasting at and in whatever your posting, then it should work. Otherwise you'll have to do it manually.

Edit the Edit: Wow Burhan, you really slipped your post right in there, before me, huh :P

kmching
08-04-2008, 01:33 AM
"I can tell you that Python is just a different ... planet. "- Burhan

You certainly won't get any argument there. But Python is not only different, it is also far far more advanced than PHP as a language. Here's just one small example: If I were reading from a database result and wanted to spit out TD cells for such, it's a pain having to keep track of when you want to end a row (TR) and when just the cell (TD). Python generators (tutorial here) (http://www.neotitans.com/resources/python-generators-tutorial.html) make your logic oh-so-clean. Here's a sample showing something that just won't be possible in PHP:


def retrieveDBrows():
result=cursor.execute("select id,name from names")
for row in result: yield result

yieldrow=retrieveDBrows()

while 1:
print "<tr>"

row=yieldrow.next()
print "<td> %d %s </td>" % (row["id"],row["name"])

row=yieldrow.next()
print "<td> %d %s </td>" % (row["id"],row["name"])

print "</tr>"

That's basically it! No confusing nested for loops to keep track of how many columns, etc... Think of a generator as a "function" that spits out a different value each time for which you do not need to live within a for loop to access.

kmching
08-04-2008, 01:41 AM
In case the generator concept is not clear, here is the simplest possible demonstration. def generatorABC():
while 1:
yield "A"
yield "B"
yield "C"

x=generatorABC() Now, every time you call x(), it will return "A", "B", and "C" over and over again. Nifty, ain't it?


By the way, WHT's code tags are a real pain to use, I have to insert the <br>s manually...

kmching
08-04-2008, 01:44 AM
The simplest way to get started with using Python as a server side language is not to try Django or Turbogears, but to learn the language itself and use something like Spyce (http://spyce.sourceforge.net), which is the equivalent of PHP. I think people who use Rails fail to appreciate Ruby the language enough, and use the framework before knowing the language in and out. Same thing applies to Python wrt Python frameworks. But because Python, the language, is a lot higher level, there might be less of a need to resort to a framework compared to PHP. Your code stays shorter and manageable far longer and you need not incur the rigidity and 'discipline' of a framework in many instances.

kmching
08-04-2008, 02:01 AM
2 corrections...


def retrieveDBrows():
result=cursor.execute("select id,name from names")
for row in result: yield result
the last line should be for row in result: yield row


and for Now, every time you call x(), that should be x.next() not x().

jerrymatchel
08-05-2008, 01:44 AM
I have played with Python/Django and PHP with Cake . Theres not too many people working with Python (as compared to php). You should consider this as a big upsell for PHP (or for that matter other languages like .NET). Imagine writing a large system and then when your looking for someone to maintain it (so you could do something else) - you dont find anybody. Its easier to hire PHP developers from place like www.s4support.com (I use them) ask them Python and they have few developers. I would consider this a big upsell for popular languages since it also decides the longevity and continuity of my code without me coding.

lenscape
08-05-2008, 05:28 AM
Quite a few of the comments so far recommend templating languages that mix the HTML with code. Mixing layout with logic makes pages harder to manage and more prone to breaking. It also means it is much harder to split the web creation functions between graphic designer and programmer.

If you use a templating language such as TAL (Zope or SimpleTAL), the web page is mostly HTML but contains special attributes to access objects and properties.

I don't know PHP very well but it seems you are more or less compelled to mix the PHP logic with the HTML. Python, being a general purpose language, has no such constraints. TAL is written in Python and when combined with Zope provides a very powerful site infrastructure which also takes care of lots of tedious stuff like user accounts and permissions.

Burhan
08-05-2008, 09:13 AM
yieldrow=retrieveDBrows()

while 1:
print "<tr>"

row=yieldrow.next()
print "<td> %d %s </td>" % (row["id"],row["name"])

row=yieldrow.next()
print "<td> %d %s </td>" % (row["id"],row["name"])

print "</tr>"


Can be revised to:


for row in retrieveDBrows():
print "<tr>"
print "<td> %d %s </td>" % (row["id"],row["name"])
print "</tr>"


Membership checks automatically call the .next() of generator functions.

kmching
08-05-2008, 09:18 AM
Its easier to hire PHP developers from place like www.s4support.com (http://www.s4support.com) (I use them) ask them Python and they have few developers. I would consider this a big upsell for popular languages since it also decides the longevity and continuity of my code without me coding.

Even though I love Python, I will not argue with the above. The thing to keep in mind is that if you go with a specific Python technology, such as Django or TurboGears, it compounds the problem even more.

Getting someone who knows Python the language is a lot easier than someone who knows TurboGears or Django. I have been using Python for around ten years now and though I am quite seasoned with the language, I have little interest in learning Django, Turbogears or CherryPy. On the other hand, Spyce (http://spyce.sourceforge.net) (http://spyce.sourceforge.net%29), I have no problem with, since it's basically just straight Python and not much additional API or philosophy to learn.

kmching
08-05-2008, 09:24 AM
yieldrow=retrieveDBrows()

while 1:
print "<tr>"

row=yieldrow.next()
print "<td> %d %s </td>" % (row["id"],row["name"])

row=yieldrow.next()
print "<td> %d %s </td>" % (row["id"],row["name"])

print "</tr>"
Can be revised to:


for row in retrieveDBrows():
print "<tr>"
print "<td> %d %s </td>" % (row["id"],row["name"])
print "</tr>"
Membership checks automatically call the .next() of generator functions.

The problem with the revised code is that you can no longer do 2 TDs within the for loop. The point of the code I gave was to show how to do away with for loops when you find yourself wanting to spit out multiple columns within a TR (a common need).

You would have to put some nasty, counter checking code if you were within a for loop and you wanted to print out multiple TDs within that TR.