Created
October 13, 2011 20:06
-
-
Save pyrtsa/1285350 to your computer and use it in GitHub Desktop.
mapping(x)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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