Live data from Hacker News

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

me.veekun.com

171–180 of 196 posts

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

#171
post #156

Earlier quoted context omitted.

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…

The first poster in this thread made the statement: "If I need a quick dynamic page on my server, it's much easier to do this in PHP than in Python or Ruby."

So yes, it really does matter in the context of this thread. On the whole I agree with your philosophy that frameworks are there for a reason: you get many features for almost nothing.

But sometimes you just don't need a framework, you need something quick-and-dirty that just works.

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

#172
post #156

Earlier quoted context omitted.

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…

The first poster in this thread made the statement: "If I need a quick dynamic page on my server, it's much easier to do this in PHP than in Python or Ruby." So yes, it really does matter in the context of this thread. On the whole I agree with your philosophy that frameworks are there for a reason: you get many features for almost nothing. But sometimes you just don't need a framework, you need something quick-and-d…

But even something quick and dirty is longer than "hello world". I've written a couple pretty dinky things with Flask, and the minuscule overhead of typing `import Flask` was definitely worth having e.g. a solid template language and XSS protection ready to go.

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

#173
post #18

Earlier quoted context omitted.

What frameworks and platforms would you recommend for web development with java? I am writing my senior project at college in java (mainly because of the wide amount of image processing libraries available) and would love to add web capabilities.

I tend to prefer Python/Flask for Web dev to Python but if you are inclined to use Java there's only really one framework that doesn't make me want to tear my eyeballs out. That framework is Spring MVC. It is DI, adapters for vendor-specific libraries and utility. It's probably not the easiest thing to start with just because it is so flexible that it can be hard to know where to start. A good "Hello World" Spring MV…

"It is DI, adapters for vendor-specific libraries and utility. It's probably not the easiest thing to start with just because it is so flexible that it can be hard to know where to start. A good "Hello World" Spring MVC 3.1 app would go a long way."

Spring is OK in the service and DAO layers. I prefer Stripes on the web layer. Stripes IMO has a far more sensible approach to laying out the web layer (e.g. combining controller and backing object, fewer configurations, more sensible tag system, etc.).

http://stripesframework.org/display/stripes/Home

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

#174
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")

I guess I should have specified "where f is a file handle":

f = File.open("foo", "r") f.read

f = File.open("foo", "w+") f.write(x)

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

#175

Earlier quoted context omitted.

The transaction is internal to the database. It's essentially a list of operations to perform, if something goes wrong it provides a list of things to reverse. If the db transaction fails, then there would need to be some code to handle rolling back operations outside of the database. I don't think db transactions were to fault in your situation.

I'm not sure I really get what you are trying to say. I know what a transaction is, and I know it wasn't the fault of transactions. It was the fault of our poor use of transactions, in the same monolithic transaction pattern that the OP appears to be advocating.

It seemed you were arguing against using transactions by default because they didn't roll back a call to a web service. I meant to point out that the issue in your case seems less about db transactions and more about the code and error handling.

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

#176
post #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 b…

> a cute trick allow me to disagree about the cuteness of that. there are ways to distinguish a module from an executable file. For example, 'main' could be a special function (like in C).

> you just list it as a parameter yes, that's what I wanted to say. the fact is, in other languages I don't have to list it as a parameter and I don't have to get used to it. Personally, I find it annoying (but I could live with it, like everyone else).

> how does wrapping C functions with C names help me forgive my forward reference, but I will argue that "you're not the target audience" for this language. I found that PHP works best if you're coming from a C background and you treat it as a C-based scripting language (and this is kind of what PHP claims to be). I've always suspected that people coming from other backgrounds are the ones who will dislike PHP the most (and it doesn't surprise me), but I also don't know why they seem so vocal about it. I, for one, don't post negative comments about Ruby/Python (unless I'm comparing PHP with them) because I acknowledge that other people with, say, LISP backgrounds will find those more intuitive and will make efficient use of the them.

Anyways, cheers.

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

#178
post #141

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…

> 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): As another commenter has already posted, these are pretty trivial in Ruby and Python(and plenty of other languages). > Want to check for errors and improve the quality of the code? Go ahead! Use low…

> these are pretty trivial in Ruby and Python I wasn't arguing that they're difficult, I was saying they're quick, dirty functions you can use to get something up quickly (the RAD way).

> fwrite/fread improve quality of code? As opposed to what? as opposed to just using file_put_contents() and never checking for the number of bytes actually written. The point I was trying to make is that you could use quick, high level functions (like file_put_contents()) and then replace them with more low-level functions like fopen()/fwrite()/fclose() to have more fine-grained checking of errors (you could complain that the script failed to open the file when calling fopen() and then you could print the exact reason why, as opposed to just printing 'could not write to file' when using file_put_contents).

> How does a language using strlen equate to not getting in my way? I switch between languages pretty often and so keeping an active context for each language in my mind is rather difficult. I have to recall if it's len(), str.len(), str.length, str.length() and so on for every other type of task I need to deal with. If the language is really similar to C, it is more natural for me to use strlen() even if I have to come back to it after 6 months. That's what I meant by 'not getting in my way'.

> it doesn't mean all criticism is invalid. PHP has warts - warts that go beyond personal preferences.

I agree. No language is perfect, but why do you feel compelled to say 'PHP has warts [...] that go beyond personal preferences' without actually bringing evidence. It makes it sound like you're speaking purely out of hatred for PHP and I can't respect that.

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

#179
post #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.

But Flask is a micro framework while web2py is a full stack framework. Comparing apples to oranges IMHO.

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

#180
post #59

Earlier quoted context omitted.

> But I don't think so. One of the reasons for this is that Jakob Moss removed his web2py bashing from this thread. Didn't edit - removed it: https://www.quora.com/Is-web2py-a-good-Python-web-framework/... . What did he remove? https://www.quora.com/Is-web2py-a-good-Python-web-framework/... This isn't the first instance of Django/Flask people not agreeing with web2py design and implementation, and they have been voca…

Sorry - I remembered that I on an earlier occasion couldn't find JKM' answer. It's there now - and I personally don't find it convincing. Again - I am a novice and has no authority here. – I'd edit above if I could. I am sorry that I wrote wrong info. Thanks for the reddit link. As you say it seems to me most of the flak is from not agreeing on something. But can an oppinion be correct? And yes: Mitsuhiko seems to be…

>And yes: Mitsuhiko seems to be a super cool, clever and friendly guy. In my last project I played around with request. Lovely it was.

That would be kenneth reitz https://github.com/kennethreitz

But I do agree, mitsuhiko is a super-cool guy!

Post reply on HN