Python alternatives for PHP functions
php2python.com
php2python.com
nl2br is a useful function. People use it. If you don't need it, don't use it. How calling it names is helpful to anything? You didn't even bother to explain why "the PHP function was a bad idea to begin with" or why it's a "mess" or why Python people would never need it. But your certainly found time to call it "dumb shit". Here comes the downvote.
(Taken from https://github.com/php/php-src/blob/PHP-5.4.9/ext/standard/s... )
Seems to do what it says on the tin.
Plus the actual HTML-escaping tools (htmlspecialchars, htmlentities) do not make whitespace significant.
Though these days, you might arguably be better off with "white-space: pre-line" in CSS instead.
It's only safe and reliable as a part of nl2br(htmlspecialchars()) combo, so a function that does both could have been a better idea.
The function never purports to do anything other than convert newlines to BR tags.
I don't see how that's implied at all. After all, the function is named nl2br, not html2text.
I agree the function does exactly what it says it will do. And if this was a private function used by something like text2html internally, then maybe it might be a fine function. However, as a public function, the argument is that it inspires bad programming practices, since again, it is almost certainly being used as a primitive form of "sanitation" or "conversion" before displaying plaintext in a larger HTML document.
I think if you could come up with an example of how this would be used NOT as an immediate precursor to dropping into HTML I could be convinced otherwise (and saying it is used after the other tags go through a sanitation process is a poor response, since it means this function must always follow the other one -- further proving its uselessness as a standalone function).
I think map() from Python should be removed. Its name implies to a new learner that it will draw a map, but it actually does nothing to that effect at all! No, it maps an array to a function. We must rename this dangerous function to call_a_function_on_every_element_of_an_array - or, even better, remove it from the language core ENTIRELY. If it was a private function used inside the runtime, maybe that would be fine, but it's a public part of the API.
There is also no mention in the manual that it is unsafe! One of the big problems with PHP is how easy it is to write dangerous code and how the standard manuals and tutorials often give little explanation to this.
[1] as if they would read it...
> I don't see how that's implied at all. After all, the function is named nl2br, not html2text.
Absolutely every example from the documentation http://php.net/manual/en/function.nl2br.php uses it exactly in this manner: taking the output and immediately outputting it to the resultant HTML document. I've already described why this is unsafe (take any of these examples, replace the string with something like "Everyone knows 4 < 5", and it breaks the document due to the inclusion of "special" characters).
Now you feel that the correct use of this function is so obvious that it merits mocking my belief that it may be misunderstood by users (despite the comments on that very documentation page describing how they use it as a simple text to html converter). So given that it is so obvious to you, I repeat my original request: just give me an example where nl2br isn't ultimately used to transform plaintext before outputting it to HTML.
Also, your criticism of map() is kind of childish. It doesn't imply to a new learner that they will draw a map, nor does the documentation even hint at anything like that. In the Python documentation, they are given a clear use case and, if they are familiar with programming (or linguistics), understand that usage of the word map as a verb. Don't be obtuse about PHP's bad documentation.
Removing it would simply cause newbies to have to wrange with str_replace to build trivial web apps.
Prior to 4.0.5, it used "<br>". As of 4.0.5, they switched to "<br />". (As of 5.3.0, they did the obvious thing and added a second parameter, is_xhtml).
This isn't an isolated incident -- any minor update is liable to change how a function works or what parameters it can take. So you're better off writing it yourself (or doing it inline with a string replace or regular expression).
EDIT: You want to talk about dumb. An incomplete object model that has no concept of protected members and doesn't enforce encapsulation on "private" ones. This is worse than PHP4 and pales in comparison with PHP5's object model.
I have fairly recently written code in both Python and Ruby, and of the many qualitative differences in how these cultures and languages influence projects written in them, it has never occurred to me that 'real' privacy in Ruby vs. convention-based privacy in Python was the cause of any noticeable difference.
Which is perfectly fine if you don't need encapsulation.
The shortest answer is that it changes how people read the program and treat symbols branded with the underscore.
Empirically this has not caused rampant abuses or even unintentional mistakes of abuse of otherwise internal members, and so I think without more evidence that it's causing a problem now that this design experiment -- ill advised or otherwise -- has been tried and seems quite successful. Hence, an appeal to philosophy is not at odds with the implementation. The simple rule is "don't do that," coupled with "and it should be obvious when you are." Just as you probably shouldn't break into another class via reflection to use its symbols, as seen in Java or .NET, or use .send in Ruby, but still can. Python opted -- mostly for reasons of implementation complexity reduction -- to just do nothing at all.
I think this viewpoint changes quite a bit in a language that is amenable to being statically analyzed, though.
Turns out it's much more than that. Rather than handing out ready-made functions to make Python behave like PHP, this site actually teaches you how to write Python like a real Pythonista. For example, str[2:5] instead of substr(str, 2, 3). Well done!
Not in my opinion. Here is my personal experience of using this site when I was just starting with Python. I had landed up there looking for a function similar to PHP's `array_fill_keys`. And it showed me `dict.fromkeys(keys, value)`[1] without a slightest of warning that unexpected things will happen if its passed a mutable data structure such as an empty list as the value.
So IMO, for people coming from PHP background writing some of their first Python code, it can be helpful at times but not an alternative to learning Python from a book or some serious online resource. Moreover, the site itself says that it won't teach you either PHP or Python.[2]
[1]: http://www.php2python.com/wiki/function.array-fill-keys/ [2]: http://www.php2python.com/about/
# before generator expressions (generates intermediate list)
result = dict([ (k, value) for k in keys ])
# with generator expressions (lazy iteration)
result = dict((k, value) for k in keys)
# with dict comprehension (brand new, probably the fastest)
result = { k: value for k in keys } d = dict.fromkeys(keys, value)
is fine if value is immutable e.g., a string, number.Your code doesn't solve the mutability problem (each value is the exact same object. If you modify it for one key; the values are modified for all keys).
For a mutable type you need to create a new value for each key:
d = {k: [] for k in keys} dict((k, []) for k in keys)
Although I later realized what I really needed was defaultdict[1] d = defaultdict(list)
[1]: http://docs.python.org/2/library/collections.html#collection...I know that, but I willfully replicated the original code behavior and made it idiomatic (which has the advantage of making it both obvious and easily adjustable)
Edit: I am wrong, not everyone hates PHP.
It might not be the most popular language around the HN crowd, but this forum is subject to trends and "emerging technologies" - not necessarily to say that these technologies are better or worse, just that they are the flavor of the month.
It's hard to argue with the numbers, PHP is still the most popularly used language [1] and while the gap is being bridged, it is really minuscule in comparison. I'm sure that while you may hate PHP, not every single developer does.
Yes, yes, down vote me, I'm a proud PHP developer and to even utter such words on this site seems to result in the most powerful of criticisms, but subjective to this entire argument is that behind every person who writes PHP is a ruby coder, tutting and shaking his head, stood behind him is a Node.js developer, laughing at him, behind him is somebody writing Assembly, scowling in to the distance in disgust, behind him is a C coder, eyes wide with surprise that someone is working in Assembly, etc.
I give it a few years until people are crying out loud on here that Node.js is dead and superlanguageemulator.pm or whatever comes next is the only way to code, at least, for people wise and intelligent enough to embrace this new technology.
[1] http://w3techs.com/technologies/overview/programming_languag...
They don't say how they determine whether a technology is being used server side -- it could be only if they see ".php" URLs on the site, for example, which would mean there would be a strong bias against languages that make it easy to write sane URLs.
They also don't share what % of sites they're able to make a determination for at all, so these numbers could be based on a statistically insignificant sample.
JS is on the rise, but no one hires anyone to write just JS. It's usually JS and PHP (and of course, HTML, CSS).
And another point, outside the bubble (I mean a place like Florida) it's hella easier finding a decent PHP guy than Python (or Ruby) guy. I learned that the hard way.
The job market in CA is very different to pretty much everywhere else in that emerging and niche technologies tend to be under-represented outside of CA, most likely because of budget constraints in web development, most clients want a simple Joomla or WordPress site and couldn't care less about anything else as long as they don't have to pay over £2,000 for it.
I've never received offers for anything else.
You are joking. Right?
There are many good job opportunities for JS developers. I don't know where you got the idea otherwise.
FWIW, I was hired to do just JS.
This is false. It is very easy to make sane URLs in PHP. You just designate index.php (or whatever you want, actually) as the default index in your http server. In fact, that's how it works in every language?
You think PHP is short because you have functions like xmlrpc_encode_request and the equivalent in Python is 20 lines. What you don't see if that xmlrpc_encode_request is pages and pages of C code and PHP macro's behind the scenes. That function should be in a library, not in a programming language, which is why PHP is more comparable to other web frameworks, not to other programming languages.
I realize this is beside the point, but I'm willing to fix it myself.
It just doesn't have xmlrpc_encode_request() with the exact method signature.
P.S.: I am wondering if it is normal to have functions marked as EXPERIMENTAL with a biiiig warning sign for 11 years in the language? That really scares me. This function has been introduced in PHP 4.1.0 which was releases in 10-Dec-2001... That's just...
edit: removed rant about xmlrpc_encode_request being built-in. It's a non-default extension, apparently..
The reason the examples appear longer is because the website is documenting how to implement PHP functions in Python. Since Python is not PHP, it won't implement line-for-line replacement functions.
The examples here are kind of disingenuous to that, but if you start writing code that takes advantage of the features of Python, your lines of code should be less than PHP or equal.
http://code.google.com/p/php-py/source/browse/trunk/src/php/...
Actually had one myself yesterday. Took me seconds to fix and was notable only because it was such a rare event.
Basically, my windows install of Vim and my *nix install are not using the same whitespace settings, and I sometimes have to fix a bunch of lines if I wrote them first in windows.
It happens infrequently enough that I haven't bothered to figure out exactly what the problem is, but I've been coding python for years without drilling this one down.
If you're getting code from someone else in your company which doesn't, just hit them round the head with PEP8 until they learn.