Last active
August 29, 2015 14:06
-
-
Save starlightsys/b352677143f516728561 to your computer and use it in GitHub Desktop.
Returns the result of fn(kwargs), or if there are exceptions, returns alternative instead.
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 safeResult(fn, expectedError = Exception, alternative = None, **kwargs): | |
| """Returns the result of fn(kwargs), or if there are exceptions, returns alternative instead.""" | |
| result = alternative | |
| try: | |
| result = fn( kwargs ) | |
| except expectedError: | |
| pass | |
| return result |
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
| class Example(object): | |
| tree = None | |
| # ... | |
| def getSourceLang(self): | |
| fn = lambda x: x["tree"].xpath( "//TS/@sourcelanguage" )[0] | |
| return safeResult( fn, alternative="und", tree=self.tree ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment