Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Created October 13, 2011 20:06
Show Gist options
  • Save pyrtsa/1285350 to your computer and use it in GitHub Desktop.
Save pyrtsa/1285350 to your computer and use it in GitHub Desktop.
mapping(x)
def mapping(x):
"""Check whether x is a mapping type (e.g. dict)"""
import sys
if 'numpy' in sys.modules:
try:
from numpy import ndarray
if isinstance(x, ndarray): return False # nasty special case
except ImportError:
pass
methods = ['__contains__', '__getitem__', 'items', 'iteritems']
return all(hasattr(x, m) and callable(getattr(x, m)) for m in methods)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment