up

underscore.py

Python port of underscore.js

Showing:

Popularity

Downloads/wk

0

GitHub Stars

278

Maintenance

Last Commit

3yrs ago

Contributors

5

Package

Dependencies

0

License

UNKNOWN

Categories

Readme

/\ \ _ _\ \ _ __ _ _ _ ___ _ __ /\ \/\ \ /' \ /'_ \ /'\/\'\/',\ /'_\ / \/\'\/'`\ /\ '\/\ \/\ \ \ \ \_\ \/\ \/\ \/\ \_\ \/\ __/\ \ \//\__, \/\ _//\ _\ \ \ \//\ __/ \ \ _\ \ \ _\ \ \ __/\ _\ _\ _,_\ __\ _\/__/\ __\ __/\ _\ __\/_\ \ ,/\/____ \ \/___/ \/_/\/_/\/__,_ /\/____/ \/_/ \/___/ \/____/\/___/ \/_/ \/____/\/_/\ \ \/ /_/> \ \ _\ /_/ \/_/ \/__/

Underscore.py is a python port of excellent javascript library underscore.js

What is underscore.js?

From underscore page: Underscore.js is a utility-belt library for JavaScript that provides support for the
usual functional suspects (each, map, reduce, filter...) without extending any core JavaScript objects.

NOTE: It's obvious that python already has nearly all features of underscore library built-in. I'm not trying to fill any gap in python. If you are coming from JavaScript this library will provide you a familiar interface, a set of tools you already know how to use and micro templating functionality. Use it wisely and try to use built-in functions wherever possible.

Build Status

Installing

Install from pypi

pip install underscore.py

or

Clone the repository:

git clone git://github.com/serkanyersen/underscore.py.git

Get into underscore.py directory

cd underscore.py

Run setup script

sudo python setup.py install

That's it

Usage

Import underscore to your project

from underscore import _

or if you don't want to mess with _ variable

from underscore import _ as us  # You can use any name you want, __ or u

Use it just like javascript version

# Dynamically
_(["foo", "bar"]).invoke("upper")  # ["FOO", "BAR"]
# or statically
_.filter(["foo", "hello", "bar", "world"], lambda x, *a: len(x) > 3)  # ["hello", "world"]
# Do chaining
_([10, 48, 56, 30, 20]).chain().filter(lambda x, *a: x > 20).map(lambda x, *a: x * 2).sortBy().value()
# [60, 96, 112]

Full micro templating support

tmpl = _.template("Name: <% if prefix: %><%= prefix %>. <% endif %><%= name %>\n\
Last Name: <%=lname.upper() %>\n\
<% if email: %>\
E-mail: <%= email %>\n\
<% endif %>")

people = [{
  "prefix": "",
  "name": "John",
  "lname": "Doe",
  "email": "johndoe@example.com"
},{
  "prefix": "Mr",
  "name": "James",
  "lname": "Brown",
  "email": "james@brown.net"
}]

for person in people:
  print tmpl(person)

Output

Name: John
Last Name: DOE
E-mail: johndoe@example.com
Name: Mr. James
Last Name: BROWN
E-mail: james@brown.net

For more information and documentation underscorejs.org

Original Source: Underscore.js on Github

Disclaimer

Please keep in mind that this is a direct port of a javascript library, so 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 the same underscore experience from javascript to python

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100