def read_component(cls, filename, component_name=None): """Reads a single |COMPONENTCLASS| object from a filename. :param filename: The name of the file. :param component_name: If the file contains more than one ComponentClass definition, this parameter must be provided as a ``string`` specifying which component to return, otherwise a NineMLRuntimeException will be raised. :rtype: Returns a |COMPONENTCLASS| object. """ xml_node_filename_map = {} root = cls._load_nested_xml( filename=filename, xml_node_filename_map=xml_node_filename_map) loader = cls.loader() loader.load_componentclasses( xmlroot=root, xml_node_filename_map=xml_node_filename_map) if component_name is None: key_func = lambda c: loader.component_srcs[c] == filename return filter_expect_single(loader.components, key_func) else: key_func = lambda c: c.name == component_name return filter_expect_single(loader.components, key_func)
def regime( self, name=None, ): """Find a regime in the componentclass by name""" assert isinstance(name, basestring) return filter_expect_single(self.componentclass.regimes, lambda r: r.name == name)
def rename_port(cls, componentclass, old_port_name, new_port_name): """ Renames a port in a componentclass """ if not componentclass.is_flat(): raise NineMLRuntimeError('rename_port() on non-flat ' 'componentclass') # Find the old port: port = filter_expect_single(componentclass.analog_ports, lambda ap: ap.name == old_port_name) port._name = new_port_name
def remap_port_to_parameter(cls, componentclass, port_name): """ Renames a port in a componentclass """ if not componentclass.is_flat(): raise NineMLRuntimeError('rename_port_to_parameter() on non-flat ' 'componentclass') # Find the old port: port = filter_expect_single(componentclass.analog_ports, lambda ap: ap.name == port_name) componentclass._analog_ports.remove(port) # Add a new parameter: componentclass._parameters[port_name] = Parameter(port_name)
def read_component(cls, filename, component_name=None): """Reads a single |COMPONENTCLASS| object from a filename. :param filename: The name of the file. :param component_name: If the file contains more than one ComponentClass definition, this parameter must be provided as a ``string`` specifying which component to return, otherwise a NineMLRuntimeException will be raised. :rtype: Returns a |COMPONENTCLASS| object. """ xml_node_filename_map = {} root = cls._load_nested_xml(filename=filename, xml_node_filename_map=xml_node_filename_map) loader = cls.loader() loader.load_componentclasses(xmlroot=root, xml_node_filename_map=xml_node_filename_map) if component_name is None: key_func = lambda c: loader.component_srcs[c] == filename return filter_expect_single(loader.components, key_func) else: key_func = lambda c: c.name == component_name return filter_expect_single(loader.components, key_func)
def close_analog_port(cls, componentclass, port_name, value="0"): """Closes an incoming analog port by assigning its value to 0""" if not componentclass.is_flat(): raise NineMLRuntimeError('close_analog_port() on non-flat ' 'componentclass') # Subsitute the value in: componentclass.accept_visitor(cls._ExpandPortDefinition(port_name, value)) # Remove it from the list of ports: port = filter_expect_single(componentclass.analog_ports, lambda ap: ap.name == port_name) if isinstance(port, AnalogSendPort): componentclass._analog_send_ports.pop(port_name) elif isinstance(port, AnalogReceivePort): componentclass._analog_receive_ports.pop(port_name) elif isinstance(port, AnalogReducePort): componentclass._analog_reduce_ports.pop(port_name) else: raise TypeError("Expected an analog port")
def get_on_event_channel(on_event, component): port = filter_expect_single( component.event_ports, lambda ep:ep.name==on_event.src_port_name) return port.channel_
def test_filter_expect_single(self): # Signature: name(lst, func=None, error_func=None) # Find a single element matching a predicate in a list. # # This is a syntactic-sugar function ``_filter`` and ``expect_single`` # in a single call. # # Returns:: # # expect_single( _filter(lst, func), error_func ) # # # This is useful when we want to find an item in a sequence with a certain # property, and expect there to be only one. # # Examples: # # >>> find_smith = lambda s: s.split()[-1] == 'Smith' # >>> filter_expect_single( ['John Smith','Tim Jones'], func=find_smith ) #doctest: +NORMALIZE_WHITESPACE # 'John Smith' find_smith = lambda s: s.split()[-1] == 'Smith' self.assertEqual( filter_expect_single(['John Smith', 'Tim Jones'], func=find_smith), 'John Smith' ) self.assertEqual( filter_expect_single(['Rob Black', 'John Smith', 'Tim Jones'], func=find_smith), 'John Smith' ) self.assertRaises( NineMLRuntimeError, filter_expect_single, ['Rob Black', 'Tim Jones'], func=find_smith, ) # No Objects: self.assertRaises( NineMLRuntimeError, filter_expect_single, [], func=lambda x: None, ) # Only a single None self.assertRaises( NineMLRuntimeError, filter_expect_single, [None], func=lambda x: x == None, ) # Duplicate objects: self.assertRaises( NineMLRuntimeError, filter_expect_single, [None, None], func=lambda x: x == None, ) # Duplicate objects: self.assertRaises( NineMLRuntimeError, filter_expect_single, [False, False], func=lambda x: x == None, ) self.assertRaises( NineMLRuntimeError, filter_expect_single, [True, True], func=lambda x: x == None, ) self.assertRaises( NineMLRuntimeError, filter_expect_single, [1, 2, 3, 4, 5, 4, 3, 2, 1], func=lambda x: x == 2, ) self.assertRaises( NineMLRuntimeError, filter_expect_single, ['1', '2', '3', '4', '3', '2', '1'], func=lambda x: x == '2', ) self.assertEqual( True, filter_expect_single(['1', '2', '3', '4', '5', True], func=lambda x: x is True) ) # Good Cases self.assertEqual( 2, filter_expect_single([1, 2, 3, 4, 5], func=lambda x: x == 2) ) self.assertEqual( '2', filter_expect_single(['1', '2', '3', '4', '5'], func=lambda x: x == '2') )
def regime(self, name=None,): """Find a regime in the componentclass by name""" assert isinstance(name, basestring) return filter_expect_single(self.componentclass.regimes, lambda r: r.name == name)
def test_filter_expect_single(self): # Signature: name(lst, func=None, error_func=None) # Find a single element matching a predicate in a list. # # This is a syntactic-sugar function ``_filter`` and ``expect_single`` # in a single call. # # Returns:: # # expect_single( _filter(lst, func), error_func ) # # # This is useful when we want to find an item in a sequence with a certain # property, and expect there to be only one. # # Examples: # # >>> find_smith = lambda s: s.split()[-1] == 'Smith' # >>> filter_expect_single( ['John Smith','Tim Jones'], func=find_smith ) #doctest: +NORMALIZE_WHITESPACE # 'John Smith' find_smith = lambda s: s.split()[-1] == 'Smith' self.assertEqual( filter_expect_single(['John Smith', 'Tim Jones'], func=find_smith), 'John Smith') self.assertEqual( filter_expect_single(['Rob Black', 'John Smith', 'Tim Jones'], func=find_smith), 'John Smith') self.assertRaises( NineMLRuntimeError, filter_expect_single, ['Rob Black', 'Tim Jones'], func=find_smith, ) # No Objects: self.assertRaises( NineMLRuntimeError, filter_expect_single, [], func=lambda x: None, ) # Only a single None self.assertRaises( NineMLRuntimeError, filter_expect_single, [None], func=lambda x: x == None, ) # Duplicate objects: self.assertRaises( NineMLRuntimeError, filter_expect_single, [None, None], func=lambda x: x == None, ) # Duplicate objects: self.assertRaises( NineMLRuntimeError, filter_expect_single, [False, False], func=lambda x: x == None, ) self.assertRaises( NineMLRuntimeError, filter_expect_single, [True, True], func=lambda x: x == None, ) self.assertRaises( NineMLRuntimeError, filter_expect_single, [1, 2, 3, 4, 5, 4, 3, 2, 1], func=lambda x: x == 2, ) self.assertRaises( NineMLRuntimeError, filter_expect_single, ['1', '2', '3', '4', '3', '2', '1'], func=lambda x: x == '2', ) self.assertEqual( True, filter_expect_single(['1', '2', '3', '4', '5', True], func=lambda x: x is True)) # Good Cases self.assertEqual( 2, filter_expect_single([1, 2, 3, 4, 5], func=lambda x: x == 2)) self.assertEqual( '2', filter_expect_single(['1', '2', '3', '4', '5'], func=lambda x: x == '2'))