Bases: cf.utils.CfList
An ordered sequence of variables stored in a list-like object.
In some contexts, whether an object is a variable or a variable list is not known and does not matter. So to avoid ungainly type testing, some aspects of the VariableList interface are shared by a variable and vice versa.
Any attribute or method belonging to a variable may be used on a variable list and will be applied independently to each element.
Just as it is straight forward to iterate over the variables in a variable list, a variable will behave like a single element variable list in iterative and indexing contexts.
Initialization
Parameters : |
|
---|
VariableList methods (undocumented methods behave exactly as their counterparts in a built-in list)
append | |
copy | Return a deep copy. |
count | Return the number of occurrences of a given value. |
delprop | Delete a CF property from each element of the list. |
dump | Return a string containing the full descriptions of each variable in |
equals | True if two instances are equal, False otherwise. |
extend | |
getprop | Return a built-in list of a CF properties from each element. |
hasprop | Return a built-in list describing whether each element has a CF |
index | Return the first index of a given value. |
insert | Insert an object before the given index in place. |
match | Return a list of booleans showing which elements match the given conditions. |
name | Return a built-in list of the names of each element of the list of |
override_units | Override the each element’s data array units in place. |
pop | |
remove | |
reverse | |
set_equals | True if two instances are set-wise equal, False otherwise. |
setprop | Set a CF property on each element of the list. |
sort | Sort the elements in place. |
subset | Return the elements which match the given conditions. |
subspace | Subspace each variable in the list, returning a new list of variables. |
Return a deep copy.
Equivalent to copy.deepcopy(s).
Returns : |
|
---|
Examples
>>> s.copy()
Return the number of occurrences of a given value.
Uses numerically tolerant equality where appropriate.
Parameters : |
|
---|---|
Returns : |
|
Examples
>>> s
[1, 2, 3, 2, 4, 2]
>>> s.count(1)
1
>>> s.count(2)
3
Delete a CF property from each element of the list.
Parameters : |
|
---|---|
Returns : | None |
Examples
>>> len(fl)
2
>>> fl.getprop('foo')
['bar', 'bar']
>>> fl.delprop('foo')
>>> fl.getprop('foo', 5)
[5, 5]
Note that recognised CF propeties (such as ‘long_name’) may be deleted directly:
>>> del fl.standard_name
>>> getattr(fl, 'standard_name', None)
[None, None]
Return a string containing the full descriptions of each variable in the list.
True if two instances are equal, False otherwise.
Two instances are equal if their attributes are equal and their elements are equal pair-wise.
Parameters : |
|
---|---|
Returns : |
|
Return a built-in list of a CF properties from each element.
Parameters : |
|
---|---|
Returns : |
|
Raises : |
|
Examples
>>> len(fl)
2
>>> fl.getprop('foo')
['bar', 'bar']
>>> fl.delprop('foo')
>>> fl.getprop('foo', 0.0)
[0.0, 0.0]
Note that recognised CF propeties (such as ‘long_name’) may be retrieved directly:
>>> fl.standard_name
['air_temperature', 'air_temperature']
>>> getattr(fl, 'long_name', 'default')
['default', 'default']
Return a built-in list describing whether each element has a CF property.
Parameters : |
|
---|---|
Returns : |
|
Examples
>>> len(fl)
2
>>> fl.hasprop('foo')
[False, True]
Warning: Recognised CF propeties (such as ‘long_name’) may be interrogated with the built-in hasattr, but with a different result:
>>> getattr(fl, 'standard_name', None)
['air_tmperature', None]
>>> hasattr(fl, 'standard_name')
True
>>> fl.hasprop('standard_name')
[True, False]
Return the first index of a given value.
Uses numerically tolerant equality where appropriate.
Parameters : |
|
---|---|
Returns : | out : int |
Raises : |
|
Examples
>>> s
[1, 2, 3, 2, 4, 2]
>>> s.index(1)
1
>>> s.index(2, 2)
3
>>> s.index(2, start=2, stop=5)
3
>>> s.index(6)
ValueError: CfList doesn't contain: 6
Insert an object before the given index in place.
Parameters : | index : int item : |
---|---|
Returns : | None |
Examples
>>> s
[1, 2, 3]
>>> s.insert(1, 'A')
>>> s
[1, 'A', 2, 3]
>>> s.insert(100, 'B')
[1, 'A', 2, 3, 'B']
>>> s.insert(100, 'B')
[1, 'A', 2, 3, 'B']
>>> s.insert(-2, 'C')
[1, 'A', 2, 'C', 3, 'B']
>>> s.insert(-100, 'D')
['D', 1, 'A', 2, 'C', 3, 'B']
Return a list of booleans showing which elements match the given conditions.
The match conditions are passed to each element’s match method in turn.
Parameters : |
|
---|---|
Returns : |
|
Examples
>>> fl
[<>
<>]
>>> fl.match(attr={'standard_name': 'air_temperature'})
[True, False]
Return a built-in list of the names of each element of the list of variables.
Override the each element’s data array units in place.
Not to be confused with setting the Units attribute to units which are equivalent to each element’s original units.
This is different to setting the Units attribute, as the new units need not be equivalent to the original ones and the each data array will not be changed to reflect the new units.
True if two instances are set-wise equal, False otherwise.
Two instances are set-wise equal if their attributes are equal and their elements are equal set-wise (i.e. the order of the lists is irrelevant).
Parameters : |
|
---|---|
Returns : |
|
Examples
Set a CF property on each element of the list.
Parameters : |
|
---|---|
Returns : | None |
Examples
>>> len(fl)
2
>>> fl.setprop('foo', 'bar')
>>> fl.getprop('foo')
['bar', 'bar']
Note that recognised CF propeties (such as ‘long_name’) may be set directly:
>>> fl.standard_name = 'air_temperature'
>>> fl.standard_name
['air_temperature', 'air_temperature']
Sort the elements in place.
The sort is stable which means that when multiple records have the same key, their original order is preserved.
Parameters : |
|
---|---|
Returns : | None |
Examples
Two ways of sorting by standard name:
>>> fl.sort(key=lambda f: f.standard_name)
>>> from operator import attrgetter
>>> fl.sort(key=attrgetter('standard_name'))
Get the elements in reverse standard name order:
>>> fl.sort(key=attrgetter('standard_name'), reverse=True)
Sort by standard name then by the value of the first element of the data array:
>>> fl.sort(key=attrgetter('standard_name', 'first_datum'))
Sort by standard name then by the value of the second element of the data array:
>>> fl.sort(key=lambda f: (f.standard_name, f.subspace[..., 1].first_datum))
Return the elements which match the given conditions.
The match conditions are passed to each element’s match method in turn.
Parameters : |
|
---|---|
Returns : |
|
Examples
>>> fl
[<>
<>]
>>> fl.subset(attr={'standard_name': 'air_temperature'})
[<>]
Subspace each variable in the list, returning a new list of variables.
Examples
>>> vl
[<CF Variable: air_temperature(73, 96)>,
<CF Variable: air_temperature(73, 96)>]
>>> vl.subspace[0,0]
[<CF Variable: air_temperature(1,1)>,
<CF Variable: air_temperature(1,1)>]