cape.cfdx.options.util
: Utilities for options modules¶
This module provides utilities for the CAPE options module. It includes
the cape.options.util.odict
class upon which all CAPE options
classes are based, and it several basic methods useful to processing
options.
The getel()
and setel()
methods in particular play an
important role in the entire CAPE coding strategy.
- cape.cfdx.options.util.applyDefaults(opts, defs)¶
Recursively apply defaults for any missing options
- Call:
>>> opts = applyDefaults(opts, defs)
- Inputs:
- Outputs:
- Versions:
2014-06-17
@ddalle
: Version 1.02014-07-28
@ddalle
: Version 1.1; move to options module
- cape.cfdx.options.util.expandJSONFile(fname)¶
Expand contents of other JSON files
- Call:
>>> txt, fnames, linenos = expandJSONFile(fname)
- Inputs:
- fname:
str
|unicode
Name of JSON file to read
- fname:
- Outputs:
- txt:
unicode
Full text with references to JSON file(s) expanded
- fnames:
list
[str
] List of files read (can include the same file multiple times) including fname and any other expanded
JSONFile()
directives- linenos:
np.ndarray
(int
, ndim=2) Line numbers in original files; column j represents the line number of each line in file j;
0
for lines not from file j
- txt:
- Versions:
2015-12-10
@ddalle
: Version 1.0
- cape.cfdx.options.util.getCapeDefaults()¶
Read default CAPE settings configuration file
- Call:
>>> defs = getCapeDefaults()
- Outputs:
- defs:
dict
Dictionary of settings read from JSON file
- defs:
- Versions:
2015-09-20
@ddalle
: Version 1.02021-03-01
@ddalle
: Version 2.0; local JSON file
- cape.cfdx.options.util.getDefaults(fname)¶
Read default settings configuration file
- cape.cfdx.options.util.getTemplateFile(fname)¶
Get the absolute path to a template file by name
- cape.cfdx.options.util.getel(x, i=None)¶
Return element i of an array if possible
- Call:
>>> x = getel(x) >>> xi = getel(x, i)
- Inputs:
- x: number-like or list-like
A number or list or NumPy vector
- i:
None
|int
Index
- Outputs:
- xi: scalar
Equal to
x[i]
if possible,x[-1]
if i is greater than the length of x, orx
if x is not alist
ornumpy.ndarray
instance
- Examples:
>>> getel('abc', 2) 'abc' >>> getel(1.4, 0) 1.4 >>> getel([200, 100, 300], 1) 100 >>> getel([200, 100, 300], 15) 300 >>> getel([200, 100, 300]) 200
- Versions:
2014-07-29
@ddalle
: Version 1.02021-10-18
@ddalle
: Version 1.1; addtuple
- cape.cfdx.options.util.isArray(x)¶
Test if a variable is “list-like.”
- Call:
>>> q = isArray(x)
- Inputs:
- x: any
Any variable
- Outputs:
- q:
bool
True
if and only if x is a list or NumPy array
- q:
- Versions:
2014-12-17
@ddalle
: Version 1.0
- cape.cfdx.options.util.isStr(x)¶
Test if a variable is “string-like”
- Call:
>>> q = isArray(x)
- Inputs:
- x: any
Any variable
- Outputs:
- q:
bool
True
if and only if x is a string or unicode
- q:
- Versions:
2014-12-17
@ddalle
: Version 1.0
- cape.cfdx.options.util.loadJSONFile(fname)¶
Read JSON file w/ helpful error handling and comment stripping
- class cape.cfdx.options.util.odict¶
Dictionary-based options module
- Call:
>>> opts = odict(**kw)
- Inputs:
- kw:
dict
Dictionary of options
- kw:
- Outputs:
- opts:
cape.options.util.odict
Dictionary-based options interface
- opts:
- Versions:
2014-08-02
@ddalle
: Version 1.02015-11-10
@ddalle
: More robustget_key()
using rck
- copy()¶
Create a copy of an options interface
- get_key(k, i=None, rck=None)¶
Intelligently get option for index i of key k
This is a two-step process. The first is to get the dictionary value or the default if k is not in opts. The default is
rc[k]
. Let V be the result of the process.The second step is to apply indexing. If V is a scalar or i is
None
, then V is the output. Otherwise, the function will attempt to returnV[i]
, but if i is too large,V[-1]
is the output.- Call:
>>> v = opts.get_key(k, i, rck=None)
- Inputs:
- Outputs:
- v: any
Let
V=opts.get(k,rc[k])
. Then v is eitherV[i]
if possible,V[-1]
if V is a list and i is notNone
, orV
otherwise
- See also:
cape.options.util.getel()
- Versions:
2014-08-02
@ddalle
: Version 1.02015-11-10
@ddalle
: Version 1.1; add rck
- init_section(cls, sec=None, parent=None, prefix=None)¶
Initialize a generic section
- Call:
>>> opts.init_section(cls, sec=None, **kw)
- Inputs:
- Versions:
2021-10-18
@ddalle
: Version 1.0
- set_key(k, v=None, i=None, rck=None)¶
Set option for key k
This sets the value for
opts[k]
oropts[k][i]
if appropriate. If i is greater than the length ofopts[k]
, thenopts[k]
is appended with its current last value enough times to makeopts[k][i]
exist.- Call:
>>> opts.set_key(k, v=None, i=None, rck=None)
- Inputs:
- See also:
cape.options.util.setel()
- Versions:
2014-08-02
@ddalle
: Version 1.02015-11-10
@ddalle
: Version 1.1; add rck
- cape.cfdx.options.util.promote_subsec(cls1, cls2, sec=None, skip=[], **kw)¶
Promote all methods of a subsection class to parent options class
Methods of parent class will not be overwritten
- Call:
>>> promote_subsec(cls1, cls2, sec=None, skip=[], **kw)
- Inputs:
- Versions:
2019-01-10
@ddalle
: Version 1.0
- cape.cfdx.options.util.rc0(p)¶
Return default setting for named parameter
- Call:
>>> v = rc0(p)
- Inputs:
- p:
str
Name of parameter to extract
- p:
- Outputs:
- v: any
Either
rc[p]
orrc[p][0]
, whichever is appropriate
- Versions:
2014-08-01
@ddalle
: Version 1.0
- cape.cfdx.options.util.setel(x, i, xi)¶
Return element i of an array if possible
- Call:
>>> y = setel(x, i, xi)
- Inputs:
- x: number-like or list-like
A number or list or NumPy vector
- i:
int
Index. If i is
None
, the output is reset to xi- xi: scalar
Value to set at scalar
- Outputs:
- y: number-like or list-like
Input x with
y[i]
set toxi
unless i isNone
- Examples:
>>> setel(['a', 2, 'c'], 1, 'b') ['a', 'b', 'c'] >>> setel(['a', 'b'], 2, 'c') ['a', 'b', 'c'] >>> setel('a', 2, 'c') ['a', None, 'b'] >>> setel([0, 1], None, 'a') 'a'
- Versions:
2014-07-29
@ddalle
: Version 1.02021-10-18
@ddalle
: Version 1.1; addtuple
- cape.cfdx.options.util.subsec_func(cls, sec=None, parent=None, init=True)¶
Decorator (w/ args) to apply a function from a subsection class
- Call:
>>> f = subsec_func(cls, sec=None, parent=None, init=True)
- Inputs:
- Outputs:
- f:
function
Decorator with arguments expanded
- f:
- Examples:
@subsec_func("RunControl", RunControl) def get_PhaseSequence(self, *a, **kw): pass
- Versions:
2019-01-10
@ddalle
: Version 1.02021-10-18
@ddalle
: Version 1.1; default sec