Skip to main content

Lodash methods in Python.

Project description

Lodash.py

Python

Lodash methods in Python.

Install with pip

$ pip install lodash-py

Usage

# From lodash_py modules import Lodash
from lodash_py import Lodash

# Create instance of Lodash
_ = Lodash()

print(_.find_index(users, lambda o: o["user"] == "barney"))
# 0

List Methods

_.compact

Creates an list with all falsey values removed. The values False, None, 0 and "" are falsey.

print(_.compact([0, 1, False, 2, '', 3, None]))
# [1, 2, 3]

_.drop

Creates a slice of list with n elements dropped from the beginning.

print(_.drop([1, 2, 3], 2))
# [3]
print(_.drop([1, 2, 3], 5))
# []
print(_.drop([1, 2, 3], 0))
# [1, 2, 3]

_.drop_right

Creates a slice of list with n elements dropped from the end.

print(_.drop_right([1, 2, 3]))
# [1, 2]
print(_.drop_right([1, 2, 3], 2))
# [1]
print(_.drop_right([1, 2, 3], 5))
# []
print(_.drop_right([1, 2, 3], 0))
# [1, 2, 3]

_.find_index

Returns the index of the found element, else -1.

users = [
  { 'user': 'Benson',  'active': False },
  { 'user': 'Frank',    'active': False },
  { 'user': 'Erik', 'active': True }
]

print(_.find_index(users, lambda o: o["user"] == "Benson"))
# 0
print(_.find_index(users, { 'user': 'Frank', 'active': False }))
# 1
print(_.find_index(users, ['active', False]))
# 0
print(_.find_index(users, 'active'))
# 2

_.from_pairs

This method returns an object composed from key-value pairs.

print(_.from_pairs([['a', 1], ['b', 2]]))
# {'a': 1, 'b': 2}

_.head

Returns the first element of list.

print(_.head([1, 2, 3]))
# 1
print(_.head([])) 
# None

_.initial

Gets all but the last element of list.

print(_.initial([1,2,3]))
# [1, 2]

_.join

Returns the joined string.

print(_.join(["a", "b", "c"], "~"))
# 'a~b~c'
print(_.join(["a", "b", "c", "d"], "@"))
# 'a@b@c@d'

_.pull

Removes all given values from list.

print(_.pull(['a', 'b', 'c', 'a', 'b', 'c'], 'a', 'c'))
# ['b', 'b']

_.pull_all

This method is like _.pull except that it accepts a list of values to remove.

print(_.pull_all(['a', 'b', 'c', 'a', 'b', 'c'], ['a', 'c']))
# ['b', 'b']

_.pull_at

Removes elements from list corresponding to indexes and returns a list of removed elements.

list_example = ['a', 'b', 'c', 'd']
pulled = _.pull_at(list_example, [1, 3]);

print(list_example)
# ['a', 'c']
print(pulled)
# ['b', 'd']

_.last

Returns the last element of list.

print(_.last([1, 2, 3]))
# 3

_.take

Creates a slice of list with n elements taken from the beginning.

print(_.take([1, 2, 3]))
# [1]
print(_.take([1, 2, 3], 2))
# [1, 2]
print(_.take([1, 2, 3], 5))
# [1, 2, 3]
print(_.take([1, 2, 3], 0))
# [0]

_.tail

Gets all but the first element of list.

print(_.tail([1, 2, 3, 4]))
# [2, 3, 4]

_.uniq

Creates a duplicate-free version of an list.

print(_.uniq([2, 1, 2]))
# [1, 2]
print(_.uniq([3, 3, 4, 4, 5, 6, 6, 7]))
# [3, 4, 5, 6, 7]

_.xor

Creates an list of unique values that is the symmetric difference of the given list. The order of result values is determined by the order they occur in the list.

print(_.xor([2, 1], [2, 3]))
# [1, 3]

Lang Methods

_.cast_list

Casts value as an list

print(_.cast_list(1))
# [1]
print(_.cast_list({ 'a': 1 }))
# [{'a': 1}]
print(_.cast_list('abc'))
# ['abc']
print(_.cast_list())
# []

_.comforms_to

Checks if dict_input conforms to source by invoking the predicate properties of source with the corresponding property values of dict_input.

dict_example = { 'a': 1, 'b': 2 };

print(_.conforms_to(dict_example, { "b":lambda n : n > 1}))
# True
print(_.conforms_to(dict_example, { "b":lambda n : n > 2}))
# False

_.gt

Returns true if value is greater than other, else false.

print(_.gt(3, 1))
# True
print(_.gt(3, 3))
# False
print(_.gt(1, 3))
# False

_.gte

Returns true if value is greater than or equal to other, else false.

print(_.gte(3, 1))
# True
print(_.gte(3, 3))
# True
print(_.gte(1, 3))
# False

_.is_list

Checks if value is classified as a List.

print(_.is_list([1, 2, 3]))
# True
print(_.is_list("abc"))
# False
print(_.is_list(None))
# False
print(_.is_list(12))
# False
print(_.is_list({"a": 12}))
# False

Math Methods

_.add

Adds two numbers.

print(_.add(6, 4))
# 10

_.divide

Divide two numbers.

print(_.divide(6, 4))
# 1.5

_.max

Computes the maximum value of list.

print(_.max([4, 2, 8, 6]))
# 8

_.min

Computes the minimum value of list.

print(_.min([4, 2, 8, 6]))
# 2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lodash-py-0.0.4.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lodash_py-0.0.4-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file lodash-py-0.0.4.tar.gz.

File metadata

  • Download URL: lodash-py-0.0.4.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.8

File hashes

Hashes for lodash-py-0.0.4.tar.gz
Algorithm Hash digest
SHA256 159a2256b5051c989f66da3059697cc5a8afe8363289ae9e1deafe92b8a8ce63
MD5 216fc99436d82784ee5d078dac533afb
BLAKE2b-256 d707cb834506fae280e4c19b55503119917dddeca2b70913fbb13087a152c7d6

See more details on using hashes here.

File details

Details for the file lodash_py-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: lodash_py-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.8

File hashes

Hashes for lodash_py-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3f76c37678bf474a2e516d9e51dd6dcd0137a44805fb3acd5514755a740141a7
MD5 b70a54950bce1b36b48e0aa30e463fa4
BLAKE2b-256 216acc3a387076ec5f53e087f4a73d18c4bae27b02f2a67e5f8457632e078c7c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page