Skip to content

Commit

Permalink
Add Element.__contains__ method (#2330)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored and jlstevens committed Feb 19, 2018
1 parent d05358f commit 98b2d62
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/Tutorials/Columnar_Data.ipynb
Expand Up @@ -315,7 +315,7 @@
"source": [
"xs = np.arange(10)\n",
"curve = hv.Curve(zip(xs, np.exp(xs)))\n",
"curve * hv.Scatter(zip(xs, curve)) + curve.table()"
"curve * hv.Scatter(curve) + curve.table()"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/user_guide/07-Tabular_Datasets.ipynb
Expand Up @@ -340,7 +340,7 @@
"source": [
"xs = np.arange(10)\n",
"curve = hv.Curve(zip(xs, np.exp(xs)))\n",
"curve * hv.Scatter(zip(xs, curve)) + curve.table()"
"curve * hv.Scatter(curve) + curve.table()"
]
},
{
Expand Down
16 changes: 16 additions & 0 deletions holoviews/core/element.py
Expand Up @@ -68,6 +68,22 @@ def __nonzero__(self):
"""
return True


def __contains__(self, dimension):
"""
Allows checking whether a Dimension is in the Elements key or
value dimensions.
"""
return dimension in self.dimensions()


def __iter__(self):
"""
Disable iterator interface.
"""
raise NotImplementedError('Iteration on Elements is not supported.')


__bool__ = __nonzero__


Expand Down
26 changes: 26 additions & 0 deletions tests/core/testelement.py
@@ -0,0 +1,26 @@
from holoviews.core import Dimension, Element
from holoviews.element.comparison import ComparisonTestCase


class ElementTests(ComparisonTestCase):

def setUp(self):
self.element = Element([], kdims=['A', 'B'], vdims=['C'])

def test_key_dimension_in_element(self):
self.assertTrue(Dimension('A') in self.element)

def test_value_dimension_in_element(self):
self.assertTrue(Dimension('C') in self.element)

def test_dimension_not_in_element(self):
self.assertFalse(Dimension('D') in self.element)

def test_key_dimension_string_in_element(self):
self.assertTrue('A' in self.element)

def test_value_dimension_string_in_element(self):
self.assertTrue('C' in self.element)

def test_dimension_string_not_in_element(self):
self.assertFalse('D' in self.element)

0 comments on commit 98b2d62

Please sign in to comment.