Skip to content

Commit

Permalink
Merge pull request #50 from scrapinghub/feat-append-extend
Browse files Browse the repository at this point in the history
New Functionalities: append and extend
  • Loading branch information
BurnzZ authored Aug 13, 2020
2 parents 9e78d4e + a59a0d2 commit ceedfa3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
29 changes: 29 additions & 0 deletions shublang/shublang.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,35 @@ def format(iterable, template):
return (template.format(*x) for x in iterable)


@Pipe
def append(iterable, data):
"""Appends data to the iterable.
:param iterable: collection of data to transform
:type iterable: list
:param data: any type of data to be appended
"""

iterable.append(data)
return iterable


@Pipe
def extend(iterable, extension):
"""Extends the iterable using another iterable.
:param iterable: collection of data to transform
:type iterable: list
:param extension: contains the additional iterable to extend the current one
:param extension: iterable
"""

iterable.extend(extension)
return iterable


@Pipe
def encode(iterable, encoding, errors='ignore'):
return (x.encode(encoding, errors=errors) for x in iterable)
Expand Down
56 changes: 51 additions & 5 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,21 @@ def test_replace(test_input, expected):
['format("Now Playing: {} and {}")', [['Rick', 'Morty']]],
['Now Playing: Rick and Morty']
),
# Ordering should be respected
(
['format("{2}, {1}, and {0}")', [['a', 'b', 'c']]],
['c, b, and a']
),
# Lists of lists are aggregated into a list
(
['format("{} and some value {}")', [[1, 2], ['x', 'y']]],
['1 and some value 2', 'x and some value y']
),
# Args could be repeated
(
['format("{0}--{0}-{1}!")', [['Re', 'Remix']]],
['Re--Re-Remix!']
),
# Standard Formatting should work
(
['format("{:.2f}")', [[7/3]]],
Expand All @@ -91,6 +87,56 @@ def test_format(test_input, expected):
assert evaluate(*test_input) == expected


@pytest.mark.parametrize(
"test_input,expected",
[
(
['append("new thing")', ['A', 'B']],
['A', 'B', 'new thing']
),
# list could be added as a single item
(
['append([1, 2, "3"])', ['A', 'B']],
['A', 'B', [1, 2, '3']]
),
]
)
def test_append(test_input, expected):
assert evaluate(*test_input) == expected


@pytest.mark.parametrize(
"test_input,expected",
[
(
['extend([1, 2, "3"])', ['A', 'B']],
['A', 'B', 1, 2, '3']
),
# generators will also work
(
['extend(range(3, 6))', ['A', 'B']],
['A', 'B', 3, 4, 5]
),
# single strings are treated as iterables
(
['extend("new")', ['A', 'B']],
['A', 'B', 'n', 'e', 'w']
),
]
)
def test_extend(test_input, expected):
assert evaluate(*test_input) == expected


def test_extend_with_non_iterable():
"""It should raise a TypeError."""

with pytest.raises(TypeError):
evaluate("extend(123)", ['A', 'B'])


def test_encode():
text = "ἀἐἠἰὀὐὠὰᾀᾐ"
assert evaluate('encode("UTF8")', data=[text]) ==\
Expand All @@ -110,7 +156,7 @@ def test_decode():
['split(",")', ['Python,Haskell,Scala,Rust']],
[['Python', 'Haskell', 'Scala', 'Rust']]
),
# maxsplit should limit the number of separations
(
['split(",", 2)', ['Python,Haskell,Scala,Rust']],
Expand Down

0 comments on commit ceedfa3

Please sign in to comment.