Skip to main content

Exception handling, the functional way.

Project description

TryingSnake

Build Status Coverage Status Code Climate GitHub release (latest by date) PyPI Conda Version License MIT

A simple, Try implementation inspired by scala.util.Try

Examples

  • Wrap functions with arguments:

    >>> from tryingsnake import Try, Try_, Success, Failure
    >>> from operator import add, truediv
    >>> Try(add, 0, 1)
    Success(1)
    >>> Try(truediv, 1, 0)  # doctest:+ELLIPSIS
    Failure(ZeroDivisionError(...))
    
  • Avoid sentinel values:

    >>> def mean_1(xs):
    ...     try:
    ...         return sum(xs) / len(xs)
    ...     except ZeroDivisionError as e:
    ...         return float("inf")  # What does it mean?
    >>> mean_1([])
    inf
    

    vs.

    >>> def mean_2(xs):
    ...     return sum(xs) / len(xs)
    >>> Try(mean_2, [])  # doctest:+ELLIPSIS
    Failure(ZeroDivisionError(...))
    >>> Try(mean_2, ["foo", "bar"])  # doctest:+ELLIPSIS
    Failure(TypeError(...))
    
  • Follow the happy path:

    >>> def inc(x): return x + 1
    >>> def inv(x): return 1. / x
    
    >>> Success(1).map(inc).map(inv)
    Success(0.5)
    
    >>> Failure(Exception("e")).map(inc).map(inv)  # doctest:+ELLIPSIS
    Failure(Exception(...))
    
    >>> Success(-1).map(inc).map(inv)  # doctest:+ELLIPSIS
    Failure(ZeroDivisionError(...))
    
  • Recover:

    >>> def get(url):
    ...     if "mirror" in url:
    ...         raise IOError("No address associated with hostname")
    ...     return url
    >>> mirrors = ["http://mirror1.example.com", "http://example.com"]
    >>> Try(get, mirrors[0]).recover(lambda _: get(mirrors[1]))
    Success('http://example.com')
    
  • Let them fail:

    >>> from operator import getitem
    >>> Try(getitem, [], 0)  # doctest:+ELLIPSIS
    Failure(IndexError(...))
    >>> Try_.set_unhandled([IndexError])
    >>> Try(getitem, [], 0)
    Traceback (most recent call last):
        ...
    IndexError: list index out of range
    
  • Make things (relatively) simple:

    >>> import math
    >>> xs = [1.0, 0.0, "-1", -3, 2, 1 + 2j]
    >>> sqrts = [Try(math.sqrt, x) for x in xs]
    >>> [x.get() for x in sqrts if x.isSuccess]
    [1.0, 0.0, 1.4142135623730951]
    >>> def get_etype(e):
    ...     return Try(lambda x: type(x).__name__, e)
    >>> [x.recoverWith(get_etype).get() for x in sqrts if x.isFailure]
    ['TypeError', 'ValueError', 'TypeError']
    
  • Inline exception handling:

    >>> from tryingsnake.curried import Try
    >>> map(Try(str.split), ["foo bar", None])  # doctest:+ELLIPSIS
    <map at ...>
    
  • Decorate your functions:

    >>> from tryingsnake.curried import Try as try_
    >>> @try_
    ... def scale_imag(x):
    ...     return complex(x.real, x.imag * 2)
    >>> [scale_imag(x) for x in [1 + 2j, "3", 42 + 0j]]
    [Success((1+4j)), Failure(AttributeError("'str' object has no attribute 'real'")), Success((42+0j))]
    
  • Wrap generator objects:

    >>> def get_nth(xs, i):
    ...     yield xs[i]
    >>> xs = [1, 3, 5, 7]
    >>> Try(get_nth(xs, 3))
    Success(7)
    >>> Try(get_nth(xs, 11))
    Failure(IndexError('list index out of range'))
    >>> def f():
    ...     divisor = 1
    ...     while True:
    ...         divisor_ = yield 1 / divisor
    ...         divisor = divisor_ if divisor_ is not None else 1
    >>> g = f()
    >>> next(g)  # Should be primed
    1.0
    >>> Try(g, 2)
    Success(0.5)
    >>> Try(g, 0)
    Failure(ZeroDivisionError('division by zero'))
    

Installation

This package is available on PYPI:

pip install tryingsnake

and conda-forge:

conda install -c conda-forge tryingsnake

Dependencies

tryingsnake supports Python 3.6 or later and requires no external dependencies.

License

MIT, See LICENSE

FAQ

  • Q: Is this project production-ready?

  • A: Sure, for some definition of production-ready. It is a toy project. It has decent test coverage, stable API, and in general seems to do what is expected to do. But it is not widely used, and the API design and overall idea are rather unpythonic.

  • Q: Why to use mixedCase method names instead of lowercase recommended by PEP8?

  • A: Mostly to make switching between Python and Scala code as painless as possible.

  • Q: What is the runtime cost?
    A: As of 0088286 (releases 0.3 and 0.4 suffered from severe performance regression caused by using typing.Generic as a base of try. See #18 for details) rough numbers for simple tasks look as follows:

    Python 3.7.5 (default, Oct 27 2019, 15:43:29)
    Type 'copyright', 'credits' or 'license' for more information
    IPython 7.11.0 -- An enhanced Interactive Python. Type '?' for help.
    In [1]: def identity(x): return x
    In [2]: from tryingsnake import Try
    In [3]: %timeit for i in range(1_000_000): identity(i)
    59.8 ms ± 683 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
    
    In [4]: %timeit for i in range(1_000_000): Try(identity, i)
    408 ms ± 4.14 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
    

    and execution time is dominated by the initializer:

    In [5]: import cProfile
    In [6]: cProfile.run("for i in range(1_000_000): Try(identity, i)")
             4000003 function calls in 0.961 seconds
    
       Ordered by: standard name
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
      1000000    0.078    0.000    0.078    0.000 <ipython-input-1-abafd771428d>:1(identity)
            1    0.263    0.263    0.961    0.961 <string>:1(<module>)
      1000000    0.094    0.000    0.094    0.000 __init__.py:234(__init__)
      1000000    0.480    0.000    0.698    0.000 __init__.py:352(Try)
      1000000    0.046    0.000    0.046    0.000 {built-in method builtins.callable}
            1    0.000    0.000    0.961    0.961 {built-in method builtins.exec}
            1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
    

    This is quite a lot for simple functions so you should probably avoid it in such cases, where raw performance is important. It is still possible to amortize the cost in such cases, for example using composition:

    from toolz.functoolz import compose
    from tryingsnake import Try
    
    Try(compose(str.split, str.lower, str.strip), " Foo BAR FooBar ")
    

    Memory overhead (as measured by memory-profiler) looks as follows:

    Line #    Mem usage    Increment   Line Contents
    ================================================
     6     37.9 MiB     37.9 MiB   @profile
     7                             def f():
     8    155.5 MiB      0.8 MiB       [Try(identity, i) for i in range(1_000_000)]
    

    compared to:

    Line #    Mem usage    Increment   Line Contents
    ================================================
     6     37.9 MiB     37.9 MiB   @profile
     7                             def f():
     8     77.4 MiB      1.0 MiB       [identity(i) for i in range(1_000_000)]
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tryingsnake-0.5.1.tar.gz (13.1 kB view details)

Uploaded Source

File details

Details for the file tryingsnake-0.5.1.tar.gz.

File metadata

  • Download URL: tryingsnake-0.5.1.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.7

File hashes

Hashes for tryingsnake-0.5.1.tar.gz
Algorithm Hash digest
SHA256 761c93661764e9af4d26243bd1b53da869328c76ce7fb7c82405d1fc16ff0946
MD5 5026d13fe1bcfdf0430f9966bbfa9765
BLAKE2b-256 4468275c71df83b7812df12ff22396455d40af61f6b669f32473cc4253b5ce8e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page