Skip to content

Instantly share code, notes, and snippets.

@Ch00k
Created January 25, 2019 10:53
Show Gist options
  • Save Ch00k/87b7550e710db95bd67b6a8b27b9a40f to your computer and use it in GitHub Desktop.
Save Ch00k/87b7550e710db95bd67b6a8b27b9a40f to your computer and use it in GitHub Desktop.
dict lookup log if None
import logging
d = {'foo': 42}
def log_if_none(message):
logging.warning(message)
foo = d.get('foo', log_if_none("No key foo"))
bar = d.get('bar', log_if_none("No key bar"))
baz = d.get('baz', log_if_none("No key baz"))
print(foo, bar, baz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment