Skip to content

Commit 0dd4171

Browse files
committed
pep8, tests
1 parent 7c382aa commit 0dd4171

9 files changed

Lines changed: 1449 additions & 108 deletions

File tree

gridspec.html

Lines changed: 1150 additions & 0 deletions
Large diffs are not rendered by default.

plotly/exceptions.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,22 @@ class InputError(Exception):
2727
pass
2828

2929

30-
def ColumnHasntBeenUploadedErrorMessage(column_name, src):
31-
return ("Hm... it looks like your "
32-
"column \"{}\" hasn't been "
33-
"uploaded to Plotly yet. "
34-
"You need to upload your "
35-
"column to Plotly before "
36-
"you can assign it to {}.\n"
37-
"To upload, try "
38-
"`plotly.plotly.grid_objs."
39-
"upload` or "
40-
"`plotly.plotly.grid_objs."
41-
"append_column`.\n"
42-
"Questions? chris@plot.ly")\
43-
.format(column_name, src)
30+
## Grid Errors ##
31+
32+
COLUMN_NOT_YET_UPLOADED_MESSAGE = (
33+
"Hm... it looks like your column '{column_name}' hasn't "
34+
"been uploaded to Plotly yet. You need to upload your "
35+
"column to Plotly before you can assign it to '{reference}'.\n"
36+
"To upload, try `plotly.plotly.grid_objs.upload` or "
37+
"`plotly.plotly.grid_objs.append_column`.\n"
38+
"Questions? chris@plot.ly"
39+
)
40+
41+
NON_UNIQUE_COLUMN_MESSAGE = (
42+
"Yikes, plotly grids currently "
43+
"can't have duplicate column names. Rename "
44+
"the column \"{}\" and try again."
45+
)
4446

4547

4648
## Graph Objects Errors ##

plotly/graph_objs/graph_objs.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,7 @@ def __init__(self, *args, **kwargs):
301301

302302
for src in ('xsrc', 'ysrc'):
303303
if src in kwargs and isinstance(kwargs[src], Column):
304-
column = kwargs[src]
305-
if column.id == '':
306-
raise exceptions.InputError(exceptions.
307-
ColumnHasntBeenUploadedErrorMessage(column.name, src))
308-
kwargs[src] = kwargs[src].id
304+
kwargs[src] = self._assign_id_to_src(src, kwargs[src])
309305

310306
super(PlotlyDict, self).__init__(*args, **kwargs)
311307
if issubclass(NAME_TO_CLASS[class_name], PlotlyTrace):
@@ -318,15 +314,22 @@ def __init__(self, *args, **kwargs):
318314
"a user interface.")
319315

320316
def __setitem__(self, key, value):
321-
for src in ('xsrc', 'ysrc'):
322-
if src == key and isinstance(value, Column):
323-
if value.id == '':
324-
raise exceptions.InputError(exceptions.
325-
ColumnHasntBeenUploadedErrorMessage(value.name, src))
326-
value = value.id
317+
if key in ('xsrc', 'ysrc'):
318+
value = self._assign_id_to_src(key, value)
327319

328320
return super(PlotlyDict, self).__setitem__(key, value)
329321

322+
def _assign_id_to_src(self, src_name, src_value):
323+
if isinstance(src_value, Column):
324+
if src_value.id == '':
325+
err = exceptions.COLUMN_NOT_YET_UPLOADED_MESSAGE
326+
err.format(column_name=src_value.name, reference=src_name)
327+
raise exceptions.InputError(err)
328+
else:
329+
src_value = src_value.id
330+
return src_value
331+
332+
330333
def update(self, dict1=None, **dict2):
331334
"""Update current dict with dict1 and then dict2.
332335

plotly/grid_objs/grid_objs.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import json
88
from collections import MutableSequence
9+
from plotly import exceptions
10+
from plotly import utils
911

1012

1113
class Column():
@@ -17,20 +19,28 @@ def __init__(self, name, data):
1719

1820
self.id = ''
1921

20-
def __repr__(self):
22+
def __str__(self):
2123
jdata = json.dumps(self.data)
22-
return '<Column "{name}": {data}{ellipses}>'.format(
24+
return ('<name="{name}", '
25+
'data={data}{ellipses}, '
26+
'id={id}>').format(
2327
name=self.name,
2428
data=jdata[0:10],
25-
ellipses='...]' if len(jdata) > 20 else '')
29+
ellipses='...]' if len(jdata) > 10 else '',
30+
id=self.id)
31+
32+
def __repr__(self):
33+
return 'Coumn("{name}", {data})'.format(name=self.name, data=self.data)
2634

2735

2836
class Grid(MutableSequence):
2937
def __init__(self, iterable_of_columns):
3038
column_names = [column.name for column in iterable_of_columns]
31-
if (len(column_names) > len(set(column_names))):
32-
# TODO: Descriptive exception
33-
raise Exception('duplicate!')
39+
duplicate_name = utils.get_first_duplicate(column_names)
40+
if duplicate_name:
41+
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(duplicate_name)
42+
raise exceptions.InputError(err)
43+
3444
self._columns = list(iterable_of_columns)
3545
self.id = ''
3646

@@ -60,8 +70,10 @@ def _validate_insertion(self, column):
6070
"""
6171
existing_column_names = [col.name for col in self._columns]
6272
if column.name in existing_column_names:
63-
# TODO: Descriptive exception
64-
raise Exception('duplicate')
73+
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(column.name)
74+
raise exceptions.InputError(err)
6575

6676
def get_column(self, column_name):
67-
return next(column for column in self._columns if column.name==column_name)
77+
for column in self._columns:
78+
if column.name == column_name:
79+
return column
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import json
2+
3+
from plotly.grid_objs.grid_objs import Column
4+
5+
6+
class ColumnJSONEncoder(json.JSONEncoder):
7+
def default(self, obj):
8+
if isinstance(obj, Column):
9+
return {'name': obj.name, 'data': obj.data}
10+
else:
11+
return json.JSONEncoder.default(self, obj)

0 commit comments

Comments
 (0)