Skip to content

Commit 514c743

Browse files
string split: allowing for quoted strings with shlex
1 parent a9ccec3 commit 514c743

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

cs_util/args.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import sys
1111
import os
12+
import shlex
1213

1314
from optparse import OptionParser
1415

@@ -103,6 +104,9 @@ def my_string_split(string, num=-1, verbose=False, stop=False, sep=None):
103104
the first in the list [space, underscore] that occurs in the string.
104105
(Thus, if both occur, use space.)
105106
107+
Handles quoted strings: entries containing spaces can be enclosed
108+
in double quotes, e.g., 'value1 "entry with spaces" value3'.
109+
106110
Parameters
107111
----------
108112
string : str
@@ -131,6 +135,19 @@ def my_string_split(string, num=-1, verbose=False, stop=False, sep=None):
131135
if string is None:
132136
return None
133137

138+
# Handle quoted strings with shlex
139+
if '"' in string or "'" in string:
140+
try:
141+
res = shlex.split(string)
142+
if num != -1 and num != len(res) and stop:
143+
raise ValueError(
144+
f"String has {len(res)} elements, required is {num}"
145+
)
146+
return res
147+
except ValueError:
148+
# Fall through to regular splitting if shlex fails
149+
pass
150+
134151
if sep is None:
135152
has_space = string.find(" ")
136153
has_underscore = string.find("_")
@@ -162,7 +179,7 @@ def my_string_split(string, num=-1, verbose=False, stop=False, sep=None):
162179

163180
if num != -1 and num != len(res) and stop:
164181
raise ValueError(
165-
f"String '{len(res)}' has length {num}, required is {num}"
182+
f"String has {len(res)} elements, required is {num}"
166183
)
167184

168185
return res

cs_util/cat.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,36 @@ def read_dndz(file_path):
253253
254254
Returns
255255
-------
256-
list :
256+
np.array
257257
redshift bin centers
258-
list :
258+
np.array
259259
number densities
260-
list :
261-
redshift bin edges
260+
np.array
261+
redshift bin edges; one less than centers and density arrays
262262
263263
"""
264-
dat = ascii.read(file_path, format="commented_header")
264+
try:
265+
# Expecting header line "# z dn_dz"
266+
dat = ascii.read(file_path, format="commented_header")
267+
missing = [col for col in ("z", "dn_dz") if col not in dat.dtype.names]
268+
if missing:
269+
raise ValueError(
270+
f"Missing columns in dndz path {file_path}: {missing}"
271+
)
272+
except:
273+
# No header line
274+
dat = ascii.read(file_path)
275+
dat.rename_column("col1", "z")
276+
dat.rename_column("col2", "dn_dz")
277+
278+
# Remove last n(z) value which should be zero, to match bin centers
279+
tolerance = 1e-5
280+
if dat["dn_dz"][-1] / sum(dat["dn_dz"]) > tolerance:
281+
raise ValueError("dn_dz at last z-edge = {dat['dn_dz'][-1]}, no zero")
265282

266-
# Remove last n(z) value which is zero, to match bin centers
267283
nz = dat["dn_dz"][:-1]
268284
z_edges = dat["z"]
285+
269286
z_centers = bin_edges2centers(z_edges)
270287

271288
return z_centers, nz, z_edges

0 commit comments

Comments
 (0)