TryingSnake API¶
-
tryingsnake.
Try
(f, *args, **kwargs)¶ Calls f with provided arguments and wraps the result using either Success or Failure.
Parameters: - f – Callable which should be evaluated
- args – args which should be passed to f
- kwargs – kwargs which should be passed to f
Returns: Either success or Failure
>>> from operator import add, truediv >>> Try(truediv, 1, 0) Failure(ZeroDivisionError(...)) >>> Try(add, 1, 2) Success(3)
-
class
tryingsnake.
Try_
(_)¶ -
failed
()¶ If this is a failure complete wrapped exception otherwise throw TypeError
Returns: None >>> Success(1).failed() Traceback (most recent call last): ... TypeError: Cannot fail Success >>> Failure(Exception("e")).failed() Traceback (most recent call last): ... Exception: e
-
filter
(f, exception_cls=<type 'exceptions.Exception'>, msg=None)¶ Convert this to Failure if f(self.get()) evaluates to False
Parameters: - f – function to be applied
- exception_cls – optional exception class to return
- msg – optional message
Returns: self if f evaluates to True otherwise Failure
>>> Success(1).filter(lambda x: x > 0) Success(1) >>> Success(1).filter(lambda x: x < 0, msg="Greater than zero") Failure(Exception('Greater than zero',)) >>> Failure(Exception("e")).filter(lambda x: x) Failure(Exception('e',))
-
flatMap
(f)¶ Apply function returning Try_ to the value.
Parameters: f – function to be applied. Returns: self if this is a Failure otherwise f applied to self.get >>> from operator import add >>> Success(1).flatMap(lambda x: Try(add, x, 1)) Success(2) >>> Failure(Exception("e")).flatMap(lambda x: Try(add, x, 1)) Failure(Exception('e',)) >>> Success(1).flatMap(lambda x: Try(add, x, "0")) Failure(TypeError(...))
-
get
()¶ If this is Success get wrapped value otherwise throw stored exception
Returns: stored value >>> Success(1).get() 1 >>> Failure(Exception("e")).get() Traceback (most recent call last): ... Exception: e
-
getOrElse
(default)¶ If this is a Success get stored value otherwise return default
Parameters: default – value to return if this is a Failure Returns: >>> Success(1).getOrElse(0) 1 >>> Failure(Exception("e")).getOrElse(0) 0
-
isFailure
¶ Check if this is a Failure.
>>> Success(1).isFailure False >>> Failure(Exception()).isFailure True
-
isSuccess
¶ Check if this is a Success.
>>> Success(1).isSuccess True >>> Failure(Exception()).isSuccess False
-
map
(f)¶ Apply function to the value.
Parameters: f – function to be applied Returns: self if this is a Failure otherwise Try(f, self.get) >>> inc = lambda x: x + 1 >>> def f(e): raise Exception("e") >>> Success(1).map(inc) Success(2) >>> Failure(Exception("e")).map(inc) Failure(Exception('e',)) >>> Success("1").map(f) Failure(Exception('e',))
-
orElse
(default)¶ If this is a Success return self otherwise default
Parameters: default – Try_ Returns: >>> Success(1).orElse(Success(0)) Success(1) >>> Failure(Exception("e")).orElse(Success(0)) Success(0)
-
recover
(f)¶ If this is a Failure apply f to value otherwise
Parameters: f – function to be applied Returns: Either Success of Failure >>> def f(e): raise Exception("e") >>> Success(1).recover(lambda e: 0) Success(1) >>> Failure(Exception("e")).recover(lambda e: 0) Success(0) >>> Failure(Exception("e")).recover(f) Failure(Exception('e',))
-
recoverWith
(f)¶ If this is a Failure apply f to self otherwise return this
Parameters: f – function to be applied Returns: Either Success of Failure >>> Success(1).recoverWith(lambda t: Try(lambda: 0)) Success(1) >>> Failure(Exception("e")).recoverWith(lambda t: Try(lambda: 0)) Success(0)
-
static
set_unhandled
(es=None)¶ Set a list of the unhandled exceptions.
Parameters: es – an iterable of exceptions or None >>> from operator import getitem >>> Try(getitem, [], 0) Failure(IndexError('list index out of range',)) >>> Try_.set_unhandled([IndexError]) >>> Try(getitem, [], 0) Traceback (most recent call last): ... IndexError: ... >>> Try_.set_unhandled() >>> Try(getitem, [], 0) Failure(IndexError('list index out of range',))
-
-
class
tryingsnake.
Success
(v)¶ Represents a successful computation
-
class
tryingsnake.
Failure
(e)¶ Represents a unsuccessful computation