[Python-modules-commits] [python-mplexporter] 110/135: Pull out recursive legend item search into a utility function

Wolfgang Borgert debacle at moszumanska.debian.org
Tue Sep 23 21:19:09 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 44d236546e204246114767b5ff0700e24a013177
Author: John Eppley <jmeppley at gmail.com>
Date:   Wed Mar 26 12:55:52 2014 -0400

    Pull out recursive legend item search into a utility function
---
 mplexporter/exporter.py | 20 +++++++-------------
 mplexporter/utils.py    | 17 +++++++++++++++++
 2 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/mplexporter/exporter.py b/mplexporter/exporter.py
index 90047d7..9911247 100644
--- a/mplexporter/exporter.py
+++ b/mplexporter/exporter.py
@@ -144,19 +144,16 @@ class Exporter(object):
                 props = utils.get_legend_properties(ax, legend)
                 with self.renderer.draw_legend(legend=legend, props=props):
                     if props['visible']:
-                        self.crawl_legend(ax, legend._legend_box)
-                        if legend.draw_frame:
-                            legend.legendPatch.set_zorder(1E6 + 
-                                    legend.legendPatch.get_zorder())
-                            self.draw_patch(ax, legend.legendPatch,
-                                    force_trans=ax.transAxes)
+                        self.crawl_legend(ax, legend)
 
-
-    def crawl_legend(self, ax, obj):
+    def crawl_legend(self, ax, legend):
         """
         Recursively look through objects in legend children
         """
-        for child in obj.get_children():
+        legendElements = list(utils.iter_all_children(legend._legend_box,
+                                                      skipContainers=True))
+        legendElements.append(legend.legendPatch)
+        for child in legendElements:
             # force a large zorder so it appears on top
             child.set_zorder(1E6 + child.get_zorder())
 
@@ -164,14 +161,11 @@ class Exporter(object):
             if isinstance(child, matplotlib.patches.Patch):
                 self.draw_patch(ax, child, force_trans=ax.transAxes)
             elif isinstance(child, matplotlib.text.Text):
-                if not (child is obj.get_children()[-1]
+                if not (child is legend.get_children()[-1]
                         and child.get_text() == 'None'):
                     self.draw_text(ax, child, force_trans=ax.transAxes)
             elif isinstance(child, matplotlib.lines.Line2D):
                 self.draw_line(ax, child, force_trans=ax.transAxes)
-            elif child.get_children() is not None \
-                    and len(child.get_children())>0:
-                self.crawl_legend(ax, child)
             else:
                 warnings.warn("Legend element %s not impemented" & child)
 
diff --git a/mplexporter/utils.py b/mplexporter/utils.py
index 78e9492..87f8ad0 100644
--- a/mplexporter/utils.py
+++ b/mplexporter/utils.py
@@ -297,6 +297,23 @@ def get_axes_properties(ax):
 
     return props
 
+def iter_all_children(obj, skipContainers=False):    
+    """
+    Returns an iterator over all childen and nested children using
+    obj's get_children() method
+
+    if skipContainers is true, only childless objects are returned.
+    """
+    if hasattr(obj, 'get_children') and len(obj.get_children())>0:
+        for child in obj.get_children():
+            if not skipContainers:
+                yield child
+            # could use `yield from` in python 3...
+            for grandchild in iter_all_children(child,
+                    skipContainers=skipContainers):
+                yield grandchild
+    else:
+        yield obj
 
 def get_legend_properties(ax, legend):
     handles, labels = ax.get_legend_handles_labels()

-- 
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