Live data from Hacker News

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

me.veekun.com

151–160 of 196 posts

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

#151
post #143

Earlier quoted context omitted.

Except that Java strings aren't in Unicode. They're in UTF-16, which is the worst-of-all-worlds encoding. (It's big and heavy, and it still has multibyte sequences. They're just rare enough that you're likely to forget about them during testing.)

UTF-16 is most certainly Unicode, even if it isn't the preferred flavor.

Wrong. UTF-16 is an encoding, Unicode is the abstract representation. You can encode Unicode strings into UTF-16, but that doesn't make UTF-16 Unicode. Python's Unicode strings are actually really just Unicode, that's why you can't write them to a file or anything - you need to encode them first (which defaults to UTF-8).

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

#152
post #143

Earlier quoted context omitted.

UTF-16 is most certainly Unicode, even if it isn't the preferred flavor.

Wrong. UTF-16 is an encoding , Unicode is the abstract representation. You can encode Unicode strings into UTF-16, but that doesn't make UTF-16 Unicode. Python's Unicode strings are actually really just Unicode, that's why you can't write them to a file or anything - you need to encode them first (which defaults to UTF-8).

Unfortunately it only defaults to utf-8 in Python 3

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

#153
post #41

Earlier quoted context omitted.

Python 2 versus 3 isn't anything like PHP 4 versus 5. The problem is that there are a few minor but backwards-incompatible changes to the syntax in py3. Libraries can't update until all their dependencies update. And web frameworks are some of the most complex software with the most dependencies, so they're updating last. It's still pretty much the same language behaving in the same way, though. If you know Python 2…

"Don't worry, migration from Python 2 to 3 is really easy. It'll take 5 minutes to learn." "Migration for libraries from Python 2 to 3 is really hard. It's taken them a few years and counting." ??

Right. Learning Python 3, once you know 2, is really easy.

Library maintainers have two problems: they have to twiddle their thumbs until all their dependencies have ported, and then they have to get their code running against both 2 and 3. The former is exactly why I'm saying to use 2 for now, and the latter is easy if you only support 2.7 but rather more difficult for projects that still want to run on some archaic thing like 2.3.

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

#154
post #116
post #23

Earlier quoted context omitted.

Fair point. Correction: working with encodings sucks. Py3 isn't magnitudes better with Unicode; it's better about converting incoming bytes, and it makes literals into real strings. But the former isn't necessarily a good thing, and you can get the latter with a `__future__` import. Always dying on implicit conversions is definitely nice, though.

Working with encoding is not that bad in for example Ruby 1.9. What makes them horrible to work with are the bad design decisions in Python, especially Python 2.

I've only read about Ruby strings, not used them, but they seem like a middle ground between Perl's approach and Python's approach. What do you perceive as the problem with Python?

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

#155
post #137
post #94

Earlier quoted context omitted.

See the Heroku Quickstart: https://devcenter.heroku.com/articles/quickstart Essentially you sign up for a Heroku account ( https://api.heroku.com/signup ), and set up the Heroku "toolbelt" ( https://toolbelt.heroku.com ). Once that's done, you can run this bash script ( https://gist.github.com/2622850 ) to create and deploy a "hello world" Flask app on Heroku: $ heroku login $ bash setup.sh helloworld $ cd helloworld…

Compared to deploying Hello World in PHP, that's a lot of work.

I guess that's true, but you know... so what? Can't we worry more about building something cool than about shaving seconds off the one-time setup cost involved with sharing that cool thing with the world?

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

#156
post #126

Earlier quoted context omitted.

In the context of the parent comment I'd argue that the differences are insignificant. Xuzz argued that PHP is simpler because you don't need to know the ecosystem. Which I countered by showing a basic example of Python which doesn't depend on the ecosystem (like frameworks, package managers or version control). Besides that, I don't think knowing HTTP headers or understanding significance of trailing newlines is req…

But I never needed to worry about those three lines in PHP, to a new user that is important. To you and I they come naturally. PHP "is especially suited for Web development and can be embedded into HTML" [1]. Python "lets you work more quickly and integrate your systems more effectively" [2]. They can both do the task of the other, but the upstart time for a developer to handle the task of making a web app is smaller…

Python has a `cgi` module that does most of the superglobal stuff for you.

But, er. Does this really matter? Yes, you can get "hello world" in PHP with one line of not-PHP. You can also get "hello world" in Python + Flask with seven lines, and you'll get half a dozen extremely nice features for free that you can learn about as you go.

Arguing about what stack has the simplest way to build from total scratch is a sad race to the bottom. I'd rather be making the point that rich Web development can be easy, not that cobble-your-own-thing-together-with-duct-tape can be easy.

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

#157
post #52
post #10

I'd say the necessity of this post is why PHP is as popular as it is. With PHP, there are no package managers I have to install; there is no template language to learn; there is often no version control; there is no "framework"; etc. You just drop in a .php file and, most likely, it will just run. If I need a quick dynamic page on my server, it's much easier to do this in PHP than in Python or Ruby. Until Python or R…

CGI scripts have much of PHPs simplicity. You can just drop in a executable file, and depending bit on server configuration, it will just run. With Python a basic hello world would look something like this: #!/usr/bin/python print "Content-type: text/plain" print "" print "Hello World!" So 3 lines of "boilerplate", and then you can do anything you want. No extra packages to install or languages to learn. No framework…

CGI is easy, yeah, but it has a lot of the same disadvantages as mod_php: no routing, no shared state, slow execution, server misconfiguration can expose your code, possible code execution if you accept uploads, etc.

There are approaches that'll let you dump a WSGI app (with a known interface) anywhere and have the web server still run it. Gosh, someone ought to write about those.

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

#158
post #145
post #128

Earlier quoted context omitted.

transaction-per-request is by far the best default pattern to use. If some particular methods have special needs, like needing to break up the operations into multiple transactions to deal with third-party communication, those are the exceptions. The alternatives to transaction-per-request are autocommit, or explicit transactions required at all times. Autocommit is a terrible choice because now you've lost all atomi…

Why not the third (and in my experience most common) alternative? Autocommit but if you explicitly start a transaction then automcommit is turned off until that transaction has been committed.

If you have more than one statement, then autocommit is usually wrong, and what you usually want is transaction-per-request. If you have exactly one statement, then autocommit is exactly identical to transaction-per-request.

Also part of the transaction-per-request pattern is that the transaction is started when the first statement is invoked. So there's no overhead for zero-statement requests either.

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

#159

Earlier quoted context omitted.

All we need is to be able to drop an .erb file in a server and have it instantly rendered with the same magic of .php files. How hard would that be? Anyone here know how to write plugins for Apache?

Install eruby binary (example given is ubuntu/apache): sudo apt-get install ruby libapache2-mod-ruby eruby Set up your web server to serve it: AddHandler rubypage .erb .xhtml Action rubypage /cgi-bin/eruby Start writing pages like this: The time is . The first two steps could be completed by your web host, or done by you once. Obviously you might run into performance issues etc (no idea how well this would work on a…

As I recall a big issue with mod-ruby was that it used a single Ruby process for all scripts.

That meant if script A decides to rewrite, say, Array#include? to only work with symbols (or whatever), script B now has to live with it.

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

#160
post #124

Earlier quoted context omitted.

> An example of this going wrong in production, I work in the online gaming (gambling) industry. One of our users won a large jackpot, our code started a transaction, contacted the 3rd party to have them release the funds, recorded the win in the transaction journal, updated the customers balance etc. Then the stored procedure which inserted a row into the outgoing email queue had a small error. This caused all of th…

> Updating a customer's winnings and recording a win are definitely things that should be together in a transaction - if one happened and not the other, that's a failure. Yes, they these clearly do belong in the same transaction. Inserting a congratulations email into the email queue doesn't.

emails typically get fired off as part of a post-commit hook. Pyramid's transaction manager has explicit support for this pattern - as you establish "emails" within your trans, the actual send operation is deferred til after the transaction proceeds.
Post reply on HN