optdict.optitem
: Tools to access items from option lists¶
This module provides several utilities for accessing items from a list or a so-called “psuedo-list.”
For example, if the value of the option is just "name"
, this is
considered to be a list with "name"
repeating infinitely many times.
If an option is [1, 7]
, this is interpreted as either 1
and
7
alternating back and forth or 1
followed by 7
arbitrarily
many times (depending on which access function is used).
- cape.optdict.optitem.assert_array(obj, listdepth: int, desc=None)¶
Check that obj is an array to depth listdpeth
- cape.optdict.optitem.check_array(v, listdepth=0)¶
Check if v is an array type to at least specified depth
- cape.optdict.optitem.check_scalar(v, listdepth=0)¶
Check if v is a “scalar” to specified depth
- Call:
>>> q = check_scalar(v, listdepth=0)
- Inputs:
- v:
object
Any value
- listdepth: {
0
} |int
> 0 Depth of list to treat as a “scalar;” e.g. if listdepth is
1
, then[3, 4]
is a “scalar”, but if listdepth is0
, then[3, 4]
is not a scalar
- v:
- Outputs:
- q:
bool
Whether or not v is a scalar to specified depth
- q:
- Versions:
2021-12-16
@ddalle
: Version 1.0
- cape.optdict.optitem.getel(v, j=None, **kw)¶
Return the jth element of an array if possible
- Call:
>>> vj = getel(v, j=None, **kw)
- Inputs:
- v: scalar |
list
A scalar or list of items
- j: {
None
} |int
Phase index; use
None
to just return v- x, values: {
None
} |dict
Reference conditions to use with
@expr
,@map
, etc.; often a run matrix; used in combination with i- i, index: {
None
} |int
|np.ndarray
Case index or indices to use with
@expr
,@map
, etc.- ring:
True
| {False
} If
True
, then usex[j % len(x)]
; ifFalse
, usex[-1]
ifj > len(x)
- listdepth: {
0
} |int
> 0 Depth of list to treat as a “scalar;” e.g. if listdepth is
1
, then[3, 4]
is a “scalar”, but if listdepth is0
, then[3, 4]
is not a scalar
- v: scalar |
- Outputs:
- xj:
object
x
if j isNone
x
if x is scalar (seecheck_scalar()
)None
ifx==[]
x[j % len(x)]
if ringx[j]
if possiblex[-1]
if j is greater thanlen(x)-1
x[0]
if j is less than-len(x)
- xj:
- 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, 100, 300] >>> getel({"@expr": "$a"}, x={"a": [1, 2]}) [1, 2]
- Versions:
2015-12-08
@ddalle
: Version 1.02019-03-02
@ddalle
: Version 1.1; incape.tnakit
2021-12-15
@ddalle
: Version 1.2; inoptdict
2021-12-16
@ddalle
: Version 2.0; add ring, listdepth2022-09-12
@ddalle
: Version 3.0; add @map, @expr, @cons
- cape.optdict.optitem.setel(x, xj, j=None, listdepth=0)¶
Set the jth element of a list if possible
- Call:
>>> y = setel(x, xj, j=None, listdepth=0)
- Inputs:
- x:
list
|tuple
|object
A list or scalar object
- j: {
None
} |int
Phase index; use
None
to just return x- xj:
object
Value to set at
x[j]
if possible- listdepth: {
0
} |int
> 0 Depth of list to treat as a “scalar;” e.g. if listdepth is
1
, then[3, 4]
is a “scalar”, but if listdepth is0
, then[3, 4]
is not a scalar
- x:
- Outputs:
- y: xj |
list
Input x with
y[j]
set to xj unless j isNone
- y: xj |
- Examples:
>>> setel(['a', 2, 'c'], 'b', 1) ['a', 'b', 'c'] >>> setel(['a', 'b'], 'c', 2) ['a', 'b', 'c'] >>> setel(['a', 'b'], 'c', j=3) ['a', 'b', 'b', 'c'] >>> setel([0, 1], 'a') 'a' >>> setel([0, 1], 'a', -4) ['a', 0, 0, 1] >>> setel([0, 1], 'a', j=1, listdepth=1) [[0, 1], 'a'] >>> setel([0, 1], 'a', j=1, listdepth=2) [[[0, 1]], 'a']
- Versions:
2015-12-08
@ddalle
: Version 1.0; incape.tnakit
2021-12-17
@ddalle
: Version 2.0