[Python-modules-commits] [python-mplexporter] 63/135: Added plotly_utils.clean_dict(). Removes None. 'None', 'none', and {}.

Wolfgang Borgert debacle at moszumanska.debian.org
Tue Sep 23 21:19:04 UTC 2014


This is an automated email from the git hooks/post-receive script.

debacle pushed a commit to branch master
in repository python-mplexporter.

commit 2b2a6b459ddc04d0e9bd0f7b7ace8794feb7102d
Author: theengineear <andseier at gmail.com>
Date:   Thu Feb 27 15:34:19 2014 -0800

    Added plotly_utils.clean_dict(). Removes None. 'None', 'none', and {}.
    
    JSON dictionaries sent to plotly should not have entries that contain
    'none', 'None', None, or are themselves empty dictionaries ({}). This
    should be updated at some point to disregard entries that are expected
    to be labels so that users could use 'None' or 'none' to label a plot.
    This is pretty unlikely, but should be changed.
    
    clean_dict(node, partent=None, node_key=None) is a recursive function to
    clean out a dictionary. Only nodes that are dictionaries are looped back
    through the function. The JSON structures are dictionary-based, while
    matplotlib structures are lists, no unintentional deletion problems have
    surface yet.
---
 mplexporter/renderers/plotly/plotly_renderer.py | 19 ++++++++-----------
 mplexporter/renderers/plotly/plotly_utils.py    | 16 ++++++++++++++++
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/mplexporter/renderers/plotly/plotly_renderer.py b/mplexporter/renderers/plotly/plotly_renderer.py
index 1f76248..2a49ff6 100644
--- a/mplexporter/renderers/plotly/plotly_renderer.py
+++ b/mplexporter/renderers/plotly/plotly_renderer.py
@@ -27,6 +27,9 @@ class PlotlyRenderer(Renderer):
     def close_figure(self, fig):
         self.output += "closing figure\n"
         self.configure_primary_axes()  # changes 'y1', 'xaxis1', etc. to 'y', 'xaxis', etc.
+        for data_dict in self.data:
+            plotly_utils.clean_dict(data_dict)
+        plotly_utils.clean_dict(self.layout)
         self.layout['showlegend'] = False
 
     def open_axes(self, ax, props):
@@ -100,16 +103,11 @@ class PlotlyRenderer(Renderer):
             self.output += "    received {} markers with 'figure' coordinates, skipping!".format(data.shape[0])
 
     def configure_primary_axes(self):
-        try:
-            for axis_no in range(0, len(self.data)):
-                if self.data[axis_no]['xaxis'] == 'x1':
-                    del self.data[axis_no]['xaxis']
-                if self.data[axis_no]['yaxis'] == 'y1':
-                    del self.data[axis_no]['yaxis']
-        except KeyError:
-            pass
-        except IndexError:
-            pass
+        for data_dict in self.data:
+            if 'xaxis' in data_dict and data_dict['xaxis'] == 'x1':
+                del data_dict['xaxis']
+            if 'yaxis' in data_dict and data_dict['yaxis'] == 'y1':
+                del data_dict['yaxis']
         if 'xaxis1' in self.layout:
             self.layout['xaxis'] = self.layout.pop('xaxis1')
         if 'yaxis1' in self.layout:
@@ -125,7 +123,6 @@ class PlotlyRenderer(Renderer):
         except KeyError:
             pass
 
-
 def fig_to_plotly(fig, username=None, api_key=None, notebook=False):
     """Convert a matplotlib figure to plotly dictionary
 
diff --git a/mplexporter/renderers/plotly/plotly_utils.py b/mplexporter/renderers/plotly/plotly_utils.py
index c526d0b..7440502 100644
--- a/mplexporter/renderers/plotly/plotly_utils.py
+++ b/mplexporter/renderers/plotly/plotly_utils.py
@@ -20,6 +20,22 @@ def get_y_domain(bounds):
     return [bounds[1], bounds[1] + bounds[3]]
 
 
+def clean_dict(node, parent=None, node_key=None):
+    del_keys = []
+    for key, item in node.items():
+        if isinstance(item, dict):
+            clean_dict(item, node, key)
+        else:
+            if item in [None, 'none', 'None']:
+                del_keys += [key]
+    for key in del_keys:
+        del node[key]
+    if parent is not None:
+        if len(node) == 0:
+            del parent[node_key]
+
+
+
 dash_map = {
     '10,0': 'solid',
     '6,6': 'dash',

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-mplexporter.git



More information about the Python-modules-commits mailing list