-
IO & IOResultpython/returns 2022. 9. 4. 22:10
https://returns.readthedocs.io/en/latest/pages/io.html#
retry, error recovery 등의 추가 기능을 제공하지 않는 것 같다.
차라리 Result (or ResultE) 를 사용하는 것이 나을 것 같다.
########################################################################### # IO # Some IO never fails, like: getting current date and time, random number, or OS name ########################################################################### import random from returns.io import IO from returns.unsafe import unsafe_perform_io x = IO(random.randint(1, 10)) y = IO(random.choice(['A', 'B'])) z = IO.do( str(a) + ':' + b for a in x for b in y ) unsafe_perform_io(z) ########################################################################### # IOResult # Some IO might fail, like: sending network requests, accessing filesystem, or database ########################################################################### from returns.io import IOResult, IOResultE, IOSuccess, IOFailure from returns.result import Result, Success from returns.unsafe import unsafe_perform_io x: Result[int, Exception] = Success(2) y: Result[int, Exception] = Success(3) io_x: IOResultE[int] = IOResult.from_result(x) io_y: IOResultE[int] = IOResult.from_result(y) io_success: IOResultE[int] = IOSuccess(4) io_failure: IOResultE[int] = IOFailure(ValueError("invalid value")) z = IOResult.do( a + b for a in io_x for b in io_y ) match z: case IOSuccess(x): print(x) case IOFailure(e): print(e) unsafe_perform_io(z)
'python > returns' 카테고리의 다른 글
Result & ResultE (0) 2022.09.04 Maybe (0) 2022.09.04