Python web programming (was Re: [ale] C#)
Joe Knapka
jknapka at kneuro.net
Wed Feb 11 01:54:35 EST 2004
John Wells <jb at devsea.com> writes:
> On Wed, 2002-09-25 at 15:43, cfowler wrote:
> > On Wed, 2002-09-25 at 15:19, Benjamin Scherrey wrote:
> > > Do it in python. You get an even easier language to use than java, all
> > > the platform independence (and perhaps more), and can always plug in
> > > some C/C++ stuff in the backend if performance becomes an issue. I'm
> > > doing all my web development in python now using mod_python and
> > > quixote. Database connectivity is great as well which is better than
> > > I can say for java or C# (although I'll be interested when a good
> > > implementation of jdo comes out).
>
> Really? Strange...I think jdbc provides particularly good database
> connectivity.
Python's DB facilities are better, IMO. The dynamicity of Python
makes it really simple to implement the "do it once and only once"
principle.
In Java, JDBC code tends to look like the following, wrapped up in
classes and repeated ad infinitum for whatever tables have to be
accessed (often generated by an IDE or XSLT etc):
DBConnection conn = ...;
Statement st = conn.createStatement("select foo,bar,baz from blah where key=5");
ResultSet rs = st.executeQuery();
while (rs.hasNext()) {
System.out.println("foo:"+rs.getObject(1));
}
You can do better, but few people bother, because there's usually
some kind of behemoth-DB-code-generating gadget lying around.
OTOH, in Python, I wrote one mixin class (in a couple of hours) that,
once and for all, implements persistent object-attributes for *any* DB
table -- that is, attribute accesses on an object can transparently
access the DB:
aBlah = new DBAttrib(table="blah",attribs=["foo","bar","baz"],key=5)
print "foo:",aBlah.foo # Implicit select.
aBlah.foo = "a new value" # Implicit update.
My recipe for attribute persistence is available via the Python
Cookbook at
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/106131>; it's
86 lines of code including comments, and is currently in use in
(small-time) production code. There are lots of other useful DB
recipes there as well.
Of course, you could do something similar in Java:
DBAttrib blah = new DBAttrib(...);
System.out.println("foo:"+blah.get("foo"));
but it would require some effort, and it would be quite error-prone
(since there'd have to be casts on all of those blah.get() calls in
practice). Python makes this kind of thing nearly trivial, and the
result is much nicer-looking IMO.
> How's your experience with python and web programming? I'm sort of
> mapping out our shop's "standard" webdev language...leaning towards
> PHP when 5 makes it out of beta. Still, I'm a python fan, but have
> never really done much web programming with it. Always seems like
> there were 20 different projects going in 20 different directions,
> and it was very hard to determine which would garner more support in
> the long term and become the sort of "standard". I'm fine with
> using lesser known projects personally, but when making a decision
> for the enterprise, I'd like to be very careful in this regard to
> choose something with longevity.
I am strictly a small-time web developer, so apply appropriate
skepticism to the following:
I've done some Zope <http://www.zope.org> development, and didn't
really like it much, mainly because it has a very steep learning curve
and for me the payoff wasn't worth the climb. Programming for Zope is
not really very much like writing pure Python code.
I like the CherryPy appserver <http://www.cherrypy.org> a lot, in
spite of the silly name. CherryPy isn't going to win any performance
prizes, but it's fast enough for me, and quite flexible. It does have
one notable wart, which is that CGI checkbox fields are passed into
a handler method as lists when multiple items are checked, but as
scalars when only one item is checked; working around that is not a
problem if you know about it ahead of time. (Kind of a pain when
you don't :-)
CherryPy also has what seems to me to be good support for
aspect-oriented programming -- a lot of boilerplate code can be
abstracted away using aspects. But it's been my only exposure to AOP,
so I do not have an informed opinion.
> While I think PHP5 will be a strong contender due to the arrival of
> a rather full set of OO features, if I were a betting man, I'd say
> there's a strong possibility will ultimately end up a Java shop.
> I've been very impressed by two things from the Java community:
> a. the quality of software being produced by a number of different
> projects/groups, particularly those out of the Jakarta project, and
> b. the high level of community support and participation.
>
> Anyway, interested to hear your comments/experiences. Thanks!
I believe that you can write Jython code that will run in Java app
servers, thereby having the Java cake while eating the Python one.
I haven't tried it, though.
Cheers,
-- Joe Knapka
--
I'm gonna do everything / silver and gold / but I got to
hurry up before I grow to old. -- Joe Strummer, 1952 -- 2002
If you really want to get my attention, send mail to
"jknapka .at. kneuro .dot. net."
More information about the Ale
mailing list