Merge lp:~hopem/charm-helpers/add-core-strutils into lp:charm-helpers

Proposed by Edward Hope-Morley
Status: Merged
Merged at revision: 309
Proposed branch: lp:~hopem/charm-helpers/add-core-strutils
Merge into: lp:charm-helpers
Diff against target: 76 lines (+67/-0)
2 files modified
charmhelpers/core/strutils.py (+38/-0)
tests/core/test_strutils.py (+29/-0)
To merge this branch: bzr merge lp:~hopem/charm-helpers/add-core-strutils
Reviewer Review Type Date Requested Status
Liam Young (community) Approve
Review via email: mp+249798@code.launchpad.net
To post a comment you must log in.
309. By Edward Hope-Morley

simplified

Revision history for this message
Liam Young (gnuoy) wrote :

Approve

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'charmhelpers/core/strutils.py'
--- charmhelpers/core/strutils.py 1970-01-01 00:00:00 +0000
+++ charmhelpers/core/strutils.py 2015-02-16 11:02:09 +0000
@@ -0,0 +1,38 @@
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright 2014-2015 Canonical Limited.
5#
6# This file is part of charm-helpers.
7#
8# charm-helpers is free software: you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License version 3 as
10# published by the Free Software Foundation.
11#
12# charm-helpers is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public License
18# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
19
20
21def bool_from_string(value):
22 """Interpret string value as boolean.
23
24 Returns True if value translates to True otherwise False.
25 """
26 if isinstance(value, str):
27 value = value.lower()
28 else:
29 msg = "Unable to interpret non-string value '%s' as boolean" % (value)
30 raise ValueError(msg)
31
32 if value in ['y', 'yes', 'true', 't']:
33 return True
34 elif value in ['n', 'no', 'false', 'f']:
35 return False
36
37 msg = "Unable to interpret string value '%s' as boolean" % (value)
38 raise ValueError(msg)
039
=== added file 'tests/core/test_strutils.py'
--- tests/core/test_strutils.py 1970-01-01 00:00:00 +0000
+++ tests/core/test_strutils.py 2015-02-16 11:02:09 +0000
@@ -0,0 +1,29 @@
1import unittest
2
3import charmhelpers.core.strutils as strutils
4
5
6class TestStrUtils(unittest.TestCase):
7 def setUp(self):
8 super(TestStrUtils, self).setUp()
9
10 def tearDown(self):
11 super(TestStrUtils, self).tearDown()
12
13 def test_bool_from_string(self):
14 self.assertTrue(strutils.bool_from_string('true'))
15 self.assertTrue(strutils.bool_from_string('True'))
16 self.assertTrue(strutils.bool_from_string('yes'))
17 self.assertTrue(strutils.bool_from_string('Yes'))
18 self.assertTrue(strutils.bool_from_string('y'))
19 self.assertTrue(strutils.bool_from_string('Y'))
20
21 self.assertFalse(strutils.bool_from_string('False'))
22 self.assertFalse(strutils.bool_from_string('false'))
23 self.assertFalse(strutils.bool_from_string('no'))
24 self.assertFalse(strutils.bool_from_string('No'))
25 self.assertFalse(strutils.bool_from_string('n'))
26 self.assertFalse(strutils.bool_from_string('N'))
27
28 self.assertRaises(ValueError, strutils.bool_from_string, None)
29 self.assertRaises(ValueError, strutils.bool_from_string, 'foo')

Subscribers

People subscribed via source and target branches