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

Skip to content

Commit ba84784

Browse files
committed
Added 'get_ordered' method to graph_objects. returns an ordered dict.
Notes: 1. references in the returned dict are NOT broken (no copies) 2. returned dict is ordered according to graph_references' INFO
1 parent 3668850 commit ba84784

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

plotly/graph_objs/graph_objs.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,21 @@ def to_string(self, level=0, indent=4, eol='\n', pretty=True, max_chars=80):
434434
string += "{eol}{indent}])".format(eol=eol, indent=' ' * indent * level)
435435
return string
436436

437+
def get_ordered(self, caller=True):
438+
if caller:
439+
try:
440+
self.to_graph_objs(caller=False)
441+
except exceptions.PlotlyGraphObjectError as err:
442+
err.add_note("Could not order list because it could not be "
443+
"converted to a valid graph object.")
444+
err.prepare()
445+
raise
446+
ordered_list = []
447+
for index, entry in enumerate(self):
448+
ordered_list += [entry.get_ordered()]
449+
return ordered_list
450+
451+
437452
def force_clean(self, caller=True):
438453
"""Attempts to convert to graph_objs and calls force_clean() on entries.
439454
@@ -753,6 +768,34 @@ def to_string(self, level=0, indent=4, eol='\n', pretty=True, max_chars=80):
753768
string += "{eol}{indent})".format(eol=eol, indent=' ' * indent * level)
754769
return string
755770

771+
def get_ordered(self, caller=True):
772+
if caller: # change everything to 'order-able' objs
773+
try:
774+
self.to_graph_objs(caller=False)
775+
except exceptions.PlotlyGraphObjectError as err:
776+
err.add_note("dictionary could not be ordered because it "
777+
"could not be converted to a valid plotly graph "
778+
"object.")
779+
err.prepare()
780+
raise
781+
obj_type = NAME_TO_KEY[self.__class__.__name__]
782+
ordered_dict = collections.OrderedDict()
783+
# grab keys like xaxis1, xaxis2, etc...
784+
unordered_keys = [key for key in self if key not in INFO[obj_type]]
785+
for key in INFO[obj_type]:
786+
if key in self:
787+
if isinstance(self[key], (PlotlyDict, PlotlyList)):
788+
ordered_dict[key] = self[key].get_ordered(caller=False)
789+
else:
790+
ordered_dict[key] = self[key]
791+
for key in unordered_keys:
792+
if isinstance(self[key], (PlotlyDict, PlotlyList)):
793+
ordered_dict[key] = self[key].get_ordered(caller=False)
794+
else:
795+
ordered_dict[key] = self[key]
796+
return ordered_dict
797+
798+
756799
def force_clean(self, caller=True):
757800
"""Attempts to convert to graph_objs and call force_clean() on values.
758801

0 commit comments

Comments
 (0)