cape.tnakit.optitem: Option list item access tools

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.tnakit.optitem.getel(x, i=None)

Return the ith element of an array if possible

Call:
>>> xi = getel(x)
>>> xi = getel(x, i)
Inputs:
x: scalar | list | numpy.ndarray

A number or list or NumPy vector

i: int | None

Index; if None, entire list is returned

Outputs:
xi: scalar | list

Equal to x[i] if possible, x[-1] if i is greater than the length of x, x if x is not a list or numpy.ndarray instance, or x if i is None

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]
Versions:
  • 2015-12-08 @ddalle: First version

  • 2019-03-02 @ddalle: Moved to tnakit

cape.tnakit.optitem.getel_list(x, i=None)

Return ith element of list where each element is list-like

Call:
>>> xi = getel_list(x)
>>> xi = getel_list(x, i)
Inputs:
x: scalar | list | numpy.ndarray

A number or list or NumPy vector

i: int | None

Index; if None, entire list is returned

Outputs:
xi: scalar | list

Equal to x[i] if possible, x[-1] if i is greater than the length of x, x if x[0] is not a list or numpy.ndarray instance, or x if i is None

Examples:
>>> getel_list('abc', 2)
'abc'
>>> getel_list(1.4, 0)
1.4
>>> getel_list([1.4], 0)
[1.4]
>>> getel_list([[1,4], [2,3], [-1,5]], 1)
[2, 3]
>>> getel_list([[1,4], [2,3], [-1,5]], 13)
[-1, 5]
>>> getel_list([[1,4], [2,3], [-1,5]], 14)
[-1, 5]
See Also:
Versions:
  • 2019-03-04 @ddalle: First version

cape.tnakit.optitem.getringel(x, i=None)

Return the ith element of a “ring”, cycling if appropriate

Call:
>>> xi = getringel(x)
>>> xi = getringel(x, i)
Inputs:
x: scalar | list | np.ndarray | tuple

A scalar or iterable list-like object

i: int | None

Index; if None, entire list is returned

Outputs:
xi: scalar | list

Equal to x[mod(i,len(x)] unless i is None

Examples:
>>> getringel('abc', 2)
'abc'
>>> getringel(1.4, 0)
1.4
>>> getringel([200, 100, 300], 1)
100
>>> getringel([200, 100, 300], 14)
300
>>> getringel([200, 100, 300], 15)
200
>>> getringel([200, 100, 300])
[200, 100, 300]
Versions:
  • 2019-03-03 @ddalle: First version

cape.tnakit.optitem.getringel_list(x, i=None)

Return ith element of “ring”, where each element should be an array

Call:
>>> xi = getringel_list(x)
>>> xi = getringel_list(x, i)
Inputs:
x: scalar | list | numpy.ndarray

A number or list or NumPy vector

i: int | None

Index; if None, entire list is returned

Outputs:
xi: scalar | list

Equal to x[i] if possible, x[-1] if i is greater than the length of x, x if x[0] is not a list or numpy.ndarray instance, or x if i is None

Examples:
>>> getringel_list('abc', 2)
'abc'
>>> getringel_list(1.4, 0)
1.4
>>> getringel_list([1.4], 0)
[1.4]
>>> getringel_list([[1,4], [2,3], [-1,5]], 1)
[2, 3]
>>> getringel_list([[1,4], [2,3], [-1,5]], 13)
[2, 3]
>>> getringel_list([[1,4], [2,3], [-1,5]], 14)
[-1, 5]
See Also:
Versions:
  • 2019-03-04 @ddalle: First version

cape.tnakit.optitem.setel(x, i, xi)

Set the ith element 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 | None

Index to set. 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 to xi unless i is None

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:
  • 2015-12-08 @ddalle: First version