Try

example_try.py
from pymonet.monad_try import Try

def divide(dividend, divisor):
    return dividend / divisor

def success_callback(value):
    print('success: {}'.format(value))

def fail_callback(error):
    print('error: {}'.format(value))

(Try.of(divide, 42, 2)
    .on_success(success_callback)
    .on_fail(fail_callback))
# success: 21

(Try.of(divide, 42, 0)
    .on_success(success_callback)
    .on_fail(fail_callback))
#error: division by zero

# map method will be only applied mapper when exception was not thrown

(Try.of(divide, 42, 2)
    .map(lambda value: value + 1)
    .on_success(success_callback)
    .on_fail(fail_callback))
# success: 22

(Try.of(divide, 42, 0)
    .on_success(success_callback)
    .map(lambda value: value + 1)
    .on_fail(fail_callback))
#error: division by zero

# get_or_else method returns value when exception was not thrown

Try.of(divide, 42, 2).get_or_else('Holy Grail') # 21
Try.of(divide, 42, 0).get_or_else('Holy Grail') # 'Holy Grail'

# get method should return value with or without exception thrown

Try.of(divide, 42, 2).get()  # 21
Try.of(divide, 42, 0).get()  # ZeroDivisionError<'division by zero'>
class pymonet.monad_try.Try(value, is_success: bool)

The Try control gives us the ability write safe code without focusing on try-catch blocks in the presence of exceptions.

bind(binder)

Take function and applied this function with monad value and returns function result.

Params binder

function to apply on monad value

Returns

for successfully result of binder, othercase copy of self

Return type

Try[B]

filter(filterer)

Take filterer function, when monad is successfully call filterer with monad value. When filterer returns True method returns copy of monad, othercase not successfully Try with previous value.

Params filterer

function to apply on monad value

Returns

Try with previous value

Return type

Try[A]

get()

Return monad value.

Returns

monad value

Return type

A

get_or_else(default_value)

Return monad value when is successfully. Othercase return default_value argument.

Params default_value

value to return when monad is not successfully.

Returns

monad value

Return type

A | B

map(mapper)

Take function and applied this function with monad value and returns new monad with mapped value.

Params mapper

function to apply on monad value

Returns

for successfully new Try with mapped value, othercase copy of self

Return type

Try[B]

classmethod of(fn: Callable, *args)

Call argument function with args in try-catch. when function don’t raise exception, not successfully when raise.

Params fn

function to call and store in monad

Params *args*args

Retruns

Successfully monad Try when function don’t raise exception, not successfully when raise

Return type

Try[A]

on_fail(fail_callback)

Call success_callback function with monad value when monad is not successfully.

Params fail_callback

function to apply with monad value.

Returns

self

Return type

Try[A]

on_success(success_callback)

Call success_callback function with monad value when monad is successfully.

Params success_callback

function to apply with monad value.

Returns

self

Return type

Try[A]