Skip to content

Commit 053e166

Browse files
author
Alan Christie
committed
- TypedColumnReader now supports booleans
- Updated to version 2.4.3
1 parent 165874d commit 053e166

File tree

4 files changed

+49
-6
lines changed

4 files changed

+49
-6
lines changed

src/python/pipelines_utils/TypedColumnReader.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,31 @@ def __init__(self, column, row, value, message):
6565
self.message = message
6666

6767

68+
def convert_boolean(string_value):
69+
"""Converts a string to a boolean (see CONVERTERS).
70+
There is a converter function for each column type.
71+
72+
Boolean strings are independent of case. Values interpreted as True
73+
are: "yes", "true", "on", "1". values interpreted as False are
74+
"no", "false", "off", "0". Any other value will result in a ValueError.
75+
76+
:param string_value: The string to convert
77+
78+
:raises: ValueError if the string cannot be represented by a boolean
79+
"""
80+
81+
lean_string_value = string_value.strip().lower()
82+
if lean_string_value in ['yes', 'true', 'on', '1']:
83+
return True
84+
elif lean_string_value in ['no', 'false', 'off', '0']:
85+
return False
86+
87+
# Not recognised boolean if we get here
88+
raise ValueError('Unrecognised boolean ({})'.format(lean_string_value))
89+
90+
6891
def convert_int(string_value):
69-
"""Converts a string to integer (see CONVERTERS).
92+
"""Converts a string to to an integer (see CONVERTERS).
7093
There is a converter function for each column type.
7194
7295
:param string_value: The string to convert
@@ -77,7 +100,7 @@ def convert_int(string_value):
77100

78101

79102
def convert_float(string_value):
80-
"""Converts string to float (see CONVERTERS).
103+
"""Converts string to a float (see CONVERTERS).
81104
There is a converter function for each column type.
82105
83106
:param string_value: The string to convert
@@ -98,7 +121,8 @@ def convert_string(string_value):
98121

99122
# A map of column type names (case-insensitive) to string conversion function.
100123
# If a column is 'name:INT' then we call 'convert_int()' for the column values.
101-
CONVERTERS = {'int': convert_int,
124+
CONVERTERS = {'boolean': convert_boolean,
125+
'int': convert_int,
102126
'float': convert_float,
103127
'string': convert_string}
104128

@@ -114,7 +138,8 @@ class TypedColumnReader(object):
114138
where, if the column header defines a type, the value is converted to that
115139
type.
116140
117-
There is built-in support for ``int``, ``float`` and ``string`` data types.
141+
There is built-in support for ``boolean``,
142+
``int``, ``float`` and ``string`` data types.
118143
119144
As an example, the following is a comma-separated header for a file with
120145
columns ``names`` "smiles", "comment", "hac" and "ratio" where
@@ -128,7 +153,6 @@ class TypedColumnReader(object):
128153
* If a column value is empty/blank the corresponding dictionary
129154
value is ``None``
130155
131-
132156
"""
133157

134158
def __init__(self, csv_file,

src/python/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_long_description():
2222
setup(
2323

2424
name='im-pipelines-utils',
25-
version='2.4.2',
25+
version='2.4.3',
2626
author='Alan Christie',
2727
author_email='[email protected]',
2828
url='https://github.com/InformaticsMatters/pipelines-utils',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a:boolean, b:boolean
2+
yes, no
3+
true, false
4+
on, off
5+
1, 0

src/python/test/python2_3/pipelines_utils/test_TypedColumnReader.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,17 @@ def test_basic_example_d_too_many_values(self):
169169
self.assertTrue(got_exception)
170170
self.assertEqual(0, num_lines)
171171
csv_file.close()
172+
173+
def test_basic_example_g_booleans(self):
174+
"""Test loading of a simple CSV file with all booleans
175+
"""
176+
test_file = os.path.join(DATA_DIR, 'TypedCsvReader.example.g.csv')
177+
csv_file = open(test_file)
178+
test_file = TypedColumnReader.TypedColumnReader(csv_file, column_sep=',')
179+
num_lines = 0
180+
for row in test_file:
181+
num_lines += 1
182+
self.assertTrue(row['a'])
183+
self.assertFalse(row['b'])
184+
self.assertEqual(4, num_lines)
185+
csv_file.close()

0 commit comments

Comments
 (0)