Live data from Hacker News

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

me.veekun.com

131–140 of 196 posts

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

#131
post #121

Earlier quoted context omitted.

And therein lies the problem, in WSGI 99% of web sites are hosted in "shared hosting" services Now compare copying the .php files with configuring WSGI "No, but WSGI is easy blah blah blah" it doesn't beat copying files And no, settings WSGI is not easy Also, if you depend on IIS for example, PHP is almost plug'n'play WSGI on Windows is the definition of "bag of hurt". You can try fastcgi to run on windows, and then…

FastCGI works fairly well in my experience on shared Linux servers (that's how I host my personal projects). And for smaller scripts where I don't bother setting up FCGI, I just use plain CGI, which is just copying files .

I think shared hosts have already enabled FastCGI for all php files

Something like this: http://test.fastcgi.com/drupal/node/5?q=node/10

At least the shared hosting I use (bluehost) has warned me they were enabling this. In 2007

Still, for most uses, CGI works fine.

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

#132
post #7

"MySQL is the PHP of databases". Strange, I just had this sentence popping in my head yesterday. I'm seriously considering writing a list of its wrong doings. Great article overall.

Linux is the PHP of Unixes and Apache is the PHP of webservers. And now we have covered the whole LAMP stack :)

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

#133

Earlier quoted context omitted.

Sure, if all you need is a mildly dynamic page the full blown MVC frameworks that are "standard" for other languages probably seem like bureaucracy to you. But they have their uses, and they also exist for PHP for a good reason. MVC makes massively complicated systems easier to maintain, especially if there's more than one contributor. Example: I'm responsible for maintaining a "classic" ASP project that's evolved ov…

That is of course assuming that the person who wrote the original code actually understood how to use an MVC framework. It seems to me that most peoples default assumption upon encountering one is to use the Model as a simple abstraction to the DB and stick all of the logic in the controllers (with many controller methods calling methods in other controllers).

It would help if web frameworks that have precious little to do with MVC would stop trying to piggy-back on the term. For example, if your model, view and controller have a control flow that makes them three layers in a stack, with the controller sitting between the model and the view, then your architecture is not really anything like MVC at all.

The common architectural pattern for web apps where you have a request handler that talks to a database and then generates its output by filling in a template is popular and widely applicable. However, I think the way some of these frameworks tried to hijack a well-established term for publicity in the early days has come back to haunt them now they are better known. Today, if someone wants to learn how to use these web frameworks, particularly the common underlying concepts and design skills, and searches for "MVC", they find a whole load of other information that doesn't really make sense in the context of web apps.

Similarly, anyone who really does want to learn MVC in a context where it is more applicable -- including designing larger JS-based applications that run primarily in-browser, for example -- comes up against a lot of material that is in some cases almost the antithesis of MVC, which doesn't help there either.

When the most common reasonably simple architecture beyond PHP's everything-is-a-template starts off at such a disadvantage, it's not really surprising that many people struggle to understand the basics and just stick with PHP's "it just works" instead, despite the other disadvantages they suffer in making that choice.

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

#134

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…

In Ruby: # Save a file f.write(str) # Read a file f.read # Trim whitespace " foo ".strip # Print anything puts 1 # even numbers! puts File.open("foo", "w+") # even file handles! puts [1, 2, 3, 4] # even arrays! puts [].class # even classes! puts [].method(:size) # even functions! # Dump an array (see above) Python is similarly clear: # Save a file f.write(str) # Read a file f.read() # Trim whitespace " foo ".strip()…

Not sure about the Python ones, but the Ruby read/write file examples aren't quite fully done/correct here:

  # Equivalent of file_put_contents() in PHP
  # This could also be used for appending if the correct mode is specified.
  File.open("filename", "w") { |f| f.write("some data") }

  # Equivalent of file_get_contents() in PHP
  some_var = File.read("filename")

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

#135
post #124

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…

> 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.

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

#136
post #134

Earlier quoted context omitted.

In Ruby: # Save a file f.write(str) # Read a file f.read # Trim whitespace " foo ".strip # Print anything puts 1 # even numbers! puts File.open("foo", "w+") # even file handles! puts [1, 2, 3, 4] # even arrays! puts [].class # even classes! puts [].method(:size) # even functions! # Dump an array (see above) Python is similarly clear: # Save a file f.write(str) # Read a file f.read() # Trim whitespace " foo ".strip()…

Not sure about the Python ones, but the Ruby read/write file examples aren't quite fully done/correct here: # Equivalent of file_put_contents() in PHP # This could also be used for appending if the correct mode is specified. File.open("filename", "w") { |f| f.write("some data") } # Equivalent of file_get_contents() in PHP some_var = File.read("filename")

Python:

with open("filename", "r") as a_file:

    contents = a_file.read()

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

#137
post #94

Earlier quoted context omitted.

That is obviously not sufficent. For using PHP I literally have to tell people to open an ftp GUI client and drag+drop a folder, or do something like: sftp -r website user@host: So, what is the full "this is how you use heroku" I can send to a windows user, who knows HTML and wants to just get started with 'hello world'.

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.

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

#138
post #121

Earlier quoted context omitted.

FastCGI works fairly well in my experience on shared Linux servers (that's how I host my personal projects). And for smaller scripts where I don't bother setting up FCGI, I just use plain CGI, which is just copying files .

might want to not do that... http://www.php.net/archive/2012.php#id2012-05-03-1

That only affects PHP scripts running as CGI (using a certain specific configuration), not CGI scripts in general.

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

#139
post #123
post #88

Earlier quoted context omitted.

Working with encodings is indeed annoying in Python, but it’s not a “universal” problem.

There is a language called 'Java' where every string is in unicode, and I do not remember ever running into an issue!

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.)

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

#140
post #66

Earlier quoted context omitted.

It's actually easier... $ git push heroku master

As a Mercurial user who doesn't feel like learning git just for the sake of deploying toy Python apps, can anyone comment on whether hg-git or the like is a reasonable way of deploying to heroku?

It would add a bit of overhead, but you can certainly have mercurial at the center of your workflow via gh-git, yes.
Post reply on HN