Skip to content

Commit 7e90cd3

Browse files
committed
join utils in decouple
1 parent 90b0bb8 commit 7e90cd3

4 files changed

Lines changed: 40 additions & 32 deletions

File tree

decouple.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from shlex import shlex
66
from io import open
77
from collections import OrderedDict
8-
from util import strtobool
98

109
# Useful for very coarse version differentiation.
1110
PYVERSION = sys.version_info
@@ -26,6 +25,18 @@
2625

2726
DEFAULT_ENCODING = 'UTF-8'
2827

28+
29+
def strtobool(value):
30+
_value = value.lower()
31+
if _value in {"y", "yes", "t", "true", "on", "1"}:
32+
result = True
33+
elif _value in {"n", "no", "f", "false", "off", "0"}:
34+
result = False
35+
else:
36+
raise ValueError(" ".join(("invalid truth value", value)))
37+
return result
38+
39+
2940
class UndefinedValueError(Exception):
3041
pass
3142

@@ -261,7 +272,6 @@ def __init__(self, flat=None, cast=text_type, choices=None):
261272
self._valid_values.extend(self.flat)
262273
self._valid_values.extend([value for value, _ in self.choices])
263274

264-
265275
def __call__(self, value):
266276
transform = self.cast(value)
267277
if transform not in self._valid_values:

tests/test_strtobool.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pytest
2+
from decouple import strtobool
3+
4+
5+
def test_true_values():
6+
true_list = ["y", "yes", "t", "true", "on", "1"]
7+
for item in true_list:
8+
assert strtobool(item) == 1
9+
10+
11+
def test_false_values():
12+
false_list = ["n", "no", "f", "false", "off", "0"]
13+
for item in false_list:
14+
assert strtobool(item) == 0
15+
16+
17+
@pytest.mark.parametrize(
18+
"test_input,expected",
19+
[
20+
("Invalid_Value_1", "invalid truth value Invalid_Value_1"),
21+
("1nv4l1d_V4lu3_2", "invalid truth value 1nv4l1d_V4lu3_2"),
22+
("invalid_value_3", "invalid truth value invalid_value_3"),
23+
],
24+
)
25+
def test_eval(test_input, expected):
26+
with pytest.raises(ValueError) as execinfo:
27+
strtobool(test_input)
28+
assert str(execinfo.value) == expected

tests/test_util.py

Lines changed: 0 additions & 21 deletions
This file was deleted.

util.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)