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

Get 'None' or a default value if key does not exist #56

Closed
yannlambret opened this issue Dec 4, 2016 · 6 comments
Closed

Get 'None' or a default value if key does not exist #56

yannlambret opened this issue Dec 4, 2016 · 6 comments

Comments

@yannlambret
Copy link
Contributor

Hello,

Thanks for working on this. I've just started using etcd v3 so maybe I'm missing something, but I find that the default behavior of the client's 'get' method when requesting a non-existing key is inappropriate. I think we should take advantage of a behavior similar to the Python dictionary, i.e. be able to get at least 'None' instead of catching an exception.

This is especially true when we want to test if a key already exist. Moreover, it would be more consistent with the 'etcdctl' CLI tool that does not display anything when a non-existing key is requested, an return a 0 exit code.

@kragniz
Copy link
Owner

kragniz commented Dec 5, 2016

You'd prefer something like this?

>>> import etcd3
>>> etcd = etcd3.client()
>>> etcd.get('not-a-key')
(None, <etcd3.client.KVMetadata object at 0x7f5e32621790>)
>>> etcd.get('not-a-key', default=b'toot')
(b'toot', <etcd3.client.KVMetadata object at 0x7f5e32621650>)

@yannlambret
Copy link
Contributor Author

I would indeed, I think it would allow to write more concise code in most situations. What do you think ?

@zimnx
Copy link
Contributor

zimnx commented Dec 5, 2016

Official etcd2 client throws exception when key is not found, I would prefer current behavior. Current implementations can simply switch type of Exception handled without modifying logic around client.

@yannlambret
Copy link
Contributor Author

There is no official Python client for etcd, just third party libraries referenced in the documentation - as this one is. If you take a look at the behavior of the go etcd client for v3 API (which is the only one maintained by CoreOS as part of the project), it returns a data structure as part of the response (resp.Kvs) which is empty if a key is not found, and the error returned by the .Get method is 'nil' in this case.

@minus7
Copy link

minus7 commented Dec 6, 2016

I'd mimic the behaviour of dict: raise KeyError (or a subclass thereof containing the KVMetadata object) if a non-existent key is accessed without providing a default. To check if a key exists in/__contains__ should be used. (A successful get returning a tuple isn't particularly nice either, maybe a proxy object would work there better?)

Disclaimer: I've never used etcd nor this library.

@kragniz
Copy link
Owner

kragniz commented Jan 4, 2017

Addressed by #73

@kragniz kragniz closed this as completed Jan 4, 2017
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

No branches or pull requests

4 participants