Skip to content

Commit 698408a

Browse files
committed
Add __contains__
1 parent 1aeb352 commit 698408a

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

cf_xarray/accessor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,12 @@ def __getattr__(self, attr):
728728
wrap_classes=True,
729729
)
730730

731+
def __contains__(self, item: str) -> bool:
732+
"""
733+
Check whether item is a valid key for indexing with .cf
734+
"""
735+
return item in self.get_valid_keys()
736+
731737
@property
732738
def plot(self):
733739
return _CFWrappedPlotMethods(self._obj, self)

cf_xarray/tests/test_accessor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ def test_dataset_plot(obj):
306306
),
307307
)
308308
def test_getitem(obj, key, expected_key):
309+
assert key in obj.cf
310+
309311
actual = obj.cf[key]
310312
if isinstance(obj, xr.Dataset):
311313
expected_key = [expected_key]

doc/examples/introduction.ipynb

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"execution_count": null,
1515
"metadata": {
1616
"ExecuteTime": {
17-
"end_time": "2020-06-30T23:25:30.534525Z",
18-
"start_time": "2020-06-30T23:25:29.588662Z"
17+
"end_time": "2020-07-24T20:12:00.609657Z",
18+
"start_time": "2020-07-24T20:11:59.698753Z"
1919
}
2020
},
2121
"outputs": [],
@@ -37,8 +37,8 @@
3737
"execution_count": null,
3838
"metadata": {
3939
"ExecuteTime": {
40-
"end_time": "2020-06-30T23:25:30.589498Z",
41-
"start_time": "2020-06-30T23:25:30.536512Z"
40+
"end_time": "2020-07-24T20:12:00.805176Z",
41+
"start_time": "2020-07-24T20:12:00.718615Z"
4242
}
4343
},
4444
"outputs": [],
@@ -61,8 +61,8 @@
6161
"execution_count": null,
6262
"metadata": {
6363
"ExecuteTime": {
64-
"end_time": "2020-06-30T23:25:30.651949Z",
65-
"start_time": "2020-06-30T23:25:30.591221Z"
64+
"end_time": "2020-07-24T20:12:02.140074Z",
65+
"start_time": "2020-07-24T20:12:02.050083Z"
6666
}
6767
},
6868
"outputs": [],
@@ -434,15 +434,36 @@
434434
"execution_count": null,
435435
"metadata": {
436436
"ExecuteTime": {
437-
"end_time": "2020-06-30T23:25:31.205009Z",
438-
"start_time": "2020-06-30T23:25:31.186385Z"
437+
"end_time": "2020-07-24T20:12:06.956464Z",
438+
"start_time": "2020-07-24T20:12:06.908943Z"
439439
}
440440
},
441441
"outputs": [],
442442
"source": [
443443
"pop.cf.get_valid_keys()"
444444
]
445445
},
446+
{
447+
"cell_type": "markdown",
448+
"metadata": {},
449+
"source": [
450+
"You can test for presence of these keys\n"
451+
]
452+
},
453+
{
454+
"cell_type": "code",
455+
"execution_count": null,
456+
"metadata": {
457+
"ExecuteTime": {
458+
"end_time": "2020-07-24T20:12:36.699151Z",
459+
"start_time": "2020-07-24T20:12:36.664209Z"
460+
}
461+
},
462+
"outputs": [],
463+
"source": [
464+
"\"sea_water_x_velocity\" in pop.cf"
465+
]
466+
},
446467
{
447468
"cell_type": "markdown",
448469
"metadata": {},

0 commit comments

Comments
 (0)