Ignore:
Timestamp:
02/24/12 12:08:05 (2 years ago)
Author:
Ales Erjavec <ales.erjavec@…>
Branch:
default
rebase_source:
16bd8d3962f986a399f3c4d99438e5961f84add3
Message:

Fixed feature descriptor lookup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Orange/OrangeWidgets/Data/OWEditDomain.py

    r10046 r10358  
    2222 
    2323def get_qualified(module, name): 
    24     """ Return a qualified module member ``name`` inside the named  
     24    """Return a qualified module member ``name`` inside the named  
    2525    ``module``. 
    2626     
    27     The module (or package) firts gets imported and the name 
     27    The module (or package) first gets imported and the name 
    2828    is retrieved from the module's global namespace. 
    2929      
    3030    """ 
    31     module = __import__(module) 
     31    # see __import__.__doc__ for why 'fromlist' is used 
     32    module = __import__(module, fromlist=[name]) 
    3233    return getattr(module, name) 
    3334 
    3435def variable_description(var): 
    35     """ Return a variable descriptor. 
     36    """Return a variable descriptor. 
    3637     
    3738    A descriptor is a hashable tuple which should uniquely define  
    3839    the variable i.e. (module, type_name, variable_name,  
    39     any_kwargs, attributes-labels). 
     40    any_kwargs, sorted-attributes-items). 
    4041     
    4142    """ 
     
    5556 
    5657def variable_from_description(description): 
    57     """ Construct a variable from its description 
     58    """Construct a variable from its description 
    5859    (:ref:`variable_description`). 
    5960     
    6061    """ 
    6162    module, type_name, name, kwargs, attrs = description 
    62     type = get_qualified(module, type_name) 
     63    try: 
     64        type = get_qualified(module, type_name) 
     65    except (ImportError, AttributeError), ex: 
     66        raise ValueError("""Invalid descriptor type '{}.{}\ 
     67        """.format(module, type_name)) 
     68         
    6369    var = type(name, **dict(list(kwargs))) 
    6470    var.attributes.update(attrs) 
     
    7581     
    7682class DictItemsModel(QStandardItemModel): 
    77     """ A Qt Item Model class displaying the contents of a python 
     83    """A Qt Item Model class displaying the contents of a python 
    7884    dictionary. 
    7985     
     
    106112 
    107113class VariableEditor(QWidget): 
    108     """ An editor widget for a variable. 
     114    """An editor widget for a variable. 
    109115     
    110116    Can edit the variable name, and its attributes dictionary. 
     
    186192         
    187193    def set_data(self, var): 
    188         """ Set the variable to edit. 
     194        """Set the variable to edit. 
    189195        """ 
    190196        self.clear() 
     
    200206             
    201207    def get_data(self): 
    202         """ Retrieve the modified variable. 
     208        """Retrieve the modified variable. 
    203209        """ 
    204210        name = str(self.name_edit.text()) 
     
    216222     
    217223    def is_same(self): 
    218         """ Is the current model state the same as the input.  
     224        """Is the current model state the same as the input.  
    219225        """ 
    220226        name = str(self.name_edit.text()) 
     
    224230             
    225231    def clear(self): 
    226         """ Clear the editor state. 
     232        """Clear the editor state. 
    227233        """ 
    228234        self.var = None 
     
    235241             
    236242    def commit(self): 
    237         """ Emit a ``variable_changed()`` signal. 
     243        """Emit a ``variable_changed()`` signal. 
    238244        """ 
    239245        self.emit(SIGNAL("variable_changed()")) 
     
    268274         
    269275class DiscreteVariableEditor(VariableEditor): 
    270     """ An editor widget for editing a discrete variable. 
     276    """An editor widget for editing a discrete variable. 
    271277     
    272278    Extends the :class:`VariableEditor` to enable editing of 
     
    298304 
    299305    def set_data(self, var): 
    300         """ Set the variable to edit 
     306        """Set the variable to edit 
    301307        """ 
    302308        VariableEditor.set_data(self, var) 
     
    307313                 
    308314    def get_data(self): 
    309         """ Retrieve the modified variable 
     315        """Retrieve the modified variable 
    310316        """ 
    311317        name = str(self.name_edit.text()) 
     
    323329             
    324330    def is_same(self): 
    325         """ Is the current model state the same as the input.  
     331        """Is the current model state the same as the input.  
    326332        """ 
    327333        values = map(str, self.values_model) 
     
    329335     
    330336    def clear(self): 
    331         """ Clear the model state. 
     337        """Clear the model state. 
    332338        """ 
    333339        VariableEditor.clear(self) 
     
    423429         
    424430    def clear(self): 
    425         """ Clear the widget state. 
     431        """Clear the widget state. 
    426432        """ 
    427433        self.data = None 
     
    431437         
    432438    def clear_editor(self): 
    433         """ Clear the current editor widget 
     439        """Clear the current editor widget 
    434440        """ 
    435441        current = self.editor_stack.currentWidget() 
     
    459465                changed = self.domain_change_hints.get(desc, None) 
    460466                if changed is not None: 
    461                     new = variable_from_description(changed) 
    462                      
    463                     # Make sure orange's domain transformations will work. 
    464                     new.source_variable = var 
    465                     new.get_value_from = Orange.core.ClassifierFromVar(whichVar=var) 
    466                     var = new 
     467                    try: 
     468                        new = variable_from_description(changed) 
     469                    except ValueError, ex: 
     470#                        print ex 
     471                        new = None 
     472                         
     473                    if new is not None: 
     474                        # Make sure orange's domain transformations will work. 
     475                        new.source_variable = var 
     476                        new.get_value_from = Orange.core.ClassifierFromVar(whichVar=var) 
     477                        var = new 
     478                         
    467479                edited_vars.append(var) 
    468480             
     
    487499             
    488500    def on_selection_changed(self, *args): 
    489         """ When selection in 'Domain Features' view changes.  
     501        """When selection in 'Domain Features' view changes.  
    490502        """ 
    491503        i = self.selected_var_index() 
     
    495507         
    496508    def selected_var_index(self): 
    497         """ Return the selected row in 'Domain Features' view or None  
     509        """Return the selected row in 'Domain Features' view or None  
    498510        if no row is selected. 
    499511         
     
    506518         
    507519    def select_variable(self, index): 
    508         """ Select the variable with ``index`` in the 'Domain Features' 
     520        """Select the variable with ``index`` in the 'Domain Features' 
    509521        view. 
    510522         
     
    515527         
    516528    def open_editor(self, index): 
    517         """ Open the editor for variable at ``index`` and move it 
     529        """Open the editor for variable at ``index`` and move it 
    518530        to the top if the stack. 
    519531         
     
    533545     
    534546    def editor_for_variable(self, var): 
    535         """ Return the editor for ``var``'s variable type. 
     547        """Return the editor for ``var``'s variable type. 
    536548         
    537549        The editors are cached and reused by type. 
     
    554566     
    555567    def on_variable_changed(self): 
    556         """ When the user edited the current variable in editor. 
     568        """When the user edited the current variable in editor. 
    557569        """ 
    558570        var = self.domain_model[self.edited_variable_index] 
     
    575587          
    576588    def reset_all(self): 
    577         """ Reset all variables to the input state. 
     589        """Reset all variables to the input state. 
    578590        """ 
    579591        self.domain_change_hints = {} 
     
    587599             
    588600    def reset_selected(self): 
    589         """ Reset the currently selected variable to its original 
     601        """Reset the currently selected variable to its original 
    590602        state. 
    591603           
     
    612624         
    613625    def commit(self): 
    614         """ Commit the changed data to output.  
     626        """Commit the changed data to output.  
    615627        """ 
    616628        new_data = None 
Note: See TracChangeset for help on using the changeset viewer.