Skip to content

Instantly share code, notes, and snippets.

@starlightsys
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save starlightsys/b352677143f516728561 to your computer and use it in GitHub Desktop.

Select an option

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.
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
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