News for hackers. Stuff that matters.
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.