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

Make unidecode optional in sanitize methods #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions shublang/shublang.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def map_value(iterable, rules_dict):
:param iterable: collection of data to transform
:type iterable: list

:param rules_dict: rules dictionary where the key should be the
:param rules_dict: rules dictionary where the key should be the
exactly text to look for and the value should be the desired output
:type rules_dict: dict

Expand Down Expand Up @@ -187,12 +187,13 @@ def split(iterable, sep, maxsplit=-1):


@Pipe
def sanitize(iterable):
def sanitize(iterable, ascii_safe=False):
# TODO change name and add other options

iterable = (x.strip() for x in iterable)
iterable = (re.sub(r'[\n\t\r\s]+', ' ', x) for x in iterable)
iterable = (unidecode(x) for x in iterable)
if ascii_safe:
iterable = (unidecode(x) for x in iterable)
iterable = (replace_entities(x) for x in iterable)
iterable = (remove_tags(x) for x in iterable)
return iterable
Expand Down
13 changes: 8 additions & 5 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# if a mapping is not found, return the value itself
(
[
'map_value({"This is foo": "foo", "This is bar": "bar"})',
'map_value({"This is foo": "foo", "This is bar": "bar"})',
["This is foo", "This is not bar"]
],
['foo', 'This is not bar']
Expand All @@ -19,7 +19,7 @@
# usual mapping
(
[
'map_value({"1": "Available", "2": "Unavailable"})',
'map_value({"1": "Available", "2": "Unavailable"})',
['1', '2']
],
['Available', 'Unavailable']
Expand All @@ -28,7 +28,7 @@
# map string to number
(
[
'map_value({"InStock": 1, "OutOfStock": 2})',
'map_value({"InStock": 1, "OutOfStock": 2})',
['OutOfStock', 'InStock']
],
[2, 1]
Expand All @@ -37,7 +37,7 @@
# map number to string
(
[
'map_value({0: "Online", 1: "Offline"})',
'map_value({0: "Online", 1: "Offline"})',
[0, 1]
],
['Online', 'Offline']
Expand Down Expand Up @@ -268,7 +268,10 @@ def test_sanitize():

def test_sanitize_1():
text = [u"Checking unicode ko\u017eu\u0161\u010dek \t\t\t\t"]
assert evaluate("sanitize", data=text) == ["Checking unicode kozuscek"]
assert evaluate("sanitize(ascii_safe=True)", data=text) == ["Checking unicode kozuscek"]

text = [u"Checking unicode ko\u017eu\u0161\u010dek \t\t\t\t"]
assert evaluate("sanitize", data=text) == ["Checking unicode kožušček"]

def test_xpath_getall():
html = '<div><li class="results"><ul>Skoda</ul><ul>Vauxhall</ul><ul>Peugot</ul></li></div>'
Expand Down