Skip to content
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

ObjectSelector support for selecting lists and dicts #268

Merged
merged 5 commits into from
Sep 20, 2018
Merged

Conversation

jbednar
Copy link
Member

@jbednar jbednar commented Sep 20, 2018

See holoviz/panel#50 for more details.

@jbednar jbednar changed the title Added support for selecting lists in ObjectSelector ObjectSelector support for selecting lists and dicts Sep 20, 2018
@coveralls
Copy link

coveralls commented Sep 20, 2018

Coverage Status

Coverage increased (+0.08%) to 77.094% when pulling 604a9b4 on hashable into f6afcd1 on master.

@jbednar jbednar requested a review from philippjfr September 20, 2018 15:36
Copy link
Member

@philippjfr philippjfr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@jbednar jbednar merged commit 97076f2 into master Sep 20, 2018
@jbednar jbednar deleted the hashable branch September 20, 2018 16:00
if isinstance(x, collections.MutableSequence):
return tuple(x)
elif isinstance(x, collections.MutableMapping):
return tuple([(k,v) for k,v in x.items()])
Copy link
Contributor

@ceball ceball Sep 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what you're doing (I think), but the terminology might be confusing in general.

An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__() method), and can be compared to other objects (it needs an __eq__() method). Hashable objects which compare equal must have the same hash value.

(https://docs.python.org/3/glossary.html#term-hashable)

E.g. could presumably have two dicts x and y which compare equal but for which hash(hashable(x)) is not the same as hash(hashable(y)). You could sort, maybe. Can't quite think through all the consequences for what you're doing or in general either way!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for the name hashable() is simply that when applied, something that Python says is not hashable becomes hashable:

>>> i=0
>>> t=(1,2,3)
>>> l=[1,2,3]
>>> d=dict(one=1,two=2,three=3)
>>> reverse_items = {v:k for k,v in {"i":i, "t":t}.items()}
>>> reverse_items = {v:k for k,v in {"i":i, "t":t, "l":l}.items()}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <dictcomp>
TypeError: unhashable type: 'list'
>>> reverse_items = {v:k for k,v in {"i":i, "t":t, "d":d}.items()}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <dictcomp>
TypeError: unhashable type: 'dict'
>>> from param import hashable
>>> reverse_items = {v:k for k,v in {"i":i, "t":t, "l":hashable(l), "d":hashable(d)}.items()}
>>>

So I don't think there can be any disagreement that the result of hashable() is in fact hashable.

But I think your point is that it may not work well as a hash for the original object in all cases, which is true (and which is presumably why Python doesn't already do this in general!). For the special but very common case of a list of literals, I don't think there is any issue; the ordering should be the same regardless, and if any item in the list is added, removed, or change it won't match, as designed. For dictionaries you're right that any ordering changes will make the comparison fail. I don't know if there are cases where the ordering will change without at least some change to the object (e.g. adding then deleting an item), and I think it's fair for people to assume that if they have changed the object in any way it will no longer match its name (in which case they will get a default str-based representation). So I'm not sure it's worth the extra step of adding sorting, but if you want to do so, then I certainly won't object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants