Underscore.py / a python port of underscore.js
serkanyersen.github.com
serkanyersen.github.com
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.
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()
- 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 :-)
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.
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.
[w.upper() for w in ["foo", "bar"]]
This underscore.py is not pythonic. Don't use it.* 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)
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".
from underscore import _ as usIs this a parody?
At least it's not php.js
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.
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.
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.
Perl - http://vti.github.com/underscore-perl/
Lua - http://mirven.github.com/underscore.lua/
PHP - http://brianhaveri.github.com/Underscore.php/
Actionscript 3 - https://github.com/amacdougall/underscore.as
Some? Pretty much all of them are already either builtins or in the stdlib…