commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 19/25: grc-refactor: minor clean-up of call


From: git
Subject: [Commit-gnuradio] [gnuradio] 19/25: grc-refactor: minor clean-up of callback generator code
Date: Fri, 27 May 2016 19:14:59 +0000 (UTC)

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

jcorgan pushed a commit to branch master
in repository gnuradio.

commit 6647de1109cd0dffbb8668797a5bd98f54ffa053
Author: Sebastian Koslowski <address@hidden>
Date:   Tue May 17 13:23:18 2016 +0200

    grc-refactor: minor clean-up of callback generator code
---
 grc/core/generator/Generator.py    | 30 ++++++++++++++++--------------
 grc/core/generator/flow_graph.tmpl |  6 +++---
 grc/core/utils/expr_utils.py       | 11 +++--------
 3 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/grc/core/generator/Generator.py b/grc/core/generator/Generator.py
index 4788711..ba272ab 100644
--- a/grc/core/generator/Generator.py
+++ b/grc/core/generator/Generator.py
@@ -141,7 +141,7 @@ class TopBlockGenerator(object):
                 pass
             return code
 
-        blocks = expr_utils.sort_objects(
+        blocks_all = expr_utils.sort_objects(
             filter(lambda b: b.get_enabled() and not b.get_bypassed(), 
fg.blocks),
             lambda b: b.get_id(), _get_block_sort_text
         )
@@ -150,7 +150,7 @@ class TopBlockGenerator(object):
             Messages.send_warning("The block {!r} is deprecated.".format(key))
 
         # List of regular blocks (all blocks minus the special ones)
-        blocks = filter(lambda b: b not in (imports + parameters), blocks)
+        blocks = filter(lambda b: b not in (imports + parameters), blocks_all)
 
         for block in blocks:
             key = block.get_key()
@@ -214,20 +214,22 @@ class TopBlockGenerator(object):
 
         connection_templates = fg.get_parent().connection_templates
         msgs = filter(lambda c: c.is_msg(), fg.get_enabled_connections())
+
         # List of variable names
         var_ids = [var.get_id() for var in parameters + variables]
-        # Prepend self.
-        replace_dict = dict([(var_id, 'self.%s' % var_id) for var_id in 
var_ids])
-        # List of callbacks
-        callbacks = [
-            expr_utils.expr_replace(cb, replace_dict)
-            for cb in sum([block.get_callbacks() for block in 
fg.get_enabled_blocks()], [])
-            ]
+        replace_dict = dict((var_id, 'self.' + var_id) for var_id in var_ids)
+        callbacks_all = []
+        for block in blocks_all:
+            callbacks_all.extend(expr_utils.expr_replace(cb, replace_dict) for 
cb in block.get_callbacks())
+
         # Map var id to callbacks
-        var_id2cbs = dict([
-            (var_id, filter(lambda c: expr_utils.get_variable_dependencies(c, 
[var_id]), callbacks))
-            for var_id in var_ids
-        ])
+        def uses_var_id():
+            return expr_utils.get_variable_dependencies(callback, [var_id])
+
+        callbacks = {}
+        for var_id in var_ids:
+            callbacks[var_id] = [callback for callback in callbacks_all if 
uses_var_id()]
+
         # Load the namespace
         namespace = {
             'title': title,
@@ -241,7 +243,7 @@ class TopBlockGenerator(object):
             'connection_templates': connection_templates,
             'msgs': msgs,
             'generate_options': self._generate_options,
-            'var_id2cbs': var_id2cbs,
+            'callbacks': callbacks,
         }
         # Build the template
         t = Template(open(FLOW_GRAPH_TEMPLATE, 'r').read(), namespace)
diff --git a/grc/core/generator/flow_graph.tmpl 
b/grc/core/generator/flow_graph.tmpl
index ecdb893..07c4169 100644
--- a/grc/core/generator/flow_graph.tmpl
+++ b/grc/core/generator/flow_graph.tmpl
@@ -13,7 +13,7 @@
 address@hidden connections the connections
 address@hidden msgs the msg type connections
 address@hidden generate_options the type of flow graph
address@hidden var_id2cbs variable id map to callback strings
address@hidden callbacks variable id map to callback strings
 ########################################################
 #def indent($code)
 #set $code = '\n        '.join(str($code).splitlines())
@@ -301,12 +301,12 @@ gr.io_signaturev($(len($io_sigs)), $(len($io_sigs)), 
[$(', '.join($size_strs))])
     #if $flow_graph.get_option('thread_safe_setters')
         with self._lock:
             self.$id = $id
-        #for $callback in $var_id2cbs[$id]
+        #for $callback in $callbacks[$id]
             $indent($callback)
         #end for
     #else
         self.$id = $id
-        #for $callback in $var_id2cbs[$id]
+        #for $callback in $callbacks[$id]
         $indent($callback)
         #end for
     #end if
diff --git a/grc/core/utils/expr_utils.py b/grc/core/utils/expr_utils.py
index 6691175..240b99e 100644
--- a/grc/core/utils/expr_utils.py
+++ b/grc/core/utils/expr_utils.py
@@ -56,7 +56,7 @@ class graph(object):
         return self._graph[node_key]
 
 
-def expr_split(expr):
+def expr_split(expr, var_chars=VAR_CHARS):
     """
     Split up an expression by non alphanumeric characters, including 
underscore.
     Leave strings in-tact.
@@ -72,7 +72,7 @@ def expr_split(expr):
     tok = ''
     quote = ''
     for char in expr:
-        if quote or char in VAR_CHARS:
+        if quote or char in var_chars:
             if char == quote:
                 quote = ''
             tok += char
@@ -118,7 +118,7 @@ def get_variable_dependencies(expr, vars):
         a subset of vars used in the expression
     """
     expr_toks = expr_split(expr)
-    return set(filter(lambda v: v in expr_toks, vars))
+    return set(var for var in vars if var in expr_toks)
 
 
 def get_graph(exprs):
@@ -189,8 +189,3 @@ def sort_objects(objects, get_id, get_expr):
     sorted_ids = sort_variables(id2expr)
     # Return list of sorted objects
     return [id2obj[id] for id in sorted_ids]
-
-
-if __name__ == '__main__':
-    for i in sort_variables({'x': '1', 'y': 'x+1', 'a': 'x+y', 'b': 'y+1', 
'c': 'a+b+x+y'}):
-        print i



reply via email to

[Prev in Thread] Current Thread [Next in Thread]