This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

TryingSnake

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)
    Failure(Exception('e',))
    
    >>> 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)
    Failure(IndexError('list index out of range',))
    >>> 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']
    

Installation

pip install tryingsnake

or

easy_install tryingsnake

License

MIT, See LICENSE

FAQ

  • Q: Is this project production-ready?
  • A: No, and it probably won’t be.
  • 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.
Release History

Release History

0.2.6

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.5

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.4

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.3

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.2.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
tryingsnake-0.2.6.tar.gz (5.1 kB) Copy SHA256 Checksum SHA256 Source Mar 7, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting