|
1 | 1 | #!/usr/bin/env python
|
2 | 2 | """Input/Output functions"""
|
| 3 | +import os |
| 4 | +from collections import Sequence |
3 | 5 | from netCDF4 import Dataset
|
4 | 6 | import configobj
|
5 |
| -from .pycompat import OrderedDict, SafeConfigParser |
| 7 | +from .pycompat import OrderedDict, SafeConfigParser, basestring, unicode_type |
6 | 8 |
|
7 | 9 |
|
8 | 10 | # -------------------------------------------------------------------- #
|
@@ -66,32 +68,27 @@ def config_type(value):
|
66 | 68 | First see the value is a bool, then try float, finally return a string.
|
67 | 69 | """
|
68 | 70 | 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 |
86 | 89 | 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 | + |
95 | 92 | # -------------------------------------------------------------------- #
|
96 | 93 |
|
97 | 94 |
|
@@ -135,3 +132,40 @@ def read_netcdf(nc_file, variables=[], coords=False, verbose=True):
|
135 | 132 | f.close()
|
136 | 133 | return d, a
|
137 | 134 | # -------------------------------------------------------------------- #
|
| 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