Giter VIP home page Giter VIP logo

underscore.py's Introduction

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

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": "[email protected]"
},{
  "prefix": "Mr",
  "name": "James",
  "lname": "Brown",
  "email": "[email protected]"
}]

for person in people:
  print tmpl(person)

Output

Name: John
Last Name: DOE
E-mail: [email protected]
Name: Mr. James
Last Name: BROWN
E-mail: [email protected]

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

underscore.py's People

Contributors

jklmli avatar roryqueue avatar serkanyersen avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.