Skip to content

Commit

Permalink
Added clone method
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Oct 11, 2016
1 parent 6ac4521 commit 9289555
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ Changelog
=========


- Add ``clone()`` method.


v0.1.1 (2016-10-10)
-------------------

Expand Down
1 change: 1 addition & 0 deletions sugar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .arrays import (
average,
clone,
construct,
count,
subtract
Expand Down
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.1.1'
__version__ = '0.2.0-dev'
__author__ = 'Bharadwaj Yarlagadda'
__email__ = 'yarlagaddabharadwaj@gmail.com'
__license__ = 'MIT License'
21 changes: 21 additions & 0 deletions sugar/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import absolute_import

import copy


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


def clone(array):
"""Returns a shallow copy of the given list.
Args:
array (list): List of values provided by the user.
Returns:
list: Shallow copy of the given array.
Example:
>>> clone([1, 2, 3])
[1, 2, 3]
.. versionadded:: 0.2.0-dev
"""
return copy.copy(array)


def construct(var, callback):
"""Constructs an array of :attr:`var` length from the values of
:attr:`callback`.
Expand Down
9 changes: 9 additions & 0 deletions tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from sugar import (
average,
clone,
construct,
count,
subtract
Expand All @@ -28,6 +29,14 @@ def test_average(array, expected_average):
assert average(array) == expected_average


@parametrize('array,expected_output', [
([1, 2, 3], [1, 2, 3])
])
def test_clone(array, expected_output):
"""Tests whether the clone method is working properly or not."""
assert clone(array) == 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 9289555

Please sign in to comment.