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

Remove ensure_unicode #108

Merged
merged 2 commits into from
Apr 26, 2021
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
6 changes: 1 addition & 5 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,7 @@ Raises an exception if o1 != o2
ensure_bytes(obj)
```
Converts to bytes
## ensure_unicode
```python
ensure_unicode(obj)
```
Converts to utf-8

## ensure_valid_filename
```python
ensure_valid_filename(s, min_length=3)
Expand Down
1 change: 0 additions & 1 deletion src/enochecker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
base64ify,
debase64ify,
ensure_bytes,
ensure_unicode,
ensure_valid_filename,
sha256ify,
snake_caseify,
Expand Down
19 changes: 1 addition & 18 deletions src/enochecker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,6 @@ def ensure_bytes(obj: Union[bytes, str, Any]) -> bytes:
return str(obj).encode("utf-8")


def ensure_unicode(obj: Union[bytes, str, Any]) -> str:
"""
Convert an object to an utf-8-encoded string.

:param obj: str or bytes (or anything else) to convert to string representation
:return: the string representation of the object
"""
if obj is None:
raise ValueError("Cannot stringify None")
if isinstance(obj, bytes):
return obj.decode("utf-8")
if isinstance(obj, str):
return obj
return str(obj)


def ensure_valid_filename(s: str, min_length: int = 3) -> str:
"""
Get a valid file name from the input.
Expand All @@ -117,14 +101,13 @@ def ensure_valid_filename(s: str, min_length: int = 3) -> str:
return s


def snake_caseify(camel: Union[str, bytes]) -> str:
def snake_caseify(camel: str) -> str:
"""
Turn camels into snake (-cases).

:param camel: camelOrSnakeWhatever
:return: camel_or_snake_whatever
"""
camel = ensure_unicode(camel)
half_snake = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", camel)
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", half_snake).lower()

Expand Down
5 changes: 1 addition & 4 deletions tests/test_enochecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
assert_equals,
assert_in,
ensure_bytes,
ensure_unicode,
snake_caseify,
)

Expand Down Expand Up @@ -120,9 +119,7 @@ def test_assert_equals():
def test_conversions():
assert isinstance(ensure_bytes("test"), bytes)
assert isinstance(ensure_bytes(b"test"), bytes)
assert isinstance(ensure_unicode("test"), type(""))
assert isinstance(ensure_unicode(b"test"), type(""))
assert ensure_unicode(ensure_bytes("test")) == "test"
assert ensure_bytes("test").decode() == "test"


def test_assert_in():
Expand Down
10 changes: 0 additions & 10 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
base64ify,
debase64ify,
ensure_bytes,
ensure_unicode,
ensure_valid_filename,
sha256ify,
snake_caseify,
Expand Down Expand Up @@ -60,15 +59,6 @@ def test_ensure_bytes():
assert ensure_bytes(1) == b"1"


def test_ensure_unicode():
with pytest.raises(ValueError):
ensure_unicode(None)

assert ensure_unicode(b"a") == "a"
assert ensure_unicode("a") == "a"
assert ensure_unicode(1) == "1"


def test_ensure_valid_filename():
assert ensure_valid_filename("filename") == "filename"
assert " " not in ensure_valid_filename("file name")
Expand Down