New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make SelectionWidget.values
a dict
#5012
Conversation
""" | ||
|
||
value = Any(help="Selected value") | ||
values = Dict(help="Dictionary of label:value the user can select") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the help string here say name:value
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, fixed.
# preserve list order with an OrderedDict | ||
kwargs['values'] = d = OrderedDict() | ||
for v in values: | ||
d[unicode_type(v)] = v |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's possible to construct an OrderedDict with 2-tuples if we want to make this a bit shorter:
OrderedDict((unicode_type(v), v) for v in values)
Various changes recommended by @takluyver should be done. |
The keys of this dictionary are also available as value_names. | ||
""") | ||
value_name = Unicode(help="The name of the selected value", sync=True) | ||
value_names = List(help="""List of names for each value. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe our List traitlet can validate the type of items in the list, i.e. List(Unicode, ...)
. Is that worth using here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, and done.
rename 'labels' and '_value' to 'value_names' and 'value_name'. To specify a mapping of value names and values, use a dict. If you specify `values=[list]`, then an OrderedDict will be used. Assignment after construction only supports a dict.
value_names is now read-only, so it is clear that there is only one place to edit this information - the values dictionary. |
raises KeyError on failed lookup (in both directions)
Looks good, merging. |
Make `SelectionWidget.values` a dict
Make `SelectionWidget.values` a dict
rename
labels
tovalue_names
and_value
tovalue_name
.To specify a mapping of value names and values, use a dict. If you specify
values=[list]
, then an OrderedDict will be used, whose keys arestr(value)
.Assignment to
values
after construction only supports a dict.closes #4961