Created
May 23, 2012 18:05
-
-
Save MichaelBlume/2776725 to your computer and use it in GitHub Desktop.
The dumps param in simplejson.dumps is often helpful, but sometimes you want to quickly pass in multiple default actions. This should help.
This file contains hidden or 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 compose_defaults(*defaults): | |
'''I got annoyed that simplejson.dumps doesn't let you pass multiple | |
defaults. So here's this.''' | |
def new_default(obj): | |
for default in defaults: | |
try: | |
return default(obj) | |
except: | |
pass | |
raise TypeError("no default functions succeeded") | |
return new_default |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment