Skip to content

Commit a74a442

Browse files
authored
allow custom default options prefix (#22)
1 parent 76e49a7 commit a74a442

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

petsctools/options.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ class OptionsManager:
227227
If no trailing underscore is provided, one is appended. Hence
228228
``foo_`` and ``foo`` are treated equivalently. As an exception,
229229
if the prefix is the empty string, no underscore is appended.
230+
default_prefix
231+
The base string to generate default prefixes. If options_prefix
232+
is not provided then a prefix is automatically generated with the
233+
form "{default_prefix}_{n}", where n is a unique integer. Note that
234+
because the unique integer is not stable any options passed via the
235+
command line with a matching prefix will be ignored.
230236
231237
See Also
232238
--------
@@ -240,19 +246,24 @@ class OptionsManager:
240246

241247
count = itertools.count()
242248

243-
def __init__(self, parameters: dict, options_prefix: str | None):
249+
def __init__(self, parameters: dict,
250+
options_prefix: str | None = None,
251+
default_prefix: str | None = None):
244252
super().__init__()
245253
if parameters is None:
246254
parameters = {}
247255
else:
248256
# Convert nested dicts
249257
parameters = flatten_parameters(parameters)
250258
if options_prefix is None:
251-
self.options_prefix = "firedrake_%d_" % next(self.count)
259+
default_prefix = default_prefix or "petsctools_"
260+
if not default_prefix.endswith("_"):
261+
default_prefix += "_"
262+
self.options_prefix = f"{default_prefix}{next(self.count)}_"
252263
self.parameters = parameters
253264
self.to_delete = set(parameters)
254265
else:
255-
if len(options_prefix) and not options_prefix.endswith("_"):
266+
if options_prefix and not options_prefix.endswith("_"):
256267
options_prefix += "_"
257268
self.options_prefix = options_prefix
258269
# Remove those options from the dict that were passed on
@@ -362,6 +373,7 @@ def attach_options(
362373
obj: petsc4py.PETSc.Object,
363374
parameters: dict | None = None,
364375
options_prefix: str | None = None,
376+
default_prefix: str | None = None
365377
) -> None:
366378
"""Set up an OptionsManager and attach it to a PETSc Object.
367379
@@ -373,6 +385,8 @@ def attach_options(
373385
The dictionary of parameters to use.
374386
options_prefix
375387
The options prefix to use for this object.
388+
default_prefix
389+
Base string for autogenerated default prefixes.
376390
377391
See Also
378392
--------
@@ -385,7 +399,9 @@ def attach_options(
385399
)
386400

387401
options = OptionsManager(
388-
parameters=parameters, options_prefix=options_prefix
402+
parameters=parameters,
403+
options_prefix=options_prefix,
404+
default_prefix=default_prefix,
389405
)
390406
obj.setAttr("options", options)
391407

@@ -470,6 +486,7 @@ def set_from_options(
470486
obj: petsc4py.PETSc.Object,
471487
parameters: dict | None = None,
472488
options_prefix: str | None = None,
489+
default_prefix: str | None = None,
473490
) -> None:
474491
"""Set up a PETSc object from the options in its OptionsManager.
475492
@@ -491,6 +508,8 @@ def set_from_options(
491508
The dictionary of parameters to use.
492509
options_prefix
493510
The options prefix to use for this object.
511+
default_prefix
512+
Base string for autogenerated default prefixes.
494513
495514
Raises
496515
------
@@ -523,7 +542,9 @@ def set_from_options(
523542
" provided to set_from_options"
524543
)
525544
attach_options(
526-
obj, parameters=parameters, options_prefix=options_prefix
545+
obj, parameters=parameters,
546+
options_prefix=options_prefix,
547+
default_prefix=default_prefix,
527548
)
528549

529550
if is_set_from_options(obj):

tests/test_options.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,25 @@ def test_unused_options(options_left):
7474
# Do we only raise a warning for the unused option?
7575
assert "optobj_not_used" in message
7676
assert "optobj_used" not in message
77+
78+
79+
@pytest.mark.skipnopetsc4py
80+
def test_options_prefix():
81+
"""Check that the OptionsManager sets the options prefix correctly.
82+
"""
83+
# Generic default prefix
84+
options = petsctools.OptionsManager({})
85+
assert options.options_prefix.startswith("petsctools_")
86+
87+
# User defined default prefix
88+
options = petsctools.OptionsManager({}, default_prefix="firedrake")
89+
assert options.options_prefix.startswith("firedrake_")
90+
91+
# Explicit prefix overrides default prefix
92+
options = petsctools.OptionsManager({}, options_prefix="myobj")
93+
assert options.options_prefix.startswith("myobj_")
94+
95+
# Explicit prefix overrides default prefix
96+
options = petsctools.OptionsManager({}, options_prefix="myobj",
97+
default_prefix="firedrake")
98+
assert options.options_prefix.startswith("myobj_")

0 commit comments

Comments
 (0)