Live data from Hacker News

I only know PHP. How do I write a Web application in Python?

me.veekun.com

161–170 of 196 posts

Re: I only know PHP. How do I write a Web application in Python?

#161

The idea is that a transaction starts when a request starts, and it’s automatically rolled back if there’s an exception. This is behavior you want from the start! It’s half (err, ¼) the point of using a database. I mostly agreed with the rest of the article but this strikes me as massive blooper. I can see no reason this would be considered good advise. It's the database equivalent of the GIL, can't figure out what y…

It's only default behavior. The alternative is for any request that runs more than one query to be inherently but unpredictably unreliable; that is definitely not acceptable.

If you're doing something complicated, you're going to have to tread carefully anyway. No default could have handled your situation cleanly.

For what it's worth, zope transactions (which Pyramid uses) support combining notions of "transaction" from arbitrary other services and tries to do the right thing for a commit and rollback. I've used this to keep a database and mogilefs in sync, for example.

Re: I only know PHP. How do I write a Web application in Python?

#162

Personally, I _like_ PHP because, at the core, it is a collection of utility functions to write webapps quickly. Here's a small set of those function that I would like to see somewhere else (like in Python for example): Save a file: file_put_contents() Load a file: file_get_contents() Trim whitespace: trim() Print a string (even numbers!): print $str Dump an array: print_r() Print the stack trace: print $e->getTraceA…

I get the feeling you aren't very familiar with Python. Actually I'm hard-pressed to name a popular language in which writing a string to a file is particularly difficult.

You don't have to check for errors in Python; most functions raise exceptions on error, rather than letting your program continue with a known bad state.

The `if __name__ == "__main__":` pattern is a cute trick to distinguish a module from a file being run directly. It's not strictly necessary; just a nice habit. It's spiritually similar to the checks you have to do in PHP library files to make sure they're not being run directly.

You don't have to pass `self` around. You just list it as a parameter. There are design reasons for this, it's a tradeoff, blah blah. Think of it as the price you pay for getting to type 'def' instead of 'function'.

I'm not a C programmer, and I doubt many people writing web applications are, so how does wrapping C functions with C names help me? Are `'b' in 'abc'` and `len('foo')` that difficult to understand?

I like to think I've covered my reasons for disliking PHP well enough by now.

Okay. You're not the target audience of this article, then.

Re: I only know PHP. How do I write a Web application in Python?

#164
post #150
post #55

Earlier quoted context omitted.

Wicket, a thousand times wicket. It's so good it makes programming in java worthwhile. Its templating, if you can even call it that, is a quantum leap ahead of everything else: it's actually object oriented, you can write self-contained reusable components in the way you expect, and you get the full benefits of strong typing at every point. I really wish there was something like it for python, but everyone seems to t…

What about something like Flask-HTMLBuilder? http://majorz.github.com/flask-htmlbuilder/

That's not the worst idea in the world, but it looks like it expects your python code to walk the whole html tree, and it's very much mingling of logic and markup. The great thing about wicket (well, one of many) is that your markup is inert html files, and you can put reams of static content in if you want, without cluttering up the actual code. Your component hierarchy has to match your html hierarchy, but not every tag needs a component; you just need to make sure A is a descendant of B in markup (with no other components in between) whenever A is a child of B in code.

Re: I only know PHP. How do I write a Web application in Python?

#165
post #67

I was thinking this could redeem the author's credibility a little after the PHP rant, but the irrational hatred for Cheetah sounded very familiar. I only used it in one project, a few years ago, but there's really no bad thing about it I couldn't say about any other templating system in any language I ever tried. I wouldn't really take offense with that, but while the XSS chapter still has some points, the Sanitizin…

I'm kinda the de facto owner of Cheetah for a fairly heavy user of it—quite possibly the heaviest. It may appear-to-work for a small project, but you would not believe the insanity that lurks beneath the surface.

It's been effectively dead for years, anyway, and the author admitted he'd jumped ship for Mako in 2008.

Re: I only know PHP. How do I write a Web application in Python?

#166
post #73
post #67

I was thinking this could redeem the author's credibility a little after the PHP rant, but the irrational hatred for Cheetah sounded very familiar. I only used it in one project, a few years ago, but there's really no bad thing about it I couldn't say about any other templating system in any language I ever tried. I wouldn't really take offense with that, but while the XSS chapter still has some points, the Sanitizin…

Explaining why what he does not like about MySQL would have been moving too far from the subject of the article. And I liked the sanitizing chapter. It explains well why "sanitizing" is a misused word, which should be avoided. Almost always when I see people call it "sanitizing input" they do not seem to know how to handle input correctly.

I don\'t see the problem with \"sanitizing\". It\'s easy: just add a \\ before every quote and the string is totally safe!

Re: I only know PHP. How do I write a Web application in Python?

#167

I really don't think it's worth taking this guy seriously. First he writes a worthless and inaccurate lambast of PHP, and now he's trying to "convert" people to Python by telling us Unicode sucks. Get a life.

Who am I trying to convert? The article answers the question asked in the beginning—a question I've been asked several times since writing the PHP article.

Re: I only know PHP. How do I write a Web application in Python?

#168
post #98

By far the easiest way to get started with Python web development is Web2py which comes as a self-contained install where you can get started in 5 minutes. So you automatically get "request" information, big deal.

Flask comes as a self-contained install where you can get started in 5 minutes.

Re: I only know PHP. How do I write a Web application in Python?

#169
post #49

Earlier quoted context omitted.

Python 3 is not "really that new" and Python 2.6-2.7 is not "old" in any way. In fact we have: pre-2.4 (really old, preinstalled on old Redhats), 2.4-2.7, 3.x Python 3 is curing problem with variable visibility (and if you do not write in "pure" functional way - you will never see this problem) and unicode strings. Other differences is not so "revolutionary".

> (and if you do not write in "pure" functional way - you will never see this problem) Could you explain this a little more, please?

I assume he's talking about the `nonlocal` keyword introduced in py3, which allows overwriting names defined in outer-but-not-global scopes.

Re: I only know PHP. How do I write a Web application in Python?

#170

Earlier quoted context omitted.

> ... by telling us Unicode sucks. Could you tell us where he did this? (I didn't notice it on the first time through; he did say to use Unicode everywhere inside your code, though, and to not do anything rude like converting Unicode to ASCII by stripping non-latin1 characters.)

Right under the big bold "Unicode" header. The very next line is > Unicode sucks. This is a universal truth. (In fairness, I think what he's really getting at is that dealing with Unicode in languages that were created before it became ubiquitous sucks, not Unicode itself. But if you start off your discussion of Unicode with the statement that "Unicode sucks", you shouldn't be surprised if people think your beef is w…

Point. Mind you, he follows that with:

"(I’m lying. Dealing with encodings sucks. Unicode is great. It’s complicated. I’ll write about it later.)"

I think I'll wait for the follow-up article.

Post reply on HN