Skip to content

Commit 9a701a2

Browse files
committed
Andrew's docs comments - adding docs on various params
1 parent f9f3bdc commit 9a701a2

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

plotly/colors.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ def color_parser(colors, function):
159159
the color or iterable of colors. If given an iterable, it will only be
160160
able to work with it if all items in the iterable are of the same type
161161
- rgb string, hex string or tuple
162-
163162
"""
164163
if isinstance(colors, str):
165164
return function(colors)
@@ -180,6 +179,10 @@ def color_parser(colors, function):
180179
def validate_colors(colors, colors_list=None):
181180
"""
182181
Validates color(s) and returns an error for invalid color(s)
182+
183+
:param (list) colors_list: whether a singleton color or a list/tuple of
184+
colors is inputted, all the color types are appended to colors_list
185+
so they can be easily iterated through for validation
183186
"""
184187
if colors_list is None:
185188
colors_list = []
@@ -191,8 +194,8 @@ def validate_colors(colors, colors_list=None):
191194
colors_list.append(colors)
192195
else:
193196
raise exceptions.PlotlyError(
194-
"If your colors variable is a string, it must be a "
195-
"Plotly scale, an rgb color or a hex color."
197+
'If your colors variable is a string, it must be a '
198+
'Plotly scale, an rgb color or a hex color.'
196199
)
197200

198201
elif isinstance(colors, tuple):
@@ -216,8 +219,8 @@ def validate_colors(colors, colors_list=None):
216219
for value in each_color:
217220
if value > 255.0:
218221
raise exceptions.PlotlyError(
219-
"Whoops! The elements in your rgb colors "
220-
"tuples cannot exceed 255.0."
222+
'Whoops! The elements in your rgb colors '
223+
'tuples cannot exceed 255.0.'
221224
)
222225
elif '#' in each_color:
223226
each_color = color_parser(
@@ -227,10 +230,9 @@ def validate_colors(colors, colors_list=None):
227230
for value in each_color:
228231
if value > 1.0:
229232
raise exceptions.PlotlyError(
230-
"Whoops! The elements in your colors tuples "
231-
"cannot exceed 1.0."
233+
'Whoops! The elements in your colors tuples '
234+
'cannot exceed 1.0.'
232235
)
233-
return
234236

235237

236238
def convert_colors_to_same_type(colors, colortype='rgb', scale=None,
@@ -247,6 +249,12 @@ def convert_colors_to_same_type(colors, colortype='rgb', scale=None,
247249
from the respective colorscale and the colors in that colorscale will also
248250
be coverted to the selected colortype. If colors is None, then there is an
249251
option to return portion of the DEFAULT_PLOTLY_COLORS
252+
253+
:param (list) colors_list: see docs for validate_colors()
254+
:param (list) scale: see docs for validate_scale_values()
255+
256+
:rtype (tuple) (colors_list, scale) if scale is None in the function call,
257+
then scale will remain None in the returned tuple
250258
"""
251259
if colors_list is None:
252260
colors_list = []
@@ -278,8 +286,8 @@ def convert_colors_to_same_type(colors, colortype='rgb', scale=None,
278286

279287
if len(colors_list) != len(scale):
280288
raise exceptions.PlotlyError(
281-
"Make sure that the length of your scale matches the length "
282-
"of your list of colors which is {}.".format(len(colors_list))
289+
'Make sure that the length of your scale matches the length '
290+
'of your list of colors which is {}.'.format(len(colors_list))
283291
)
284292

285293
# convert all colors to rgb
@@ -315,8 +323,8 @@ def convert_colors_to_same_type(colors, colortype='rgb', scale=None,
315323
colors_list[j] = each_color
316324
return (colors_list, scale)
317325
else:
318-
raise exceptions.PlotlyError("You must select either rgb or tuple "
319-
"for your colortype variable.")
326+
raise exceptions.PlotlyError('You must select either rgb or tuple '
327+
'for your colortype variable.')
320328

321329

322330
def convert_dict_colors_to_same_type(colors, colortype='rgb'):
@@ -356,31 +364,36 @@ def convert_dict_colors_to_same_type(colors, colortype='rgb'):
356364
)
357365
return colors
358366
else:
359-
raise exceptions.PlotlyError("You must select either rgb or tuple "
360-
"for your colortype variable.")
367+
raise exceptions.PlotlyError('You must select either rgb or tuple '
368+
'for your colortype variable.')
361369

362370

363371
def validate_scale_values(scale):
364372
"""
365373
Validates scale values from a colorscale
374+
375+
:param (list) scale: a strictly increasing list of floats that begins
376+
with 0 and ends with 1. Its usage derives from a colorscale which is
377+
a list of two-lists (a list with two elements) of the form
378+
[value, color] which are used to determine how interpolation weighting
379+
works between the colors in the colorscale. Therefore scale is just
380+
the extraction of these values from the two-lists in order
366381
"""
367382
if len(scale) < 2:
368-
raise exceptions.PlotlyError("You must input a list of scale values "
369-
"that has at least two values.")
383+
raise exceptions.PlotlyError('You must input a list of scale values '
384+
'that has at least two values.')
370385

371386
if (scale[0] != 0) or (scale[-1] != 1):
372387
raise exceptions.PlotlyError(
373-
"The first and last number in your scale must be 0.0 and 1.0 "
374-
"respectively."
388+
'The first and last number in your scale must be 0.0 and 1.0 '
389+
'respectively.'
375390
)
376391

377-
for j in range(1, len(scale)):
378-
if scale[j] <= scale[j-1]:
392+
if not all(x < y for x, y in zip(scale, scale[1:])):
379393
raise exceptions.PlotlyError(
380-
"'scale' must be a list that contains an increasing "
394+
"'scale' must be a list that contains a strictly increasing "
381395
"sequence of numbers."
382396
)
383-
return
384397

385398

386399
def make_colorscale(colors, scale=None, colorscale=None):
@@ -399,17 +412,17 @@ def make_colorscale(colors, scale=None, colorscale=None):
399412

400413
# validate minimum colors length of 2
401414
if len(colors) < 2:
402-
raise exceptions.PlotlyError("You must input a list of colors that "
403-
"has at least two colors.")
415+
raise exceptions.PlotlyError('You must input a list of colors that '
416+
'has at least two colors.')
404417

405418
if scale is None:
406419
scale_incr = 1./(len(colors) - 1)
407420
return [[i * scale_incr, color] for i, color in enumerate(colors)]
408421

409422
else:
410423
if len(colors) != len(scale):
411-
raise exceptions.PlotlyError("The length of colors and scale "
412-
"must be the same.")
424+
raise exceptions.PlotlyError('The length of colors and scale '
425+
'must be the same.')
413426

414427
validate_scale_values(scale)
415428

@@ -424,7 +437,6 @@ def find_intermediate_color(lowcolor, highcolor, intermed):
424437
This function takes two color tuples, where each element is between 0
425438
and 1, along with a value 0 < intermed < 1 and returns a color that is
426439
intermed-percent from lowcolor to highcolor
427-
428440
"""
429441
diff_0 = float(highcolor[0] - lowcolor[0])
430442
diff_1 = float(highcolor[1] - lowcolor[1])
@@ -442,7 +454,6 @@ def unconvert_from_RGB_255(colors):
442454
Takes a (list of) color tuple(s) where each element is between 0 and
443455
255. Returns the same tuples where each tuple element is normalized to
444456
a value between 0 and 1
445-
446457
"""
447458
return (colors[0]/(255.0),
448459
colors[1]/(255.0),
@@ -458,6 +469,9 @@ def convert_to_RGB_255(colors, rgb_components=None):
458469
if x is odd, the number rounds up to (x+1). Otherwise, it rounds down
459470
to just x. This is the way rounding works in Python 3 and in current
460471
statistical analysis to avoid rounding bias
472+
473+
:param (list) rgb_components: grabs the three R, G and B values to be
474+
returned as computed in the function
461475
"""
462476
if rgb_components is None:
463477
rgb_components = []
@@ -480,7 +494,6 @@ def n_colors(lowcolor, highcolor, n_colors):
480494
Accepts two color tuples and returns a list of n_colors colors
481495
which form the intermediate colors between lowcolor and highcolor
482496
from linearly interpolating through RGB space
483-
484497
"""
485498
diff_0 = float(highcolor[0] - lowcolor[0])
486499
incr_0 = diff_0/(n_colors - 1)
@@ -512,7 +525,6 @@ def unlabel_rgb(colors):
512525
513526
This function takes either an 'rgb(a, b, c)' color or a list of
514527
such colors and returns the color tuples in tuple(s) (a, b, c)
515-
516528
"""
517529
str_vals = ''
518530
for index in range(len(colors)):

plotly/tests/test_core/test_colors/test_colors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ def test_validate_scale_values(self):
9595
# test numbers increase
9696
scale = [0, 2, 1]
9797

98-
pattern = ("'scale' must be a list that contains an increasing "
99-
"sequence of numbers.")
98+
pattern = ("'scale' must be a list that contains a strictly "
99+
"increasing sequence of numbers.")
100100

101101
self.assertRaisesRegexp(PlotlyError, pattern,
102102
colors.validate_scale_values,

0 commit comments

Comments
 (0)