Skip to content

Instantly share code, notes, and snippets.

@chartjes
Created January 18, 2018 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chartjes/3aeffbd593be3e40daa1d8fcc1937c63 to your computer and use it in GitHub Desktop.
Save chartjes/3aeffbd593be3e40daa1d8fcc1937c63 to your computer and use it in GitHub Desktop.
'''
bug_data is a dict like {'product': 'Cloud Services', 'version': 'Unspecified'}
Is there a better way to do look for specific keys in that dict so I can say
"You are missing one of the default fields
'''
if 'product' not in bug_data:
return "Missing 'product' field"
elif 'component' not in bug_data:
return "Missing 'component' field"
elif 'summary' not in bug_data:
return "Missing 'summary' field"
elif 'version' not in bug_data:
return "Missing 'version' field"
@ikwattro
Copy link

ikwattro commented Jan 18, 2018

@chartjes

dict = {'product': 'Cloud Services', 'version': 'Unspecified'}

def check_missing_keys():
    for x in ['product','component','version']:
        if x not in dict:
            return 'Missing "' + x + '" field'
    return 'all good'

print(check_missing_keys())

ikwattro@mbp666 ~/dev> python3 hello.py
Missing "component" field

or

print('Missing ' + ', '.join([x for x in ['product','component','version'] if not x in dict]) + ' field(s)')

@rlittlefield
Copy link

required = {'product', 'component', 'summary', 'version'}
bug_data = {'product': 'Cloud Services', 'version': 'Unspecified'}
diff = required.difference(bug_data)
if diff:
    print('Missing ' + ', '.join(diff) + ' field(s)')

@HacKanCuBa
Copy link

I like @rlittlefield's answer but how about: print('Missing {} field(s)'.format(', '.join(diff))) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment