Giter Club home page Giter Club logo

tryingsnake's Introduction

TryingSnake

Tests 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)]
    

tryingsnake's People

Contributors

mfcabrera avatar zero323 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bennyelg

tryingsnake's Issues

Performance regression caused by type hints

It seems like adding type annotations (#5)

class Try_(Generic[T]):

caused rather unexpected performance regression:

Python 3.8.0 | packaged by conda-forge | (default, Nov 22 2019, 19:11:38) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.11.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import importlib                                                                                

In [2]: !git checkout 3a25dba30a527476deb4bfbd3cfa88ccd2b977ee                                          
Note: switching to '3a25dba30a527476deb4bfbd3cfa88ccd2b977ee'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at 3a25dba Provide type annotations and apply required refactoring

In [3]: import tryingsnake                                                                              

In [4]: %timeit for i in range(1_000): tryingsnake.Try(lambda x: x, 1)                                  
1.36 ms ± 12.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [5]: !git checkout HEAD~1                                                                            
Previous HEAD position was 3a25dba Provide type annotations and apply required refactoring
HEAD is now at a666744 Merge pull request #4 from zero323/failed

In [6]: importlib.reload(tryingsnake)                                                                   
Out[6]: <module 'tryingsnake' from '/path/to/tryingsnake/tryingsnake/__init__.py'>

In [7]: %timeit for i in range(1_000): tryingsnake.Try(lambda x: x, 1)                                  
668 µs ± 6.82 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

It seems that the main culprit is Generic[T] added as a base class for Try_.

asciicast

Such overhead is rather unacceptable in practice, so unless it is considered a bug, and solved upstream, we should probably move annotations to stub files (401ec6f).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.