Skip to content

Commit a697ea9

Browse files
author
Emanuele Palazzetti
committed
[core] add asbool function to parse env vars
1 parent eba304c commit a697ea9

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

ddtrace/util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ def _get_original_method(thing, key):
100100
setattr(patchable, key, dest.__get__(patchable, patchable.__class__))
101101

102102

103+
def asbool(value):
104+
"""Convert the given String to a boolean object. Accepted
105+
values are `True` and `1`."""
106+
if value is None:
107+
return False
108+
109+
if isinstance(value, bool):
110+
return value
111+
112+
return value.lower() in ("true", "1")
113+
114+
103115
def unwrap(obj, attr):
104116
f = getattr(obj, attr, None)
105117
if f and isinstance(f, wrapt.ObjectProxy) and hasattr(f, '__wrapped__'):

tests/contrib/test_utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from nose.tools import eq_
22

33
from ddtrace.contrib.util import func_name
4+
from ddtrace.util import asbool
45
from functools import partial
56

67
class SomethingCallable(object):

tests/test_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import unittest
2+
3+
from nose.tools import eq_
4+
5+
from ddtrace.util import asbool
6+
7+
8+
class TestUtilities(unittest.TestCase):
9+
def test_asbool(self):
10+
# ensure the value is properly cast
11+
eq_(asbool("True"), True)
12+
eq_(asbool("true"), True)
13+
eq_(asbool("1"), True)
14+
eq_(asbool("False"), False)
15+
eq_(asbool("false"), False)
16+
eq_(asbool(None), False)
17+
eq_(asbool(""), False)
18+
eq_(asbool(True), True)
19+
eq_(asbool(False), False)

0 commit comments

Comments
 (0)