Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d7e770d

Browse files
committed
making utils independent
1 parent 22145eb commit d7e770d

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

plotly/grid_objs/grid_objs.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import json
1010
from collections import MutableSequence
1111
from plotly import exceptions
12-
import plotly
12+
from plotly import utils
1313

1414
__all__ = None
1515

@@ -24,7 +24,7 @@ def __init__(self, data, name):
2424

2525
def __str__(self):
2626
max_chars = 10
27-
jdata = json.dumps(self.data, cls=plotly.utils._plotlyJSONEncoder)
27+
jdata = json.dumps(self.data, cls=utils._plotlyJSONEncoder)
2828
if len(jdata) > max_chars:
2929
data_string = jdata[:max_chars] + "...]"
3030
else:
@@ -33,13 +33,16 @@ def __str__(self):
3333
return string.format(name=self.name, data=data_string, id=self.id)
3434

3535
def __repr__(self):
36-
return 'Column("{}", {})'.format(self.data, self.name)
36+
return 'Column("{}", {})'.format(self.data, self.name)
37+
38+
def to_json(self):
39+
return {'name': self.name, 'data': self.data}
3740

3841

3942
class Grid(MutableSequence):
4043
def __init__(self, iterable_of_columns):
4144
column_names = [column.name for column in iterable_of_columns]
42-
duplicate_name = plotly.utils.get_first_duplicate(column_names)
45+
duplicate_name = utils.get_first_duplicate(column_names)
4346
if duplicate_name:
4447
err = exceptions.NON_UNIQUE_COLUMN_MESSAGE.format(duplicate_name)
4548
raise exceptions.InputError(err)

plotly/utils.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import threading
1313
import re
1414

15-
import plotly
16-
1715
### incase people are using threading, we lock file reads
1816
lock = threading.Lock()
1917

@@ -128,19 +126,18 @@ def sageJSONEncoder(self, obj):
128126
pass
129127
return None
130128

131-
def ColumnJSONEncoder(self, obj):
132-
if isinstance(obj, plotly.grid_objs.Column):
133-
return {'name': obj.name, 'data': obj.data}
134-
else:
129+
def builtinJSONEncoder(self, obj):
130+
try:
131+
return obj.to_json()
132+
except AttributeError:
135133
return None
136134

137-
138135
def default(self, obj):
139136
try:
140137
return json.dumps(obj)
141138
except TypeError as e:
142-
encoders = (self.datetimeJSONEncoder, self.numpyJSONEncoder,
143-
self.pandasJSONEncoder, self.ColumnJSONEncoder,
139+
encoders = (self.builtinJSONEncoder, self.datetimeJSONEncoder,
140+
self.numpyJSONEncoder, self.pandasJSONEncoder,
144141
self.sageJSONEncoder)
145142
for encoder in encoders:
146143
s = encoder(obj)

0 commit comments

Comments
 (0)