Advertisement
FranzVuttke

test_result.py

Dec 8th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | Source Code | 0 0
  1. #!/home/ounis/pyapps/virtual_env/test_result/bin/python
  2.  
  3.  
  4.  
  5. '''
  6. Python errors as values: Comparing useful patterns from Go and Rust
  7. https://www.inngest.com/blog/python-errors-as-values?fbclid=IwAR2DwzgjiRm2W2TL70VMUwjdY_xX0aLbIqusr2WdRIBpF7NzzuzIwq65qBU
  8. https://pypi.org/project/result/
  9. '''
  10.  
  11. from result import Result, Ok, Err
  12.  
  13. def divide(a: int, b: int) -> Result[int, str]:
  14.     '''
  15.    jazda bez trzymanki
  16.    '''
  17.     # result = 0
  18.     # result = a // b
  19.     #
  20.     # return Ok(result)
  21.  
  22.     if b == 0:
  23.         return Err("Cannot divide by zero")
  24.     return Ok(a // b)
  25.  
  26. values = [(6, 2),(10, 0), (10, 5)]
  27. for i, (a, b) in enumerate(values,1):
  28.     print(i)
  29.     divide_result = divide(a, b)
  30.     match divide_result:
  31.         case Ok(value):
  32.             print(f"{a} // {b} == {value}", flush=True)
  33.         case Err(e):
  34.             print(e)
  35.  
  36.  
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement