News for hackers. Stuff that matters.
Re:Underscore.py / a python port of underscore.js by serkanyersen on Sunday September 09, 2012 @09:57AM (#4495780)
Hi Everyone,

First of all, thank you all for your comments. I have to clarify the purpose of this library. I'm originally a JavaScript developer and I didn't write a single line of python code before this project.

I needed a project to learn python and I choose something I'm already familiar with. Underscore.js has almost everything you want to learn on a new language and it thought me a lot.

When I completed the project I figured I can post it online and other clueless javascript developers like myself can learn python from a familiar place.

I'm fully aware that python already has most of the tools underscore has and this code is not pythonic at all. It was built that way intentionally. I wanted it to be the same as underscore.js.

Now I need your advice to write a better python code and maybe create a useful library after all.

Thank you all.

PS: I wrote this comment from mobile phone so please excuse any errors.

Re:Underscore.py / a python port of underscore.js by anonymouz on Sunday September 09, 2012 @10:28AM (#4495855)
My personal feeling is, that you would learn more about Python by trying to write something using the standard library and embracing Python's style, instead of porting a JavaScript library.
Re:Underscore.py / a python port of underscore.js by tebeka on Sunday September 09, 2012 @10:16AM (#4495823)
Also note that _ has a meaning in the Python REPL - it's the value of the last expression.
Re:Underscore.py / a python port of underscore.js by malandrew on Sunday September 09, 2012 @03:18PM (#4496648)
_ is also used in the node.js REPL to mean the last expression. This means that you can't require() underscore.js as you normally would and must instead choose a different name.
Re:Underscore.py / a python port of underscore.js by serkanyersen on Sunday September 09, 2012 @10:21AM (#4495832)
Yes, I'm aware of this problem, underscore.php solved this using double underscores maybe I can add a noConflict method to rename the library and free up underscore variable.
Re:Underscore.py / a python port of underscore.js by jmagnusson on Sunday September 09, 2012 @07:45AM (#4495571)
Why, oh why?

map(str.upper, ['foo', 'bar']) == _(["foo", "bar"]).invoke("upper")

[w for w in ["foo", "hello", "bar", "world"] if len(w) > 3] == _.filter(["foo", "hello", "bar", "world"], lambda x, a: len(x) > 3)

sorted([i 2 for i in [10, 48, 56, 30, 20] if i > 20]) == _([10, 48, 56, 30, 20]).chain().filter(lambda x, a: x > 20).map(lambda x, a: x * 2).sortBy().value()

Re:Underscore.py / a python port of underscore.js by rmc on Sunday September 09, 2012 @08:18AM (#4495613)
I even don't like using map, pefering comprehension.

    [w.upper() for w in ["foo", "bar"]]
This underscore.py is not pythonic. Don't use it.
Re:Underscore.py / a python port of underscore.js by jmagnusson on Sunday September 09, 2012 @09:36AM (#4495737)
I completely agree with you. I only used map as he pointed out in the examples how "easy" it was to call it dynamically.
Re:Underscore.py / a python port of underscore.js by masklinn on Sunday September 09, 2012 @09:53AM (#4495771)
An other option if you use map is `operator.methodcaller`, which is also slightly closer in semantics to underscore's invoke (function name specified by string, ability to specify argument to apply to the method, duck-typed).
Re:Underscore.py / a python port of underscore.js by masklinn on Sunday September 09, 2012 @08:07AM (#4495593)
I strongly recommend not reading the source, it's worse.
Re:Underscore.py / a python port of underscore.js by serkanyersen on Sunday September 09, 2012 @10:09AM (#4495804)
It's more like a JavaScript code. I need to learn more about python to write real pythonic code. I'm open to your suggestions.
Re:Underscore.py / a python port of underscore.js by herberth-amaral on Sunday September 09, 2012 @11:31AM (#4496005)
I appreciate your effort, but there are some things that I think you need to know about Python when you come from JS world:

- List comprehensions: helps a lot.

- (str|unicode).format method (http://docs.python.org/library/string.html#format-examples) provides you a very simple templating language.

- Avoid using camelCase in variable/method/function names. Yes, I know Python itself break some of those rules, but they're trying to do it right in Python3. In fact, it'll be better if you read the PEP8 document (http://www.python.org/dev/peps/pep-0008/)

Nevertheless I would like to welcome you to Python and I hope you have a great experience with the language :-)

Re:Underscore.py / a python port of underscore.js by beambot on Sunday September 09, 2012 @11:41AM (#4496025)
Despite being a day-to-day python programmer (scientific computing with some django on the side), apparently I've been living under a rock. When did .format supersede normal printf-style string formatting, ie. "%f" % (3.14)? Is the latter now deprecated?
Re:Underscore.py / a python port of underscore.js by jspiral on Sunday September 09, 2012 @02:55PM (#4496575)
Hints have appeared suggesting that interpolation will be deprecated at some point in python 3, e.g. from this article:

http://docs.python.org/dev/py3k/whatsnew/3.0.html#changes-al...

PEP 3101: Advanced String Formatting. [...] The plan is to eventually make this the only API for string formatting, and to start deprecating the % operator in Python 3.1.

As far as I know, interpolation is not actually deprecated yet though.

Re:Underscore.py / a python port of underscore.js by masklinn on Sunday September 09, 2012 @04:47PM (#4496924)
> When did .format supersede normal printf-style string formatting

It was backported, but the superseding is mostly for Python 3.

> Is the latter now deprecated?

Not at all, and not expected to be, though str.format is recommended going forward: http://docs.python.org/py3k/library/stdtypes.html#old-string...

> As the new String Formatting syntax is more flexible and handles tuples and dictionaries naturally, it is recommended for new code. However, there are no current plans to deprecate printf-style formatting.

Re:Underscore.py / a python port of underscore.js by michaelhoffman on Sunday September 09, 2012 @12:43PM (#4496193)
I hesitate to add to the negative response to what is surely a well-meaning free project for the world. But `_` is really a poor choice for a variable name in Python. It is already used as:

* the value of the last expression in the REPL

* a conventional abbreviation for `gettext.gettext` (http://docs.python.org/library/gettext.html)

* a conventional throwaway variable (for `str.partition` and other functions that return a larger tuple than you want)

And for most of these features, invoking them through a object/class method rather than a standalone function is unwieldy (and unpythonic)

Re:Underscore.py / a python port of underscore.js by j_baker on Sunday September 09, 2012 @01:27PM (#4496323)
From the page itself:

Please keep in mind that this is just a port of a javascript library, please don't get started with the "but it's not pythonic" stuff. This library has no intentions to be pythonic, infact it tries to bring same underscore experience from javascript to python.

You have to give the author credit for his candor at least.

That said, I like that the author of this is trying new things out. We can't let Python become petrified by rigid definitions of "pythonic".

Re:Underscore.py / a python port of underscore.js by sitkack on Sunday September 09, 2012 @01:36PM (#4496348)
Only by being constructively critically reflective can we continue to grow. Pythonic is bandied about like a hammer. If people understood the intent of _pythonic_ I think they wouldn't use the term unpythonic so loosely.
Re:Underscore.py / a python port of underscore.js by sitkack on Sunday September 09, 2012 @01:31PM (#4496339)

    from underscore import _ as us
Re:Underscore.py / a python port of underscore.js by djacobs on Sunday September 09, 2012 @09:32AM (#4495727)
It is hilarious that someone would have the knowledge to build this but not know that it was already implemented as part of the language.

Is this a parody?

Re:Underscore.py / a python port of underscore.js by serkanyersen on Sunday September 09, 2012 @10:07AM (#4495798)
I posted a comment to clear this up. Sorry for the confusion.
Re:Underscore.py / a python port of underscore.js by StavrosK on Sunday September 09, 2012 @01:10PM (#4496268)
I'm sorry about all the negativity. Python includes all these things, sure, and the code is not great, but it's a learning project. Can we treat it as such and not criticize it so much?
Re:Underscore.py / a python port of underscore.js by djacobs on Sunday September 09, 2012 @03:01PM (#4496598)
I'm not sure the responses are expressing negativity as much as they are expressing confusion. This was a learning project, sure, but it was also posted to Hacker News (and without that caveat).
Re:Underscore.py / a python port of underscore.js by masklinn on Sunday September 09, 2012 @04:49PM (#4496935)
> Is this a parody?

At least it's not php.js

Re:Underscore.py / a python port of underscore.js by gbin on Sunday September 09, 2012 @09:46AM (#4495762)
+1 This is all standard python, I don't get it
Re:Underscore.py / a python port of underscore.js by stephen_mcd on Sunday September 09, 2012 @07:38AM (#4495561)
Most of the methods provided are functions already built into Python (map, filter, reduce, all, any, min, max)

Also the underscore variable name conflicts with the common convention of using an underscore as the i18n translation function.

Those issues aside, the templating looks handy, and I'm sure the whole thing was fun to write.

Re:Underscore.py / a python port of underscore.js by FuzzyDunlop on Sunday September 09, 2012 @09:05AM (#4495682)

    VERSION = "0.1.2"
    """
    Version of the library
    """
This is a good example of unnecessary commenting.

But I think this port misses the point of Underscore.js, which is to make the same functional features of the language available to all browser implementations, because not all browsers have forEach, or filter, or reduce, etc. In Python, or other languages? The features are already there.

Re:Underscore.py / a python port of underscore.js by serkanyersen on Sunday September 09, 2012 @10:39AM (#4495890)
Haha :) I don't know what I was thinking by putting that comment.

Underscore.py is not trying to fill any gap in Python. It's just a port. Maybe a useful collection of tools. But I agree it's mostly pointless when there are itertools and functools.

Re:Underscore.py / a python port of underscore.js by draegtun on Sunday September 09, 2012 @01:39PM (#4496356)
Re:Underscore.py / a python port of underscore.js by borntyping on Sunday September 09, 2012 @09:29AM (#4495719)
There is absolutely no reason to use this. As far as I can tell, it's a direct port of underscore.js, and implements nothing but functionality that's already in python.
Re:Underscore.py / a python port of underscore.js by serkanyersen on Sunday September 09, 2012 @10:11AM (#4495809)
I should put this on the project description :) you are exactly right.
Re:Underscore.py / a python port of underscore.js by yoduh on Sunday September 09, 2012 @04:21PM (#4496862)
My opinion: Anytime you share code by posting it publicly, you should always be prepared for negative feedback. In some cases that might be the whole point of posting it: you want to learn from your mistakes.
Re:Underscore.py / a python port of underscore.js by why-el on Sunday September 09, 2012 @08:37AM (#4495638)
Good job! I know that some of these functions maybe have already occurred to Python developers (map et al), but this probably lead you to a broader understanding of underscore.js itself.
Re:Underscore.py / a python port of underscore.js by serkanyersen on Sunday September 09, 2012 @10:13AM (#4495814)
Thanks. Only purpose was to learn python and I also get a very good understanding of underscore.js and it was pretty fun to work on this.
Re:Underscore.py / a python port of underscore.js by masklinn on Sunday September 09, 2012 @09:56AM (#4495777)
> I know that some of these functions maybe have already occurred to Python developers

Some? Pretty much all of them are already either builtins or in the stdlib…

Re:Underscore.py / a python port of underscore.js by why-el on Sunday September 09, 2012 @11:52AM (#4496049)
I am pretty sure they are. The "some" is due to my lack of Python knowledge. :)
Re:Underscore.py / a python port of underscore.js by mark_story on Sunday September 09, 2012 @09:17AM (#4495704)
While I can understand this might have been fun from a hacking/building experience, I don't understand why do it. Everything except the micro templating already exists in python.
Re:Underscore.py / a python port of underscore.js by victorlin on Sunday September 09, 2012 @09:34AM (#4495730)
This doesn't bring anything good to Python. Python can do those things nicely. I can't see any point of this library. Also, underscore already has its meaning in some places, such as for i18n. For example, _('Text to be localized'). Another use of underscore is to store the previous output in console. Although you can rename it, still, it's pointless.
Re:Underscore.py / a python port of underscore.js by sontek on Sunday September 09, 2012 @09:48AM (#4495764)
I think the biggest benefit of this is having the template language ported to python so you can use the same templates server side or client side.
Re:Underscore.py / a python port of underscore.js by apendleton on Sunday September 09, 2012 @01:11PM (#4496271)
This template language is only superficially the same, but the code between the <% and %> would need to be python in this version, not JS, so you can't actually share anything. You would need to combine something like this with a JS-to-Python transpiler for that to work.
Re:Underscore.py / a python port of underscore.js by cpylua on Sunday September 09, 2012 @08:24AM (#4495621)
Collection functions are not that useful in Python. For Function functions I'd prefer using decorators.
Re:Underscore.py / a python port of underscore.js by sophacles on Sunday September 09, 2012 @01:24PM (#4496316)
How are collection functions not useful in python? I use map and sort (or the comprehension equivalent) daily. The rest I use at least once a week, with the possible exception of groupBy - as my workload doesn't really need it very often.
Re:Underscore.py / a python port of underscore.js by masklinn on Sunday September 09, 2012 @04:51PM (#4496940)
I think he means the underscore collection functions are not very useful because for the most part they already exist in Python.