From 6169fbb851b4f827309ddb48656c853889a2821e 2015-02-03 19:33:03 From: Sylvain Corlay Date: 2015-02-03 19:33:03 Subject: [PATCH] Allow a comparison operator 'equals' to be set for reverse lookup --- diff --git a/IPython/html/widgets/widget_selection.py b/IPython/html/widgets/widget_selection.py index 5193fb1..228498b 100644 --- a/IPython/html/widgets/widget_selection.py +++ b/IPython/html/widgets/widget_selection.py @@ -55,6 +55,7 @@ class _Selection(DOMWidget): def __init__(self, *args, **kwargs): self.value_lock = Lock() self.options_lock = Lock() + self.equals = kwargs.pop('equals', lambda x, y: x == y) self.on_trait_change(self._options_readonly_changed, ['_options_dict', '_options_labels', '_options_values', '_options']) if 'options' in kwargs: self.options = kwargs.pop('options') @@ -105,13 +106,14 @@ class _Selection(DOMWidget): def _options_readonly_changed(self, name, old, new): if not self.options_lock.locked(): raise TraitError("`.%s` is a read-only trait. Use the `.options` tuple instead." % name) + def _value_changed(self, name, old, new): """Called when value has been changed""" if self.value_lock.acquire(False): try: # Reverse dictionary lookup for the value name for k, v in self._options_dict.items(): - if new == v: + if self.equals(new, v): # set the selected value name self.selected_label = k return