Skip to content

Commit 616979d

Browse files
author
Joe Hamman
committed
update config parsers, add tests
1 parent a1f6d27 commit 616979d

File tree

2 files changed

+100
-26
lines changed

2 files changed

+100
-26
lines changed

tests/test_io.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from tonic.io import config_type, isfloat, isint, isscalar
2+
3+
4+
def test_config_type_int():
5+
assert config_type('1') == 1
6+
7+
8+
def test_config_type_float():
9+
assert config_type('1.75') == 1.75
10+
11+
12+
def test_config_type_bool():
13+
assert config_type('True')
14+
15+
16+
def test_config_type():
17+
x = [True, None, 'abc', 1, 1.5]
18+
assert config_type(", ".join(map(str, x))) == x
19+
20+
21+
def test_isfloat():
22+
assert isfloat(4.3)
23+
assert isfloat('4.3')
24+
assert isfloat(4)
25+
assert isfloat('4')
26+
assert not isfloat('four')
27+
28+
29+
def test_isint():
30+
assert isint(4)
31+
assert isint('4')
32+
assert not isint(4.3)
33+
assert not isint('4.3')
34+
35+
36+
def test_isscalar():
37+
assert not isscalar([0, 1])
38+
assert not isscalar(('a', 'b'))
39+
assert isscalar(1)
40+
assert not isscalar('str')

tonic/io.py

+60-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/usr/bin/env python
22
"""Input/Output functions"""
3+
import os
4+
from collections import Sequence
35
from netCDF4 import Dataset
46
import configobj
5-
from .pycompat import OrderedDict, SafeConfigParser
7+
from .pycompat import OrderedDict, SafeConfigParser, basestring, unicode_type
68

79

810
# -------------------------------------------------------------------- #
@@ -66,32 +68,27 @@ def config_type(value):
6668
First see the value is a bool, then try float, finally return a string.
6769
"""
6870
val_list = [x.strip() for x in value.split(',')]
69-
if len(val_list) == 1:
70-
value = val_list[0]
71-
if value in ['true', 'True', 'TRUE', 'T']:
72-
return True
73-
elif value in ['false', 'False', 'FALSE', 'F']:
74-
return False
75-
elif value in ['none', 'None', 'NONE', '']:
76-
return None
77-
else:
78-
try:
79-
return int(value)
80-
except:
81-
pass
82-
try:
83-
return float(value)
84-
except:
85-
return value
71+
ret_list = []
72+
73+
for value in val_list:
74+
if value.lower() in ['true', 't']: # True
75+
ret_list.append(True)
76+
elif value.lower() in ['false', 'f']: # False
77+
ret_list.append(False)
78+
elif value.lower() in ['none', '']: # None
79+
ret_list.append(None)
80+
elif isint(value): # int
81+
ret_list.append(int(value))
82+
elif isfloat(value): # float
83+
ret_list.append(float(value))
84+
else: # string or similar
85+
ret_list.append(os.path.expandvars(value))
86+
87+
if len(ret_list) > 1:
88+
return ret_list
8689
else:
87-
try:
88-
return list(map(int, val_list))
89-
except:
90-
pass
91-
try:
92-
return list(map(float, val_list))
93-
except:
94-
return val_list
90+
return ret_list[0]
91+
9592
# -------------------------------------------------------------------- #
9693

9794

@@ -135,3 +132,40 @@ def read_netcdf(nc_file, variables=[], coords=False, verbose=True):
135132
f.close()
136133
return d, a
137134
# -------------------------------------------------------------------- #
135+
136+
137+
# -------------------------------------------------------------------- #
138+
def isfloat(x):
139+
'''Test if value is a float'''
140+
try:
141+
float(x)
142+
except ValueError:
143+
return False
144+
else:
145+
return True
146+
# -------------------------------------------------------------------- #
147+
148+
149+
# -------------------------------------------------------------------- #
150+
def isint(x):
151+
'''Test if value is an integer'''
152+
if isinstance(x, float) or isinstance(x, basestring) and '.' in x:
153+
return False
154+
try:
155+
a = float(x)
156+
b = int(a)
157+
except ValueError:
158+
return False
159+
else:
160+
return a == b
161+
# -------------------------------------------------------------------- #
162+
163+
164+
# -------------------------------------------------------------------- #
165+
def isscalar(x):
166+
'''Test if a value is a scalar'''
167+
if isinstance(x, (Sequence, basestring, unicode_type)):
168+
return False
169+
else:
170+
return True
171+
# -------------------------------------------------------------------- #

0 commit comments

Comments
 (0)