Created
April 5, 2016 23:35
-
-
Save decibel/b3d7ea5883c73cd3daf0abd56cc272d1 to your computer and use it in GitHub Desktop.
Simple python function to return a default value depending on the data type
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 clear(i): | |
if isinstance(i, basestring): | |
return '' | |
elif isinstance(i, int): | |
return 0 | |
elif isinstance(i, float): | |
return 0.0 | |
elif i is None: | |
return None | |
elif isinstance(i, dict): | |
return {k: clear(v) for k,v in i.iteritems()} | |
elif isinstance(i, list): | |
return [clear(item) for item in i] | |
raise TypeError('unknown type ' + str(type(i))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment