Skip to content

Commit 0e644f0

Browse files
committed
Add utility function to check for ‘data’ type.
* is the type ‘data’ * is it ‘style’, but as an array?
1 parent c102140 commit 0e644f0

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

plotly/graph_objs/graph_objs_tools.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,36 @@ def curtail_val_repr(val, max_chars, add_delim=False):
226226
if add_delim:
227227
r += delim
228228
return r
229+
230+
231+
def value_is_data(obj_name, key, value):
232+
"""
233+
Values have types associated with them based on graph_reference.
234+
235+
'data' type values are always kept
236+
'style' values are kept if they're sequences (but not strings)
237+
238+
:param (str) obj_name: E.g., 'scatter', 'figure'
239+
:param (str) key: E.g., 'x', 'y', 'text'
240+
:param (*) value:
241+
:returns: (bool)
242+
243+
"""
244+
try:
245+
key_type = INFO[obj_name]['keymeta'][key]['key_type']
246+
except KeyError:
247+
return False
248+
249+
if key_type not in ['data', 'style']:
250+
return False
251+
252+
if key_type == 'data':
253+
return True
254+
255+
if key_type == 'style':
256+
iterable = hasattr(value, '__iter__')
257+
stringy = isinstance(value, six.string_types)
258+
dicty = isinstance(value, dict)
259+
return iterable and not stringy and not dicty
260+
261+
return False
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from __future__ import absolute_import
2+
3+
from unittest import TestCase
4+
5+
try:
6+
from collections import OrderedDict
7+
except ImportError:
8+
from ordereddict import OrderedDict
9+
10+
from plotly.graph_objs.graph_objs_tools import value_is_data
11+
12+
13+
class TestValueIsData(TestCase):
14+
15+
def test_unknown_strings(self):
16+
self.assertFalse(value_is_data('scatter', 'blah', ''))
17+
self.assertFalse(value_is_data('huh?', 'text', None))
18+
19+
def test_data_value_type(self):
20+
self.assertTrue(value_is_data('scatter', 'x', {}))
21+
self.assertTrue(value_is_data('bar', 'name', 'bill'))
22+
self.assertTrue(value_is_data('histogram', 'x', [5, 5]))
23+
self.assertTrue(value_is_data('xaxis', 'range', [0, 5]))
24+
25+
def test_style_value_type(self):
26+
self.assertFalse(value_is_data('marker', 'color', 'red'))
27+
self.assertTrue(value_is_data('marker', 'color', ['red', 'blue']))
28+
self.assertTrue(value_is_data('marker', 'opacity', (.9, .5)))
29+
self.assertFalse(value_is_data('marker', 'symbol', {}))

0 commit comments

Comments
 (0)