Skip to main content

A modern Python utility library delivering modularity, performance & extras.

Project description

Python Lodash

pylodash is collection utilities allow you working with arrays, maths, number and string

Installation

You can install the Pylodash from PyPI:

pip install pylodash

Pylodash is supported on Python 3.4 and above.

How to use methods

You can call the Pylodash in your own Python code, by importing the pylodash package:

>>> import pylodash as _
>>> _.chunk(['a', 'b' , 'c', 'd'], 2)
// => [['a', 'b'], ['c', 'd']]

Pylodash Methods

  1. Arrays

    • chunk()

      • Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
    • compact()

      • Creates an array with all falsey values removed. The values False, None, 0 and "" are falsey.
    • difference()

      • Creates an array of array values not included in the other given arrays using SameValueZero for equality comparisons. The order and references of result values are determined by the first array.
    • drop()

      • Creates a slice of array with n elements dropped from the beginning.
    • drop_right()

      • Creates a slice of array with n elements dropped from the end.
    • fill()

      • Fills elements of array with value from start up to, but not including, end.
    • index_of()

      • Gets the index at which the first occurrence of value is found in array using SameValueZero for equality comparisons. If fromIndex is negative, it's used as the offset from the end of array.
    • initial()

      • Gets all but the last element of array.
    • pull()

      • Removes all given values from array using SameValueZero for equality comparisons.
  2. Maths

    • add()

      • Adds two numbers.
    • ceil()

      • Computes number rounded up to precision.
    • divide()

      • Divide two numbers.
    • floor()

      • Computes number rounded down to precision.
    • max()

      • Computes the maximum value of array. If array is empty or falsey, [] is returned.
    • mean()

      • Computes the mean of the values in array.
    • min()

      • Computes the minimum value of array. If array is empty or falsey, [] is returned.
    • multiply()

      • Multiply two numbers.
    • substract()

      • Subtract two numbers.
    • sum()

      • Computes the sum of the values in array.
  3. Number

    • clamp()

      • Clamps number within the inclusive lower and upper bounds.
    • in_range()

      • Checks if n is between start and up to, but not including, end. If end is not specified, it's set to start with start then set to 0. If start is greater than end the params are swapped to support negative ranges.
    • random()

      • Produces a random number between the inclusive lower and upper bounds. If only one argument is provided a number between 0 and the given number is returned. If floating is true, or either lower or upper are floats, a floating-point number is returned instead of an integer.
  4. String

    • camel_case()

      • Converts string to camel case.
    • capitalize()

      • Converts the first character of string to upper case and the remaining to lower case.
    • ends_with()

      • Checks if string ends with the given target string.
    • escape()

      • Converts the characters "&", "<", ">", '"', and "'" in string to their corresponding HTML entities.
    • lower_case()

      • Converts string, as space separated words, to lower case.
    • lower_first()

      • Converts the first character of string to lower case.
    • pad()

      • Pads string on the left and right sides if it's shorter than length. Padding characters are truncated if they can't be evenly divided by length.
    • pad_end()

      • Pads string on the right side if it's shorter than length. Padding characters are truncated if they exceed length.
    • pad_start()

      • Pads string on the left side if it's shorter than length. Padding characters are truncated if they exceed length.
    • repeat()

      • Repeats the given string n times.
    • replace()

      • Replaces matches for pattern in string with replacement.
    • starts_with()

      • Checks if string starts with the given target string.

List methods in packages

"Arrays" Methods

_.chunk(array, [size=1])

Example

_.chunk(['a', 'b', 'c', 'd'], 2)
// => [['a', 'b'], ['c', 'd']]
 
_.chunk(['a', 'b', 'c', 'd'], 3)
// => [['a', 'b', 'c'], ['d']]

_.compact(array)

Example

_.compact([0, 1, False, 2, '', 3])
// => [1, 2, 3]

_.difference(array, [values])

Example

_.difference([2, 1], [2, 3])
// => [1]

_.drop(array, [n=1])

Example

_.drop([1, 2, 3])
// => [2, 3]
 
_.drop([1, 2, 3], 2)
// => [3]
 
_.drop([1, 2, 3], 5)
// => []
 
_.drop([1, 2, 3], 0)
// => [1, 2, 3]

_.drop_right(array, [n=1])

Example

_.drop_right([1, 2, 3])
// => [1, 2]
 
_.drop_right([1, 2, 3], 2)
// => [1]
 
_.drop_right([1, 2, 3], 5)
// => []
 
_.drop_right([1, 2, 3], 0)
// => [1, 2, 3]

_.fill(array, value, [start=0])

Example

array = [1, 2, 3]
 
_.fill(array, 'a')
// => ['a', 'a', 'a']
 
_.fill(Array(3), 2)
// => [2, 2, 2]
 
_.fill([4, 6, 8, 10], '*', 1, 3)
// => [4, '*', '*', 10]

_.index_of(array, value, [fromIndex=0])

Example

_.index_of([1, 2, 1, 2], 2)
// => 1
 
// Search from the `fromIndex`.
_.index_of([1, 2, 1, 2], 2, 2)
// => 3

_.initial(array)

Example

_.initial([1, 2, 3])
// => [1, 2]

_.pull(array, [values])

Example

array = ['a', 'b', 'c', 'a', 'b', 'c']
 
_.pull(array, 'a', 'c')
// => ['b', 'b']

"Maths" Methods

_.add(augend, addend)

Example

_.add(6, 4)
// => 10

_.ceil(number, [precision=0])

Example

_.ceil(4.006)
// => 5
 
_.ceil(6.004, 2)
// => 6.01
 
_.ceil(6040, -2)
// => 6100

_.divide(dividend, divisor)

Example

_.divide(6, 4)
// => 1.5

_.floor(number, [precision=0])

Example

_.floor(4.006)
// => 4
 
_.floor(0.046, 2)
// => 0.04
 
_.floor(4060, -2)
// => 4000

_.max(array)

Example

_.max([4, 2, 8, 6])
// => 8
 
_.max([]);
// => undefined

_.mean(array)

Example

_.mean([4, 2, 8, 6]);
// => 5

_.min(array)

Example

_.min([4, 2, 8, 6]);
// => 2
 
_.min([]);
// => undefined

_.multiply(multiplier, multiplicand)

Example

_.multiply(6, 4)
// => 24

_.round(number, [precision=0])

Example

_.round(4.006)
// => 4
 
_.round(4.006, 2)
// => 4.01
 
_.round(4060, -2)
// => 4100

_.subtract(minuend, subtrahend)

Example

_.subtract(6, 4)
// => 2

_.sum(array)

Example

_.sum([4, 2, 8, 6])
// => 20

"Number" Methods

_.clamp(number, [lower], upper)

Example

_.clamp(-10, -5, 5)
// => -5
 
_.clamp(10, -5, 5)
// => 5

_.in_range(number, [start=0], end)

Example

_.in_range(3, 2, 4)
// => True
 
_.in_range(4, 8)
// => True
 
_.in_range(4, 2)
// => False
 
_.in_range(2, 2)
// => False
 
_.in_range(1.2, 2)
// => True
 
_.in_range(5.2, 4)
// => False
 
_.in_range(-3, -2, -6)
// => True

_.random([lower=0], [upper=1], [floating])

Example

_.random(0, 5);
// => an integer between 0 and 5
 
_.random(5);
// => also an integer between 0 and 5
 
_.random(5, true);
// => a floating-point number between 0 and 5
 
_.random(1.2, 5.2);
// => a floating-point number between 1.2 and 5.2

"String" Methods

_.camel_case([string=''])

Example

_.camel_case('Foo Bar')
// => 'fooBar'
 
_.camel_case('--foo-bar--')
// => 'fooBar'
 
_.camel_case('__FOO_BAR__')
// => 'fooBar'

_.capitalize([string=''])

Example

_.capitalize('FRED')
// => 'Fred'

_.ends_with([string=''], [target], [position=string.length])

Example

_.ends_with('abc', 'c')
// => True
 
_.ends_with('abc', 'b')
// => False
 
_.ends_with('abc', 'b', 2)
// => True

_.escape([string=''])

Example

_.escape('fred, barney, & pebbles')
// => 'fred, barney, &amp; pebbles'

_.lower_case([string=''])

Example

_.lower_case('--Foo-Bar--')
// => 'foo bar'
 
_.lower_case('fooBar')
// => 'foo bar'
 
_.lower_case('__FOO_BAR__')
// => 'foo bar'

_.lower_first([string=''])

Example

_.lower_first('Fred')
// => 'fred'
 
_.lower_first('FRED')
// => 'fRED'

_.pad([string=''], [length=0], [chars=' '])

Example

_.pad('abc', 8)
// => '  abc   '
 
_.pad('abc', 8, '_-')
// => '_-abc_-_'
 
_.pad('abc', 3)
// => 'abc'

_.pad_end([string=''], [length=0], [chars=' '])

Example

_.pad_end('abc', 6)
// => 'abc   '
 
_.pad_end('abc', 6, '_-')
// => 'abc_-_'
 
_.pad_end('abc', 3)
// => 'abc'

_.pad_start([string=''], [length=0], [chars=' '])

Example

_.pad_start('abc', 6)
// => '   abc'
 
_.pad_start('abc', 6, '_-')
// => '_-_abc'
 
_.pad_start('abc', 3)
// => 'abc'

_.repeat([string=''], [n=1])

Example

_.repeat('*', 3)
// => '***'
 
_.repeat('abc', 2)
// => 'abcabc'
 
_.repeat('abc', 0)
// => ''

_.replace([string=''], pattern, replacement)

Example

_.replace('Hi Fred', 'Fred', 'Barney')
// => 'Hi Barney'

_.starts_with([string=''], [target], [position=0])

Example

_.starts_with('abc', 'a')
// => True
 
_.starts_with('abc', 'b')
// => False
 
_.starts_with('abc', 'b', 1)
// => True

Development Mode

Building Your Package

In development mode, pylodash need install packages below to build package:

- setuptools >= 38.6.0
- wheel >= 0.31.0
- twine >= 1.11.0

Run command to build package:

$ python3 setup.py sdist bdist_wheel

Testing Your Package

To check that your package description will render properly on PyPI, you can run twine check on the files created in dist:

$ twine check dist/*

Uploading Your Packages

To install package to develop environment, we can use command:

$ python3 setup.py develop

To upload package to testing environment before upload to PyPI, follow step (make sure you have an account registered):

$ twine upload --repository-url https://test.pypi.org/legacy/ dist/*

To publish your own package to PyPI, this final step is short:

$ twine upload dist/*

pip install Your Package

With your package uploaded to PyPI, you can install it with pip as well:

$ pip install pylodash

To run all test cases in package, you can run command below in folder package:

$ python setup.py test

License

Copyright © 2019 All rights reserved.

Project details


Download files

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

Files for pylodash, version 0.4.4
Filename, size File type Python version Upload date Hashes
Filename, size pylodash-0.4.4.tar.gz (10.6 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page