Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new 'split' functionality #47

Merged
merged 1 commit into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions shublang/shublang.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ def decode(iterable, encoding):
return (x.decode(encoding) for x in iterable)


@Pipe
def split(iterable, sep, maxsplit=-1):
"""Returns a list of words in the string, using sep as the delimiter.
If maxsplit is given, at most maxsplit splits are done.

:param iterable: collection of data to transform
:type iterable: list

:param sep: this is a delimiter. The string will be split by this separator.
:type sep: string

:param maxsplit: (optional) if given, there will be at most maxsplit splits.
:type maxsplit: int
"""


return (x.split(sep, maxsplit) for x in iterable)


@Pipe
def sanitize(iterable):
# TODO change name and add other options
Expand Down
21 changes: 21 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TODO add tests for functions

import pytest
from shublang import evaluate

def test_sub():
Expand All @@ -21,6 +22,26 @@ def test_decode():
b'\xbe\x80\xe1\xbe\x90'
assert evaluate('decode("UTF8")', data=[text]) == ["ἀἐἠἰὀὐὠὰᾀᾐ"]


@pytest.mark.parametrize(
"test_input,expected",
[
(
['split(",")', ['Python,Haskell,Scala,Rust']],
[['Python', 'Haskell', 'Scala', 'Rust']]
),

# maxsplit should limit the number of separations
(
['split(",", 2)', ['Python,Haskell,Scala,Rust']],
[['Python', 'Haskell', 'Scala,Rust']]
),
]
)
def test_split(test_input, expected):
assert evaluate(*test_input) == expected


def test_sanitize():
text = ["Python \t\t\t\t",
"<br/>Haskell",
Expand Down