Repository: plotly/plotly.py

Issue:
Add key ordering back to python api.
There's no order in the new plot schema.



Discussion:
Wouldn't loading up the plot-schema json with as an ordered dict suffice?

sorta. i think this is still hard to test though, if we made a change in the plot-schema and the new hashes changed the order of the stored json, then loading this new json as an ordered dict would break tests, right? we _have_ to set an order in `plot-schema.json` before we can start using the `OrderedDict` `ordered_pairs` hook. Right?

@theengineear That sounds like another syncing nightmare.

Why would we need to test the order of the plot-schema?

Doesn't validation only care of the existence of the attributes, not their position in the plot-schema?

Are you concerned about the output of the `help` method?

Testing `to_string` is nearly impossible without having a predictable order.

Just in case we're not on the same page. As far as updating the python api goes, I really don't care or need the plot schema to have a consistent order. I do some _exceedingly_ simple ordering in the python api so that the `to_string` test result will pass. But that's all. This is highly :football:-able as far as I'm concerned.

I created an issue because we _would_ ideally like better `help` and I think there's some documentation-related needs for ordering in the plot schema.

The python api has no intention of testing full ordering, but just a _small_ subset needs to be predictable in order to test the `to_string` method.

@chriddyp :wind_chime:-ed in on this earlier though and might have more opinions on it.

@theengineear all good :+1: 


Resolving patch:
diff --git a/plotly/graph_objs/graph_objs.py b/plotly/graph_objs/graph_objs.py
--- a/plotly/graph_objs/graph_objs.py
+++ b/plotly/graph_objs/graph_objs.py
@@ -25,10 +25,11 @@
 from __future__ import absolute_import
 
 import copy
-
 import re
-import six
 import warnings
+from collections import OrderedDict
+
+import six
 
 from plotly import exceptions, graph_reference
 from plotly.graph_objs import graph_objs_tools
@@ -113,17 +114,6 @@ def validate(self):
         """Everything is *always* validated now. keep for backwards compat."""
         pass
 
-    def get_ordered(self, **kwargs):
-        """
-        We have no way to order things anymore. Keep for backwards compat.
-
-        See https://github.com/plotly/python-api/issues/290.
-
-        :return: (PlotlyBase)
-
-        """
-        return self
-
 
 class PlotlyList(list, PlotlyBase):
     """
@@ -311,6 +301,10 @@ def get_data(self, flatten=False):
         else:
             return l
 
+    def get_ordered(self, **kwargs):
+        """All children are already validated. Just use get_ordered on them."""
+        return [child.get_ordered for child in self]
+
     def to_string(self, level=0, indent=4, eol='\n',
                   pretty=True, max_chars=80):
         """Get formatted string by calling `to_string` on children items."""
@@ -668,6 +662,17 @@ def get_data(self, flatten=False):
                     del d[key]
         return d
 
+    def get_ordered(self, **kwargs):
+        """Return a predictable, OrderedDict version of self."""
+        keys = sorted(self.keys(), key=graph_objs_tools.sort_keys)
+        ordered = OrderedDict()
+        for key in keys:
+            if isinstance(self[key], PlotlyBase):
+                ordered[key] = self[key].get_ordered()
+            else:
+                ordered[key] = self[key]
+        return ordered
+
     def to_string(self, level=0, indent=4, eol='\n',
                   pretty=True, max_chars=80):
         """
diff --git a/plotly/version.py b/plotly/version.py
--- a/plotly/version.py
+++ b/plotly/version.py
@@ -1 +1 @@
-__version__ = '1.8.9'
+__version__ = '1.8.10'


Test patch: