Package: thresult
thresultPythonResultlibrary for handling returned values (Result,Ok,Err) from functions/methods and handling errors. It iserror handlinglibrary which is alternative totry/exceptstyle of programming.It is inspired by greatRustResult,Ok,Errtypes.InstallationpipinstallthresultSimple UsageTraditionaltry-exceptexampledefdiv(x:float,y:float)->float:z:float=x/yreturnzz0:float=div(1.0,2.0)# 0.5z1:float=div(1.0,0.0)# raises "ZeroDivisionError: division by zero" exceptionUnwrappingResultas Context ManagerfromthresultimportResult@Resultdefdiv(x:float,y:float)->float:z:float=x/yreturnztry:withdiv(1.0,0.0)asz:# unreachabler:float=zexceptZeroDivisionErrorase:# exception happenedpassUnwrappingResultinunwrapContext ManagerfromthresultimportResult,unwrap@Resultdeff(x:float,y:float)->float:z:float=x/yreturnztry:withunwrap():# unreachabler:float=f(1,0)exceptZeroDivisionErrorase:# exception happenedraiseePattern-matchingResultusingmatch-casefromthresultimportResult,Ok,Err@Resultdefdiv(x:float,y:float)->float:# can raise "ZeroDivisionError: division by zero" exceptionz:float=x/yreturnzr:Result=div(1.0,2.0)# Ok[float](0.5)matchr:caseOk(v):assertv==0.5caseErr(e):# unreachable# "ZeroDivisionError: division by zero"assertisinstance(e,ZeroDivisionError)In-place pattern-matchingResultusingwalrus operatorandmatch-case@Resultdefdiv(x:float,y:float)->float:# can raise "ZeroDivisionError: division by zero" exceptionz:float=x/yreturnzmatchr:=div(1.0,2.0):# Ok[float](0.5)caseOk(v):assertv==0.5caseErr(e):# unreachable# "ZeroDivisionError: division by zero"assertisinstance(e,ZeroDivisionError)Advanced UsageUnwrappingResult[float, ZeroDivisionError]as Context ManagerfromthresultimportResult,Ok,Err@Result[float,ZeroDivisionError]defdiv(x:float,y:float)->float:z:float=x/yreturnztry:withdiv(1.0,0.0)asz:# unreachablepassexceptZeroDivisionErrorase:# exception happenedpassManually create Result value, and Structural Pattern MatchingfromthresultimportResult,Ok,Errdefdiv(x:float,y:float)->Result[float,ZeroDivisionError]:res:Result[float,ZeroDivisionError]try:# can raise "ZeroDivisionError: division by zero" exceptionz:float=x/yres=Ok[float](z)exceptZeroDivisionErrorase:res=Err[ZeroDivisionError](e)returnresr0:Result=div(1.0,2.0)# Okr1:Result=div(1.0,0.0)# Errmatchr0:caseOk(v):print('Ok, value:',v)caseErr(e):print('Err, error:',e)# "ZeroDivisionError: division by zero"matchr1:caseOk(v):print('Ok, value:',v)caseErr(e):print('Err, error:',e)# "ZeroDivisionError: division by zero"z0:float=r0.unwrap()# 0.5z1:float=r1.unwrap_or(float('inf'))# infz1:float=r1.unwrap()# raises "ZeroDivisionError: division by zero" exceptionDecorate function with Result, and Structural Pattern MatchingfromthresultimportResult,Ok,Err@Result[float,ZeroDivisionError]defdiv(x:float,y:float)->float:# can raise "ZeroDivisionError: division by zero" exceptionz:float=x/yreturnzr0:Result=div(1.0,2.0)# Okr1:Result=div(1.0,0.0)# Errmatchr0:caseOk(v):print('Ok, value:',v)caseErr(e):print('Err, error:',e)# "ZeroDivisionError: division by zero"matchr1:caseOk(v):print('Ok, value:',v)caseErr(e):print('Err, error:',e)# "ZeroDivisionError: division by zero"z0:float=r0.unwrap()# 0.5z1:float=r1.unwrap_or(float('inf'))# infz1:float=r1.unwrap()# raises "ZeroDivisionError: division by zero" exceptionRun / Develop Cycledockerrun--rm-it-v$PWD/thresult:/code-w/codepython:3.11-alpinepython-Bresult.pyTestingdocker-composebuildthresult-test;docker-composerun--rm-v$PWD:/testthresult-testCoveragedocker-composebuildthresult-coverage;docker-composerun--rm-v$PWD:/testthresult-coverageBuildingdocker-composebuildthresult-build;docker-composerun--rmthresult-buildLicensingthresultis licensed under the BSD 3 license.Check theLICENSEfor details.