Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 105 lines (87 sloc) 3.5 kb
293a9bb @serkanyersen Change readme file name
authored
1 __
2 /\ \
3 __ __ ___ \_\ \ __ _ __ ____ ___ ___ _ __ __ _____ __ __
4 /\ \/\ \ /' _ `\ /'_` \ /'__`\/\`'__\/',__\ /'___\ / __`\/\`'__\/'__`\ /\ '__`\/\ \/\ \
5 \ \ \_\ \/\ \/\ \/\ \_\ \/\ __/\ \ \//\__, `\/\ \__//\ \_\ \ \ \//\ __/ _\ \ \_\ \ \ \_\ \
6 \ \____/\ \_\ \_\ \___,_\ \____\\ \_\\/\____/\ \____\ \____/\ \_\\ \____\/\_\ \ ,__/\/`____ \
7 \/___/ \/_/\/_/\/__,_ /\/____/ \/_/ \/___/ \/____/\/___/ \/_/ \/____/\/_/\ \ \/ `/___/> \
8 \ \_\ /\___/
9 \/_/ \/__/
10
11 Underscore.py is a python port of excellent javascript library underscore.js
12
13 **What is underscore.js?**
14
15 From underscore page: Underscore.js is a utility-belt library for JavaScript that provides support for the
16 usual functional suspects (each, map, reduce, filter...) without extending any core JavaScript objects.
17
18 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.
19
d526fd3 @serkanyersen Bump version and add travis build status to readme
authored
20 [![Build Status](https://travis-ci.org/serkanyersen/underscore.py.png?branch=master)](https://travis-ci.org/serkanyersen/underscore.py)
21
293a9bb @serkanyersen Change readme file name
authored
22 ## Installing
23
24 Install from pypi
25 ```bash
26 pip install underscore.py
27 ```
28 **or**
29
30 Clone the repository:
31 ```bash
32 git clone git://github.com/serkanyersen/underscore.py.git
33 ```
34 Get into underscore.py directory
35 ```bash
36 cd underscore.py
37 ```
38 Run setup script
39 ```bash
40 sudo python setup.py install
41 ```
42 That's it
43
44 ## Usage
45 Import underscore to your project
46 ```python
47 from underscore import _
48 ```
49 or if you don't want to mess with _ variable
50 ```python
51 from underscore import _ as us # You can use any name you want, __ or u
52 ```
53
54 ## Use it just like javascript version
55 ```python
56 # Dynamically
57 _(["foo", "bar"]).invoke("upper") # ["FOO", "BAR"]
58 # or statically
59 _.filter(["foo", "hello", "bar", "world"], lambda x, *a: len(x) > 3) # ["hello", "world"]
60 # Do chaining
61 _([10, 48, 56, 30, 20]).chain().filter(lambda x, *a: x > 20).map(lambda x, *a: x * 2).sortBy().value()
62 # [60, 96, 112]
63 ```
64
65 ## Full micro templating support
66 ```python
67 tmpl = _.template("Name: <% if prefix: %><%= prefix %>. <% endif %><%= name %>\n\
68 Last Name: <%=lname.upper() %>\n\
69 <% if email: %>\
70 E-mail: <%= email %>\n\
71 <% endif %>")
72
73 people = [{
74 "prefix": "",
75 "name": "John",
76 "lname": "Doe",
77 "email": "johndoe@example.com"
78 },{
79 "prefix": "Mr",
80 "name": "James",
81 "lname": "Brown",
82 "email": "james@brown.net"
83 }]
84
85 for person in people:
86 print tmpl(person)
87 ```
88 Output
89
90 Name: John
91 Last Name: DOE
92 E-mail: johndoe@example.com
93 Name: Mr. James
94 Last Name: BROWN
95 E-mail: james@brown.net
96
97 For more information and documentation [underscorejs.org](http://underscorejs.org)
98
99 Original Source: [Underscore.js on Github](https://github.com/documentcloud/underscore)
100
101 ### Disclaimer
102 Please keep in mind that this is a direct port of a javascript library, so don't get started with
103 the "but it's not pythonic" stuff. This library has no intentions to be pythonic, infact it tries to
104 bring the same underscore experience from javascript to python
Something went wrong with that request. Please try again.