Skip to content

Commit

Permalink
Added compact method
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Oct 11, 2016
1 parent 01da969 commit a144520
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog


- Add ``clone()`` method.
- Add ``compact()`` method.


v0.1.1 (2016-10-10)
Expand Down
11 changes: 8 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ Install using pip:
>>> float(sugar.average([1, 2, 3]))
2.0
>>> sugar.clone([1, 2, 3])
[1, 2, 3]
>>> sugar.compact([1, None, 2, False, 3])
[1, 2, False, 3]
>>> sugar.compact([1, None, '', False, 2], all=True)
[1, 2]
>>> sugar.construct(4, lambda x: x * 2)
[0, 2, 4, 6]
Expand All @@ -49,9 +57,6 @@ Install using pip:
>>> sugar.subtract([1, 2, 3], 4)
[1, 2, 3]
>>> sugar.clone([1, 2, 3])
[1, 2, 3]
.. |version| image:: https://img.shields.io/pypi/v/sugar.py.svg?style=flat-square
:target: https://pypi.python.org/pypi/sugar.py/
Expand Down
1 change: 1 addition & 0 deletions sugar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .arrays import (
average,
clone,
compact,
construct,
count,
subtract
Expand Down
27 changes: 27 additions & 0 deletions sugar/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ def clone(array):
return copy.copy(array)


def compact(array, all=False):
"""Removes all instances of None, False, empty strings. This includes
None, False, and empty strings.
Args:
array (list): List of values provided by the user.
all (bool): Boolean value to remove all the instances of None, False
and empty strings.
Returns:
list: List of values with all falsy elements removed.
Example:
>>> compact([1, None, 2, False, 3])
[1, 2, False, 3]
>>> compact([1, None, '', False, 2], all=True)
[1, 2]
.. versionadded:: 0.2.0-dev
"""
if all:
return subtract(array, [None, False, ''])

return subtract(array, None)


def construct(var, callback):
"""Constructs an array of :attr:`var` length from the values of
:attr:`callback`.
Expand Down
10 changes: 10 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from sugar import (
average,
clone,
compact,
construct,
count,
subtract
Expand Down Expand Up @@ -37,6 +38,15 @@ def test_clone(array, expected_output):
assert clone(array) == expected_output


@parametrize('array,all,expected_output', [
([1, None, '', 2], False, [1, '', 2]),
([1, None, '', False, 2], True, [1, 2])
])
def test_compact(array, all, expected_output):
"""Tests whether the compact method is working properly or not."""
assert compact(array, all) == expected_output


@parametrize('length,callback,expected_output', [
(4, double, [0, 2, 4, 6]),
(4, triple, [0, 3, 6, 9])
Expand Down

0 comments on commit a144520

Please sign in to comment.