[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Commit-gnuradio] r11243 - in gnuradio/branches/developers/jblum/grc/grc
From: |
jblum |
Subject: |
[Commit-gnuradio] r11243 - in gnuradio/branches/developers/jblum/grc/grc: base gui python |
Date: |
Fri, 19 Jun 2009 18:32:29 -0600 (MDT) |
Author: jblum
Date: 2009-06-19 18:32:29 -0600 (Fri, 19 Jun 2009)
New Revision: 11243
Modified:
gnuradio/branches/developers/jblum/grc/grc/base/Block.py
gnuradio/branches/developers/jblum/grc/grc/base/Connection.py
gnuradio/branches/developers/jblum/grc/grc/base/Element.py
gnuradio/branches/developers/jblum/grc/grc/base/FlowGraph.py
gnuradio/branches/developers/jblum/grc/grc/base/Param.py
gnuradio/branches/developers/jblum/grc/grc/base/Port.py
gnuradio/branches/developers/jblum/grc/grc/gui/FlowGraph.py
gnuradio/branches/developers/jblum/grc/grc/gui/Param.py
gnuradio/branches/developers/jblum/grc/grc/gui/ParamsDialog.py
gnuradio/branches/developers/jblum/grc/grc/python/Block.py
gnuradio/branches/developers/jblum/grc/grc/python/Connection.py
gnuradio/branches/developers/jblum/grc/grc/python/FlowGraph.py
gnuradio/branches/developers/jblum/grc/grc/python/Param.py
gnuradio/branches/developers/jblum/grc/grc/python/Port.py
Log:
Speed up for flow graphs, particularly large ones:
Changed validation so actual validate() method is called only once.
The result of validate is stored once, rather than in each call of is_valid().
Also, the result of evaluate() is stored, and can be reached by get_evaluated().
Modified: gnuradio/branches/developers/jblum/grc/grc/base/Block.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/base/Block.py 2009-06-19
22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/base/Block.py 2009-06-20
00:32:29 UTC (rev 11243)
@@ -44,7 +44,7 @@
return str(self._param.to_code())
def __call__(self):
- return self._param.evaluate()
+ return self._param.get_evaluated()
class Block(Element):
@@ -141,11 +141,14 @@
All ports and params must be valid.
All checks must evaluate to true.
"""
+ Element.validate(self)
for c in self.get_params() + self.get_ports() +
self.get_connections():
- try: assert(c.is_valid())
+ try:
+ c.validate()
+ assert c.is_valid()
except AssertionError:
for msg in c.get_error_messages():
- self._add_error_message('>>>
%s:\n\t%s'%(c, msg))
+ self.add_error_message('>>>
%s:\n\t%s'%(c, msg))
def __str__(self): return 'Block - %s - %s(%s)'%(self.get_id(),
self.get_name(), self.get_key())
@@ -256,4 +259,3 @@
#the key must exist in this block's params
if key in self.get_param_keys():
self.get_param(key).set_value(value)
- self.validate()
Modified: gnuradio/branches/developers/jblum/grc/grc/base/Connection.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/base/Connection.py
2009-06-19 22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/base/Connection.py
2009-06-20 00:32:29 UTC (rev 11243)
@@ -59,10 +59,11 @@
Validate the connections.
The ports must match in type.
"""
+ Element.validate(self)
source_type = self.get_source().get_type()
sink_type = self.get_sink().get_type()
try: assert source_type == sink_type
- except AssertionError: self._add_error_message('Source type
"%s" does not match sink type "%s".'%(source_type, sink_type))
+ except AssertionError: self.add_error_message('Source type "%s"
does not match sink type "%s".'%(source_type, sink_type))
def get_enabled(self):
"""
Modified: gnuradio/branches/developers/jblum/grc/grc/base/Element.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/base/Element.py 2009-06-19
22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/base/Element.py 2009-06-20
00:32:29 UTC (rev 11243)
@@ -21,7 +21,6 @@
def __init__(self, parent=None):
self._parent = parent
- self._error_messages = []
self.flag()
def test(self):
@@ -31,32 +30,18 @@
"""
pass
- def validate(self):
- """
- Validate the data in this element.
- Set the error message non blank for errors.
- Overload this method in sub-classes.
- """
- pass
+ ##################################################
+ # Element Validation API
+ ##################################################
+ def validate(self): self._error_messages = list()
+ def is_valid(self): return not self.get_error_messages() or not
self.get_enabled()
+ def add_error_message(self, msg): self._error_messages.append(msg)
+ def get_error_messages(self): return self._error_messages
- def is_valid(self):
- self._error_messages = []#reset err msgs
- if self.get_enabled():
- try: self.validate()
- except: pass
- return not self.get_error_messages()
-
def get_enabled(self): return True
- def _add_error_message(self, msg):
- self._error_messages.append(msg)
+ def get_parent(self): return self._parent
- def get_error_messages(self):
- return self._error_messages
-
- def get_parent(self):
- return self._parent
-
def _exit_with_error(self, error):
parent = self
#build hier list of elements
Modified: gnuradio/branches/developers/jblum/grc/grc/base/FlowGraph.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/base/FlowGraph.py
2009-06-19 22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/base/FlowGraph.py
2009-06-20 00:32:29 UTC (rev 11243)
@@ -58,7 +58,7 @@
@param key the param key for the options block
@return the value held by that param
"""
- return self._options_block.get_param(key).evaluate()
+ return self._options_block.get_param(key).get_evaluated()
def is_flow_graph(self): return True
@@ -152,9 +152,12 @@
Validate the flow graph.
All connections and blocks must be valid.
"""
+ Element.validate(self)
for c in self.get_elements():
- try: assert c.is_valid()
- except AssertionError: self._add_error_message('Element
"%s" is not valid.'%c)
+ try:
+ c.validate()
+ assert c.is_valid()
+ except AssertionError: self.add_error_message('Element
"%s" is not valid.'%c)
##############################################
## Import/Export Methods
@@ -195,6 +198,7 @@
#only load the block when the block key was valid
if block: block.import_data(block_n)
else: Messages.send_error_load('Block key "%s" not
found in %s'%(key, self.get_parent()))
+ self.validate() #validate all blocks before connections are
made (in case of nports)
#build the connections
for connection_n in connections_n:
#try to make the connection
@@ -221,4 +225,3 @@
#build the connection
self.connect(source, sink)
except AssertionError:
Messages.send_error_load('Connection between %s(%s) and %s(%s) could not be
made.'%(source_block_id, source_key, sink_block_id, sink_key))
- self.validate()
Modified: gnuradio/branches/developers/jblum/grc/grc/base/Param.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/base/Param.py 2009-06-19
22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/base/Param.py 2009-06-20
00:32:29 UTC (rev 11243)
@@ -177,21 +177,11 @@
Validate the param.
The value must be evaluated and type must a possible type.
"""
- try:
- assert(self.get_type() in self.TYPES)
- try: self.evaluate()
- except:
- #if the evaluate failed but added no error
messages, add the generic one below
- if not self.get_error_messages():
- self._add_error_message('Value "%s"
cannot be evaluated.'%self.get_value())
- except AssertionError: self._add_error_message('Type "%s" is
not a possible type.'%self.get_type())
+ Element.validate(self)
+ try: assert self.get_type() in self.TYPES
+ except AssertionError: self.add_error_message('Type "%s" is not
a possible type.'%self.get_type())
- def evaluate(self):
- """
- Evaluate the value of this param.
- @throw NotImplementedError
- """
- raise NotImplementedError
+ def get_evaluated(self): raise NotImplementedError
def to_code(self):
"""
Modified: gnuradio/branches/developers/jblum/grc/grc/base/Port.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/base/Port.py 2009-06-19
22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/base/Port.py 2009-06-20
00:32:29 UTC (rev 11243)
@@ -46,8 +46,9 @@
Validate the port.
The port must be non-empty and type must a possible type.
"""
- try: assert(self.get_type() in self.TYPES)
- except AssertionError: self._add_error_message('Type "%s" is
not a possible type.'%self.get_type())
+ Element.validate(self)
+ try: assert self.get_type() in self.TYPES
+ except AssertionError: self.add_error_message('Type "%s" is not
a possible type.'%self.get_type())
def __str__(self):
if self.is_source():
Modified: gnuradio/branches/developers/jblum/grc/grc/gui/FlowGraph.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/gui/FlowGraph.py 2009-06-19
22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/gui/FlowGraph.py 2009-06-20
00:32:29 UTC (rev 11243)
@@ -289,6 +289,7 @@
"""
Call update on all elements.
"""
+ self.validate()
for element in self.get_elements(): element.update()
##########################################################################
Modified: gnuradio/branches/developers/jblum/grc/grc/gui/Param.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/gui/Param.py 2009-06-19
22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/gui/Param.py 2009-06-20
00:32:29 UTC (rev 11243)
@@ -37,7 +37,7 @@
Key: $param.get_key()
Type: $param.get_type()
#if $param.is_valid()
-Value: $param.evaluate()
+Value: $param.get_evaluated()
#elif len($param.get_error_messages()) == 1
Error: $(param.get_error_messages()[0])
#else
@@ -75,6 +75,7 @@
Finish by calling the exteral callback.
"""
self.set_value(self._input.get_text())
+ self.validate()
#is param is involved in a callback? #FIXME: messy
has_cb = \
hasattr(self.get_parent(), 'get_callbacks') and \
Modified: gnuradio/branches/developers/jblum/grc/grc/gui/ParamsDialog.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/gui/ParamsDialog.py
2009-06-19 22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/gui/ParamsDialog.py
2009-06-20 00:32:29 UTC (rev 11243)
@@ -91,6 +91,7 @@
Update the documentation block.
Hide the box if there are no docs.
"""
+ self.block.validate()
#update the errors box
if self.block.is_valid(): self._error_box.hide()
else: self._error_box.show()
Modified: gnuradio/branches/developers/jblum/grc/grc/python/Block.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/python/Block.py 2009-06-19
22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/python/Block.py 2009-06-20
00:32:29 UTC (rev 11243)
@@ -62,8 +62,8 @@
try:
check_eval =
self.get_parent().evaluate(check_res)
try: assert check_eval
- except AssertionError:
self._add_error_message('Check "%s" failed.'%check)
- except: self._add_error_message('Check "%s" did not
evaluate.'%check)
+ except AssertionError:
self.add_error_message('Check "%s" failed.'%check)
+ except: self.add_error_message('Check "%s" did not
evaluate.'%check)
#adjust nports
for ports, Port in (
(self._sources, self.get_parent().get_parent().Source),
@@ -115,7 +115,7 @@
if param.is_enum() or param.get_key() not in
nports_str: continue
#try to increment the port controller by direction
try:
- value = param.evaluate()
+ value = param.get_evaluated()
value = value + direction
assert 0 < value
param.set_value(value)
Modified: gnuradio/branches/developers/jblum/grc/grc/python/Connection.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/python/Connection.py
2009-06-19 22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/python/Connection.py
2009-06-20 00:32:29 UTC (rev 11243)
@@ -30,5 +30,5 @@
#check vector length
source_vlen = self.get_source().get_vlen()
sink_vlen = self.get_sink().get_vlen()
- try: assert(source_vlen == sink_vlen)
- except AssertionError: self._add_error_message('Source vector
length "%s" does not match sink vector length "%s".'%(source_vlen, sink_vlen))
+ try: assert source_vlen == sink_vlen
+ except AssertionError: self.add_error_message('Source vector
length "%s" does not match sink vector length "%s".'%(source_vlen, sink_vlen))
Modified: gnuradio/branches/developers/jblum/grc/grc/python/FlowGraph.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/python/FlowGraph.py
2009-06-19 22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/python/FlowGraph.py
2009-06-20 00:32:29 UTC (rev 11243)
@@ -71,9 +71,9 @@
pad = pads[0] #take only the first, user should not have more
than 1
#load io signature
return {
- 'nports': str(pad.get_param('nports').evaluate()),
- 'type': str(pad.get_param('type').evaluate()),
- 'vlen': str(pad.get_param('vlen').evaluate()),
+ 'nports': str(pad.get_param('nports').get_evaluated()),
+ 'type': str(pad.get_param('type').get_evaluated()),
+ 'vlen': str(pad.get_param('vlen').get_evaluated()),
'size': pad.get_param('type').get_opt('size'),
}
Modified: gnuradio/branches/developers/jblum/grc/grc/python/Param.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/python/Param.py 2009-06-19
22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/python/Param.py 2009-06-20
00:32:29 UTC (rev 11243)
@@ -46,7 +46,7 @@
Replace the text in the entry with the new filename from the
file dialog.
"""
#get the paths
- file_path = self.param.is_valid() and self.param.evaluate() or
''
+ file_path = self.param.is_valid() and
self.param.get_evaluated() or ''
(dirname, basename) = os.path.isfile(file_path) and
os.path.split(file_path) or (file_path, '')
if not os.path.exists(dirname): dirname = os.getcwd() #fix bad
paths
#build the dialog
@@ -120,7 +120,7 @@
##################################################
truncate = 0 #default center truncate
max_len = max(27 - len(self.get_name()), 3)
- e = self.evaluate()
+ e = self.get_evaluated()
t = self.get_type()
if isinstance(e, bool): return str(e)
elif isinstance(e, COMPLEX_TYPES): dt_str = num_to_str(e)
@@ -195,18 +195,32 @@
lambda p: p._vlen, self.get_parent().get_ports())
):
try:
- assert int(self.evaluate()) == 1
+ assert int(self.get_evaluated()) == 1
return 'part'
except: pass
#hide empty grid positions
if self.get_key() == 'grid_pos' and not self.get_value():
return 'part'
return hide
+ def validate(self):
+ """
+ Validate the param.
+ A test evaluation is performed
+ """
+ _Param.validate(self) #checks type
+ self._evaluated = None
+ try: self._evaluated = self.evaluate()
+ except: #if the evaluate failed but added no error messages,
add the generic one below
+ if not self.get_error_messages():
self.add_error_message('Value "%s" cannot be evaluated.'%self.get_value())
+
+ def get_evaluated(self): return self._evaluated
+
def evaluate(self):
"""
Evaluate the value.
@return evaluated type
"""
+ self._init = True
self._lisitify_flag = False
self._stringify_flag = False
self._hostage_cells = list()
@@ -231,26 +245,26 @@
#raise exception if python cannot evaluate this value
try: e = self.get_parent().get_parent().evaluate(v)
except Exception, e:
- self._add_error_message('Value "%s" cannot be
evaluated: %s'%(v, e))
+ self.add_error_message('Value "%s" cannot be
evaluated: %s'%(v, e))
raise Exception
#raise an exception if the data is invalid
if t == 'raw': return e
elif t == 'complex':
try: assert(isinstance(e, COMPLEX_TYPES))
except AssertionError:
- self._add_error_message('Expression
"%s" is invalid for type complex.'%str(e))
+ self.add_error_message('Expression "%s"
is invalid for type complex.'%str(e))
raise Exception
return e
elif t == 'real':
try: assert(isinstance(e, REAL_TYPES))
except AssertionError:
- self._add_error_message('Expression
"%s" is invalid for type real.'%str(e))
+ self.add_error_message('Expression "%s"
is invalid for type real.'%str(e))
raise Exception
return e
elif t == 'int':
try: assert(isinstance(e, INT_TYPES))
except AssertionError:
- self._add_error_message('Expression
"%s" is invalid for type integer.'%str(e))
+ self.add_error_message('Expression "%s"
is invalid for type integer.'%str(e))
raise Exception
return e
#########################
@@ -264,7 +278,7 @@
for ei in e:
assert(isinstance(ei,
COMPLEX_TYPES))
except AssertionError:
- self._add_error_message('Expression
"%s" is invalid for type complex vector.'%str(e))
+ self.add_error_message('Expression "%s"
is invalid for type complex vector.'%str(e))
raise Exception
return e
elif t == 'real_vector':
@@ -275,7 +289,7 @@
for ei in e:
assert(isinstance(ei,
REAL_TYPES))
except AssertionError:
- self._add_error_message('Expression
"%s" is invalid for type real vector.'%str(e))
+ self.add_error_message('Expression "%s"
is invalid for type real vector.'%str(e))
raise Exception
return e
elif t == 'int_vector':
@@ -286,14 +300,14 @@
for ei in e:
assert(isinstance(ei,
INT_TYPES))
except AssertionError:
- self._add_error_message('Expression
"%s" is invalid for type integer vector.'%str(e))
+ self.add_error_message('Expression "%s"
is invalid for type integer vector.'%str(e))
raise Exception
return e
elif t == 'hex': return hex(e)
elif t == 'bool':
try: assert e in (True, False)
except AssertionError:
- self._add_error_message('Expression
"%s" is invalid for type bool.'%str(e))
+ self.add_error_message('Expression "%s"
is invalid for type bool.'%str(e))
raise Exception
return bool(e)
else: raise TypeError, 'Type "%s" not handled'%t
@@ -314,17 +328,17 @@
assert(v[0].isalpha())
for c in v: assert(c.isalnum() or c in ('_',))
except AssertionError:
- self._add_error_message('ID "%s" must be
alpha-numeric or underscored, and begin with a letter.'%v)
+ self.add_error_message('ID "%s" must be
alpha-numeric or underscored, and begin with a letter.'%v)
raise Exception
params = self.get_all_params('id')
keys = [param.get_value() for param in params]
try: assert keys.count(v) <= 1 #id should only appear
once, or zero times if block is disabled
except:
- self._add_error_message('ID "%s" is not
unique.'%v)
+ self.add_error_message('ID "%s" is not
unique.'%v)
raise Exception
try: assert v not in ID_BLACKLIST
except:
- self._add_error_message('ID "%s" is
blacklisted.'%v)
+ self.add_error_message('ID "%s" is
blacklisted.'%v)
raise Exception
return v
#########################
@@ -337,18 +351,18 @@
assert(isinstance(e, (list, tuple)) and len(e)
== 4)
for ei in e: assert(isinstance(ei, int))
except AssertionError:
- self._add_error_message('A grid position must
be a list of 4 integers.')
+ self.add_error_message('A grid position must be
a list of 4 integers.')
raise Exception
row, col, row_span, col_span = e
#check row, col
try: assert(row >= 0 and col >= 0)
except AssertionError:
- self._add_error_message('Row and column must be
non-negative.')
+ self.add_error_message('Row and column must be
non-negative.')
raise Exception
#check row span, col span
try: assert(row_span > 0 and col_span > 0)
except AssertionError:
- self._add_error_message('Row and column span
must be greater than zero.')
+ self.add_error_message('Row and column span
must be greater than zero.')
raise Exception
#calculate hostage cells
for r in range(row_span):
@@ -359,7 +373,7 @@
for param in params:
for cell in param._hostage_cells:
if cell in self._hostage_cells:
-
self._add_error_message('Another graphical element is using cell
"%s".'%str(cell))
+ self.add_error_message('Another
graphical element is using cell "%s".'%str(cell))
raise Exception
return e
#########################
@@ -369,10 +383,10 @@
n = dict() #new namespace
try: exec v in n
except ImportError:
- self._add_error_message('Import "%s" failed.'%v)
+ self.add_error_message('Import "%s" failed.'%v)
raise Exception
except Exception:
- self._add_error_message('Bad import syntax:
"%s".'%v)
+ self.add_error_message('Bad import syntax:
"%s".'%v)
raise Exception
return filter(lambda k: str(k) != '__builtins__',
n.keys())
#########################
@@ -385,9 +399,7 @@
"""
#run init tasks in evaluate
#such as setting flags
- if not self._init:
- self.evaluate()
- self._init = True
+ if not self._init: self.evaluate()
v = self.get_value()
t = self.get_type()
if t in ('string', 'file_open', 'file_save'): #string types
Modified: gnuradio/branches/developers/jblum/grc/grc/python/Port.py
===================================================================
--- gnuradio/branches/developers/jblum/grc/grc/python/Port.py 2009-06-19
22:32:31 UTC (rev 11242)
+++ gnuradio/branches/developers/jblum/grc/grc/python/Port.py 2009-06-20
00:32:29 UTC (rev 11243)
@@ -47,10 +47,10 @@
def validate(self):
_Port.validate(self)
- try: assert(self.get_enabled_connections() or
self.get_optional())
- except AssertionError: self._add_error_message('Port is not
connected.')
- try: assert(self.is_source() or
len(self.get_enabled_connections()) <= 1)
- except AssertionError: self._add_error_message('Port has too
many connections.')
+ try: assert self.get_enabled_connections() or
self.get_optional()
+ except AssertionError: self.add_error_message('Port is not
connected.')
+ try: assert self.is_source() or
len(self.get_enabled_connections()) <= 1
+ except AssertionError: self.add_error_message('Port has too
many connections.')
def get_vlen(self):
"""
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Commit-gnuradio] r11243 - in gnuradio/branches/developers/jblum/grc/grc: base gui python,
jblum <=