Skip to content

Commit b247952

Browse files
Merge pull request #61 from martinkilbinger/shlex
string split with quoted strings
2 parents 1fefa72 + 0acb456 commit b247952

3 files changed

Lines changed: 22 additions & 5 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,11 @@ def read_dndz(file_path):
253253
254254
Returns
255255
-------
256-
np.array :
256+
np.array
257257
redshift bin centers
258-
np.array :
258+
np.array
259259
number densities
260-
np.array :
260+
np.array
261261
redshift bin edges; one less than centers and density arrays
262262
263263
"""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "cs_util"
3-
version = "0.1.8"
3+
version = "0.1.9"
44
description = "Utility library for CosmoStat"
55
authors = [
66
{ name = "Martin Kilbinger", email = "martin.kilbinger@cea.fr" },

0 commit comments

Comments
 (0)