Skip to content

Commit

Permalink
Added arrays.create() method
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Oct 18, 2016
1 parent 724f2de commit e150ca8
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 46 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ Changelog

- Add ``clone()``.
- Add ``compact()``.
- Add ``create()``.
- Add ``every()``.
- Add ``exclude()``.
- Add ``filter()``.
- Add ``is_array()``.
- Add ``is_boolean()``.
- Add ``is_number()``.
- Add ``is_string()``.


v0.1.1 (2016-10-10)
Expand Down
11 changes: 11 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,16 @@
API Reference
=============

Arrays
------

.. automodule:: sugar.arrays
:members:

Predicates
----------

.. automodule:: sugar.predicates
:members:


8 changes: 8 additions & 0 deletions sugar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@
compact,
construct,
count,
create,
every,
exclude,
filter,
subtract
)

from .predicates import (
is_array,
is_boolean,
is_number,
is_string
)
2 changes: 1 addition & 1 deletion sugar/__pkg__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
__package_name__ = 'sugar.py'
__description__ = 'Python utility library. Based on sugar Javascript Library.'
__url__ = 'https://github.com/bharadwajyarlagadda/sugar.py'
__version__ = '0.2.0-dev'
__version__ = '0.1.1'
__author__ = 'Bharadwaj Yarlagadda'
__email__ = 'yarlagaddabharadwaj@gmail.com'
__license__ = 'MIT License'
80 changes: 71 additions & 9 deletions sugar/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import copy

import sugar as _


def average(array):
"""Returns the average for all the values in the given :attr:`array`.
Expand All @@ -24,11 +26,12 @@ def average(array):
return sum(array) / len(array)


def clone(array):
"""Returns a shallow copy of the given list.
def clone(obj):
"""Returns a shallow copy of the given list. This method can also be used
for othe objects such as int/float/string.
Args:
array (list): List of values provided by the user.
obj (list): List of values provided by the user.
Returns:
list: Shallow copy of the given array.
Expand All @@ -37,10 +40,14 @@ def clone(array):
>>> clone([1, 2, 3])
[1, 2, 3]
>>> clone('foobar')
'foobar'
>>> clone(1234)
1234
.. versionadded:: 0.2.0-dev
.. versionadded:: TODO
"""
return copy.copy(array)
return copy.copy(obj)


def compact(array, all=False):
Expand All @@ -62,7 +69,7 @@ def compact(array, all=False):
>>> compact([1, None, '', False, 2], all=True)
[1, 2]
.. versionadded:: 0.2.0-dev
.. versionadded:: TODO
"""
if all:
return subtract(array, [None, False, ''])
Expand Down Expand Up @@ -113,6 +120,61 @@ def count(array, value):
return array.count(value)


def create(obj=None, copy=False):
"""Creates an array from an unknown object.
Args:
obj (mixed): Value passed in by the user.
copy (bool): If clone is true, the array will be shallow cloned.
Returns:
list: Array from the given object.
Example:
>>> create('abc def 109;cd')
['a', 'b', 'c', ' ', 'd', 'e', 'f', ' ', '1', '0', '9', ';', 'c', 'd']
>>> create(1234)
[1234]
>>> create([11, 22, 33, 44], copy=True)
[11, 22, 33, 44]
>>> create(True)
[True]
>>> create()
[]
.. versionadded:: TODO
"""
result = None

if not obj:
result = []

if _.is_array(obj):
if copy:
# Shallow copy of the object.
result = clone(obj)
else:
# Copies the object. This is the fast way to copy the object.
result = obj[:]

if _.is_boolean(obj) or _.is_number(obj):
if copy:
# Shallow copy of the object.
result = [clone(obj)]
else:
result = [obj]

if _.is_string(obj):
if copy:
# Shallow copy of the object.
result = [value for value in clone(obj)[:]]
else:
result = [value for value in obj[:]]

return result


def every(array, value):
"""Returns true if search is true for all elements of the array. In other
words, this method returns True if :attr:`array` contains all the same
Expand All @@ -133,7 +195,7 @@ def every(array, value):
>>> every([2, 2, 3], 2)
False
.. versionadded:: 0.2.0-dev
.. versionadded:: TODO
"""
return all(element == value for element in array)

Expand All @@ -158,7 +220,7 @@ def exclude(array, value):
>>> exclude([11, 22, 33], [11, 22])
[33]
.. versionadded:: 0.2.0-dev
.. versionadded:: TODO
"""
return subtract(array, value)

Expand All @@ -184,7 +246,7 @@ def filter(array, value=None, callback=None):
>>> filter([1, 2, 2, 4], callback=lambda x: x > 1)
[2, 2, 4]
.. versionadded:: 0.2.0-dev
.. versionadded:: TODO
"""
if value:
return [element for element in array if element == value]
Expand Down
75 changes: 39 additions & 36 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,7 @@

from .fixtures import parametrize

from sugar import (
average,
clone,
compact,
construct,
count,
every,
exclude,
filter,
subtract
)
import sugar as _


def double(variable):
Expand All @@ -30,77 +20,90 @@ def triple(variable):
])
def test_average(array, expected_average):
"""Tests whether the average method is working properly or not."""
assert average(array) == expected_average
assert _.average(array) == expected_average


@parametrize('array,expected_output', [
@parametrize('array,expected', [
([1, 2, 3], [1, 2, 3])
])
def test_clone(array, expected_output):
def test_clone(array, expected):
"""Tests whether the clone method is working properly or not."""
assert clone(array) == expected_output
assert _.clone(array) == expected


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


@parametrize('length,callback,expected_output', [
@parametrize('length,callback,expected', [
(4, double, [0, 2, 4, 6]),
(4, triple, [0, 3, 6, 9])
])
def test_construct(length, callback, expected_output):
def test_construct(length, callback, expected):
"""Tests whether the construct method is working properly or not."""
assert construct(length, callback) == expected_output
assert _.construct(length, callback) == expected


@parametrize('array,value,expected_output', [
@parametrize('array,value,expected', [
([1, 2, 3, 3, 4], 3, 2),
([1, 2, 3, 3, 4], 5, 0)
])
def test_count(array, value, expected_output):
def test_count(array, value, expected):
"""Tests whether the count method is working properly or not."""
assert count(array, value) == expected_output
assert _.count(array, value) == expected


@parametrize('array,value,expected_output', [
@parametrize('obj,copy,expected', [
('abc def 10;/',
True,
['a', 'b', 'c', ' ', 'd', 'e', 'f', ' ', '1', '0', ';', '/']),
(True, False, [True]),
(1234, True, [1234]),
(None, True, []),
([11, 22, 33, 44], False, [11, 22, 33, 44])
])
def test_create(obj, copy, expected):
assert _.create(obj, copy) == expected


@parametrize('array,value,expected', [
([2, 2, 2], 2, True),
([2, 3, 4], 2, False)
])
def test_every(array, value, expected_output):
def test_every(array, value, expected):
"""Tests whether the every method is working properly or not."""
assert every(array, value) == expected_output
assert _.every(array, value) == expected


@parametrize('array,item,expected_output', [
@parametrize('array,item,expected', [
([1, 2, 3], 3, [1, 2]),
([1, 2, 3], [1, 3], [2]),
([1, 2, 3], 4, [1, 2, 3])
])
def test_exclude(array, item, expected_output):
def test_exclude(array, item, expected):
"""Tests whether the exclude method is working properly or not."""
assert exclude(array, item) == expected_output
assert _.exclude(array, item) == expected


@parametrize('array,value,callback,expected_output', [
@parametrize('array,value,callback,expected', [
([1, 2, 2, 4], 2, None, [2, 2]),
([1, 2, 3, 3, 4], None, lambda x: x > 1, [2, 3, 3, 4])
])
def test_filter(array, value, callback, expected_output):
def test_filter(array, value, callback, expected):
"""Tests whether the filter method is working properly or not."""
assert filter(array, value, callback) == expected_output
assert _.filter(array, value, callback) == expected


@parametrize('array,item,expected_output', [
@parametrize('array,item,expected', [
([1, 2, 3], 3, [1, 2]),
([1, 2, 3], [1, 3], [2]),
([1, 2, 3], [4], [1, 2, 3])
])
def test_subtract(array, item, expected_output):
def test_subtract(array, item, expected):
"""Tests whether the subtract method is working properly or not."""
assert subtract(array, item) == expected_output
assert _.subtract(array, item) == expected

0 comments on commit e150ca8

Please sign in to comment.