From 3e06214b7e77f27f7d290321c865075ad33b0955 Mon Sep 17 00:00:00 2001 From: jan Date: Sat, 14 Apr 2018 14:53:53 -0700 Subject: [PATCH] Add recursive "apply()" helper to pattern --- masque/pattern.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/masque/pattern.py b/masque/pattern.py index 7af9654..8b8575f 100644 --- a/masque/pattern.py +++ b/masque/pattern.py @@ -94,6 +94,22 @@ class Pattern: pat.subpatterns = [s for s in self.subpatterns if subpatterns_func(s)] return pat + def apply(self, + func: Callable[['Pattern'], 'Pattern'] + ) -> 'Pattern': + """ + Recursively apply func() to this pattern and its subpatterns. + func() is expected to take and return a Pattern. + func() is first applied to the pattern as a whole, then the subpatterns. + + :param func: Function which accepts a Pattern, and returns a pattern. + :return: The result of applying func() to this pattern and all subpatterns. + """ + pat = func(self) + for subpat in pat.subpatterns: + subpat.pattern = subpat.pattern.apply(func) + return pat + def polygonize(self, poly_num_points: int=None, poly_max_arclen: float=None