Skip to content

Commit 8dbff6b

Browse files
committed
Several options are sequences instead of str
Splitting is made directly via TupleEntry from loam instead of manually from a string option.
1 parent 09fff40 commit 8dbff6b

File tree

9 files changed

+38
-66
lines changed

9 files changed

+38
-66
lines changed

stagpy/_helpers.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from . import conf
1010

1111
if TYPE_CHECKING:
12-
from typing import Optional, Any, List, Callable, NoReturn
12+
from typing import Optional, Any, Callable, NoReturn
1313
from matplotlib.figure import Figure
1414
from numpy import ndarray
1515

@@ -89,26 +89,6 @@ def baredoc(obj: object) -> str:
8989
return doc.rstrip(' .').lstrip()
9090

9191

92-
def list_of_vars(arg_plot: str) -> List[List[List[str]]]:
93-
"""Construct list of variables per plot.
94-
95-
Args:
96-
arg_plot: variable names separated with ``-`` (figures),
97-
``.`` (subplots) and ``,`` (same subplot).
98-
Returns:
99-
three nested lists of str
100-
101-
- variables on the same subplot;
102-
- subplots on the same figure;
103-
- figures.
104-
"""
105-
lovs = [[[var for var in svars.split(',') if var]
106-
for svars in pvars.split('.') if svars]
107-
for pvars in arg_plot.split('-') if pvars]
108-
lovs = [[slov for slov in lov if slov] for lov in lovs if lov]
109-
return [lov for lov in lovs if lov]
110-
111-
11292
def find_in_sorted_arr(value: Any, array: ndarray, after: bool = False) -> int:
11393
"""Return position of element in a sorted array.
11494

stagpy/commands.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def info_cmd() -> None:
2929
Other Parameters:
3030
conf.info
3131
"""
32-
varlist = [var for var in conf.info.output.replace(',', ' ').split()]
3332
sdat = stagyydata.StagyyData()
3433
lsnap = sdat.snaps[-1]
3534
lstep = sdat.steps[-1]
@@ -53,7 +52,7 @@ def info_cmd() -> None:
5352
print(f', snapshot {step.isnap}/{lsnap.isnap}')
5453
else:
5554
print()
56-
series = step.timeinfo.loc[varlist]
55+
series = step.timeinfo.loc[list(conf.info.output)]
5756
if conf.scaling.dimensional:
5857
series = series.copy()
5958
dimensions = []

stagpy/config.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818

1919
_indices = TupleEntry(inner_from_toml=lprs.slice_or_int_parser)
20+
_plots = TupleEntry.wrapping(
21+
TupleEntry.wrapping(TupleEntry(str), str_sep="."), str_sep="-")
2022

2123
HOME_DIR = Path.home()
2224
CONFIG_DIR = HOME_DIR / '.config' / 'stagpy'
@@ -87,9 +89,8 @@ class Scaling(Section):
8789
class Field(Section):
8890
"""Options of the field command."""
8991

90-
plot: str = entry(
91-
val='T,stream', cli_short='o',
92-
cli_kwargs={'nargs': '?', 'const': '', 'type': str},
92+
plot: Sequence[Sequence[Sequence[str]]] = _plots.entry(
93+
default='T,stream', cli_short='o',
9394
doc="variables to plot (see stagpy var)")
9495
perturbation: bool = switch_opt(
9596
False, None, "plot departure from average profile")
@@ -120,9 +121,8 @@ class Field(Section):
120121
class Rprof(Section):
121122
"""Options of the rprof command."""
122123

123-
plot: str = entry(
124-
val="Tmean", cli_short='o',
125-
cli_kwargs={'nargs': '?', 'const': ''},
124+
plot: Sequence[Sequence[Sequence[str]]] = _plots.entry(
125+
default="Tmean", cli_short='o',
126126
doc="variables to plot (see stagpy var)")
127127
style: str = entry(val='-', doc="matplotlib line style")
128128
average: bool = switch_opt(False, 'a', 'plot temporal average')
@@ -134,9 +134,8 @@ class Rprof(Section):
134134
class Time(Section):
135135
"""Options of the time command."""
136136

137-
plot: str = entry(
138-
val="Nutop,ebalance,Nubot.Tmean", cli_short='o',
139-
cli_kwargs={'nargs': '?', 'const': ''},
137+
plot: Sequence[Sequence[Sequence[str]]] = _plots.entry(
138+
default="Nutop,ebalance,Nubot.Tmean", cli_short='o',
140139
doc="variables to plot (see stagpy var)")
141140
style: str = entry(val='-', doc="matplotlib line style")
142141
compstat: Sequence[str] = TupleEntry(str).entry(
@@ -159,19 +158,17 @@ class Time(Section):
159158
class Refstate(Section):
160159
"""Options of the refstate command."""
161160

162-
plot: str = entry(
163-
val='T', cli_short='o', cli_kwargs={'nargs': '?', 'const': ''},
164-
doc="variables to plot (see stagpy var)")
161+
plot: Sequence[str] = TupleEntry(str).entry(
162+
default='T', cli_short='o', doc="variables to plot (see stagpy var)")
165163
style: str = entry(val='-', doc="matplotlib line style")
166164

167165

168166
@dataclass
169167
class Plates(Section):
170168
"""Options of the plates command."""
171169

172-
plot: str = entry(
173-
val='c.T.v2-v2.dv2-v2.topo_top', cli_short='o',
174-
cli_kwargs={'nargs': '?', 'const': ''},
170+
plot: Sequence[Sequence[Sequence[str]]] = _plots.entry(
171+
default='c.T.v2-v2.dv2-v2.topo_top', cli_short='o',
175172
doc="variables to plot, can be a surface field, field, or dv2")
176173
field: str = entry(val='eta', doc="field to plot with plates info")
177174
stress: bool = switch_opt(
@@ -192,8 +189,9 @@ class Plates(Section):
192189
class Info(Section):
193190
"""Options of the info command."""
194191

195-
output: str = entry(val='t,Tmean,vrms,Nutop,Nubot', cli_short='o',
196-
doc="time series to print")
192+
output: Sequence[str] = TupleEntry(str).entry(
193+
default='t,Tmean,vrms,Nutop,Nubot', cli_short='o',
194+
doc="time series to print")
197195

198196

199197
@dataclass

stagpy/field.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,8 @@ def cmd() -> None:
344344
conf.core
345345
"""
346346
sdat = StagyyData()
347-
lovs = _helpers.list_of_vars(conf.field.plot)
348347
# no more than two fields in a subplot
349-
lovs = [[slov[:2] for slov in plov] for plov in lovs]
348+
lovs = [[slov[:2] for slov in plov] for plov in conf.field.plot]
350349
minmax = {}
351350
if conf.plot.cminmax:
352351
conf.plot.vmin = None

stagpy/plates.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,18 @@ def _continents_location(snap: Step, at_surface: bool = True) -> ndarray:
194194
return csurf >= 2
195195

196196

197-
def plot_at_surface(snap: Step, names: str) -> None:
197+
def plot_at_surface(
198+
snap: Step, names: Sequence[Sequence[Sequence[str]]]
199+
) -> None:
198200
"""Plot surface diagnostics.
199201
200202
Args:
201203
snap: a :class:`~stagpy._step.Step` of a StagyyData instance.
202-
names: names of requested surface diagnotics. They are separated by
203-
``-`` (figures), ``.`` (subplots) and ``,`` (same subplot).
204-
Surface diagnotics can be valid surface field names, field names,
205-
or `"dv2"` which is d(vphi)/dphi.
204+
names: names of requested surface diagnotics. They are organized by
205+
figures, plots and subplots. Surface diagnotics can be valid
206+
surface field names, field names, or `"dv2"` which is d(vphi)/dphi.
206207
"""
207-
for vfig in _helpers.list_of_vars(names):
208+
for vfig in names:
208209
fig, axes = plt.subplots(nrows=len(vfig), sharex=True,
209210
figsize=(12, 2 * len(vfig)))
210211
axes = [axes] if len(vfig) == 1 else axes

stagpy/refstate.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,5 @@ def cmd() -> None:
4444
"""
4545
sdat = StagyyData()
4646

47-
lov = conf.refstate.plot.split(',')
48-
if not lov:
49-
return
50-
for var in lov:
47+
for var in conf.refstate.plot:
5148
plot_ref(sdat, var)

stagpy/rprof.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,24 @@
99
from .stagyydata import StagyyData
1010

1111
if typing.TYPE_CHECKING:
12+
from typing import Sequence
1213
from ._step import Step, _Rprofs
1314

1415

15-
def plot_rprofs(rprofs: _Rprofs, names: str) -> None:
16+
def plot_rprofs(
17+
rprofs: _Rprofs, names: Sequence[Sequence[Sequence[str]]]
18+
) -> None:
1619
"""Plot requested radial profiles.
1720
1821
Args:
1922
rprofs: a radial profile collection, such as :attr:`Step.rprofs` or
2023
:attr:`_StepsView.rprofs_averaged`.
21-
names: profile names separated by ``-`` (figures), ``.`` (subplots) and
22-
``,`` (same subplot).
24+
names: profile names organized by figures, plots and subplots.
2325
"""
2426
stepstr = rprofs.stepstr
2527
sdat = rprofs.step.sdat
2628

27-
for vfig in _helpers.list_of_vars(names):
29+
for vfig in names:
2830
fig, axes = plt.subplots(ncols=len(vfig), sharey=True,
2931
figsize=(4 * len(vfig), 6))
3032
axes = [axes] if len(vfig) == 1 else axes

stagpy/time_series.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .stagyydata import StagyyData
1313

1414
if typing.TYPE_CHECKING:
15-
from typing import Optional, List
15+
from typing import Optional, List, Sequence
1616
from pandas import DataFrame
1717

1818

@@ -26,20 +26,21 @@ def _collect_marks(sdat: StagyyData) -> List[float]:
2626
return times
2727

2828

29-
def plot_time_series(sdat: StagyyData, names: str) -> None:
29+
def plot_time_series(
30+
sdat: StagyyData, names: Sequence[Sequence[Sequence[str]]]
31+
) -> None:
3032
"""Plot requested time series.
3133
3234
Args:
3335
sdat: a :class:`~stagpy.stagyydata.StagyyData` instance.
34-
names: time series names separated by ``-`` (figures), ``.`` (subplots)
35-
and ``,`` (same subplot).
36+
names: time series names organized by figures, plots and subplots.
3637
3738
Other Parameters:
3839
conf.time.tstart: the starting time.
3940
conf.time.tend: the ending time.
4041
"""
4142
time_marks = _collect_marks(sdat)
42-
for vfig in _helpers.list_of_vars(names):
43+
for vfig in names:
4344
tstart = conf.time.tstart
4445
tend = conf.time.tend
4546
fig, axes = plt.subplots(nrows=len(vfig), sharex=True,

tests/test_helpers.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,3 @@ def test_baredoc():
2323
"""
2424
expected = 'Badly formatted docstring'
2525
assert stagpy._helpers.baredoc(test_baredoc) == expected
26-
27-
28-
def test_list_of_vars():
29-
expected = [[['a', 'b'], ['c', 'd', 'e']], [['f', 'g'], ['h']]]
30-
assert stagpy._helpers.list_of_vars('a,b..c,d,,,e-f,g.h-,..,-') == expected

0 commit comments

Comments
 (0)