{ "info": { "author": "Alisue", "author_email": "lambdalisue@hashnote.net", "bugtrack_url": null, "classifiers": [], "description": "tolerance\n==========================\n.. image:: https://secure.travis-ci.org/lambdalisue/tolerance.png?branch=master\n :target: http://travis-ci.org/lambdalisue/tolerance\n :alt: Build status\n\n.. image:: https://coveralls.io/repos/lambdalisue/tolerance/badge.png?branch=master\n :target: https://coveralls.io/r/lambdalisue/tolerance/\n :alt: Coverage\n\n.. image:: https://pypip.in/d/tolerance/badge.png\n :target: https://pypi.python.org/pypi/tolerance/\n :alt: Downloads\n\n.. image:: https://pypip.in/v/tolerance/badge.png\n :target: https://pypi.python.org/pypi/tolerance/\n :alt: Latest version\n\n.. image:: https://pypip.in/wheel/tolerance/badge.png\n :target: https://pypi.python.org/pypi/tolerance/\n :alt: Wheel Status\n\n.. image:: https://pypip.in/egg/tolerance/badge.png\n :target: https://pypi.python.org/pypi/tolerance/\n :alt: Egg Status\n\n.. image:: https://pypip.in/license/tolerance/badge.png\n :target: https://pypi.python.org/pypi/tolerance/\n :alt: License\n\nAuthor\n Alisue \nSupported python versions\n 2.6, 2.7, 3.2, 3.3, 3.4\n\nDo you often write the fail silent codes like below?\n\n.. code-block:: python\n\n try:\n # do what ever you need...\n return \"foo\"\n except:\n # fail silently\n return \"\"\n\nThis kind of codes are often found in Django_ projects or programs which should\nnot raise any exceptions in product mode.\n\n**tolerance** is a function decorator to make a tolerant function; a function\nwhich does not raise any exceptions even there are exceptions.\nThis concept is quite useful for making stable product or ``prefer_int`` types\nof code described in Usage section.\n\n.. _Django: https://www.djangoproject.com/\n\nCheck\n`online documentation `_\nfor more details.\n\nFeatures\n--------\n\n+ Convert a function to a tolerant function\n+ The decorated function returns ``substitute`` (Default is ``None``) when it\n is not callable.\n The function returns a \"returned value\" from ``substitute`` function when\n it is callable.\n+ Ignoreing exceptions can be specified as a exception class list with\n ``exceptions`` argument.\n+ When ``fail_silently=False`` is passed to the decorated function,\n the function does not ignore exceptions (the argument name can be changed\n with making switch function via ``argument_switch_generator`` function).\n\nInstallation\n------------\nUse pip_ like::\n\n $ pip install tolerance\n\n.. _pip: https://pypi.python.org/pypi/pip\n\nUsage\n-----\nAssume that you need a function which convert a string to an integer when it is\npossible.\nWithout tolerance, you need to write a code like below\n\n.. code-block:: python\n\n >>> # without tolerance\n >>> def prefer_int_withot_tolerance(x):\n ... try:\n ... return int(x)\n ... except:\n ... # fail silently\n ... return x\n >>> prefer_int_withot_tolerance(0)\n 0\n >>> prefer_int_withot_tolerance('0')\n 0\n >>> prefer_int_withot_tolerance('zero')\n 'zero'\n\nHowever, with tolerance, you just need to write a single line code like\n\n.. code-block:: python\n\n >>> from tolerance import tolerate\n >>> prefer_int = tolerate(lambda x: x)(int)\n >>> prefer_int(0)\n 0\n >>> prefer_int('0')\n 0\n >>> prefer_int('zero')\n 'zero'\n\nOr you can use ``tolerate`` as a function decorator described in PEP-318_\n\n.. code-block:: python\n\n >>> from tolerance import tolerate\n >>> @tolerate(lambda x: x)\n ... def prefer_int_318(x):\n ... return int(x)\n >>> prefer_int_318(0)\n 0\n >>> prefer_int_318('0')\n 0\n >>> prefer_int_318('zero')\n 'zero'\n\nThe example codes above specify ``substitute`` argument of ``tolerate``\nfunction to specify the returning value when the function has failed (\n``lambda x: x`` part).\n``tolerate`` function takes several arguments to configure the function\nbehavior.\nThese arguments are explained in Case study and detailed in API documentation.\n\n.. _PEP-318: http://www.python.org/dev/peps/pep-0318/\n\nChange log\n----------\nVersion 0.1.0\n + Initial development\n + Manually tested with Python 2.4, 2.5, 2.7, 3.2, 3.3\nVersion 0.1.1\n + ``switch`` shortcut feature is added\n + Drop off supporting Python 2.4 and 2.5\n + Support Python 3.2 and 3.3 via 2to3\n + Use tox_ for testing\n\n.. _tox: http://tox.readthedocs.org/en/latest/index.html\n\nCase study\n----------\n\nQ. How can I return the default value when the function fail?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nA. Use ``substitute`` argument to specify the default value like\n\n.. code-block:: python\n \n >>> from tolerance import tolerate\n >>> @tolerate(substitute='foo')\n ... def raise_exception():\n ... raise Exception\n >>> raise_exception()\n 'foo'\n\nQ. How can I change the default value depends on passed arguments?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nA. Specify ``substitute`` argument as a function\n\n.. code-block:: python\n \n >>> from tolerance import tolerate\n >>> def substitute_function(*args, **kwargs):\n ... # do what ever you need, this example simply return 1st argument\n ... return args[0]\n >>> @tolerate(substitute=substitute_function)\n ... def raise_exception(*args):\n ... raise Exception\n >>> raise_exception('bar', 'hoge')\n 'bar'\n\nQ. How can I make the function to ignore only several exceptions?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nA. Use ``exceptions`` argument to specify exceptions which will be ignored.\n\n.. code-block:: python\n \n >>> from tolerance import tolerate\n >>> exceptions_ignored = (\n ... AttributeError,\n ... ValueError,\n ... )\n >>> @tolerate(exceptions=exceptions_ignored)\n ... def raise_exception(x):\n ... if x == 0:\n ... raise AttributeError\n ... elif x == 1:\n ... raise ValueError\n ... else:\n ... raise KeyError\n >>> raise_exception(0) is None\n True\n >>> raise_exception(1) is None\n True\n >>> raise_exception(2)\n Traceback (most recent call last):\n ...\n KeyError\n\nQ. How can I disable ignoreing exceptions in the decorated function?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nA. Pass ``fail_silently=False`` to the decorated function.\n\n.. code-block:: python\n \n >>> from tolerance import tolerate\n >>> @tolerate()\n ... def raise_exception():\n ... raise KeyError\n >>> raise_exception() is None\n True\n >>> raise_exception(fail_silently=False)\n Traceback (most recent call last):\n ...\n KeyError\n\nYou can change the attribute name with specifing new switch function.\nIt will be explained below.\n\nQ. How can I disable ignoreing exceptions globally?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nA. Set ``tolerate.disabled = True`` to disable tolerance globally.\n\n.. code-block:: python\n \n >>> from tolerance import tolerate\n >>> @tolerate()\n ... def raise_exception():\n ... raise KeyError\n >>> raise_exception() is None\n True\n >>> tolerate.disabled = True\n >>> raise_exception()\n Traceback (most recent call last):\n ...\n KeyError\n >>> # rollback\n >>> tolerate.disabled = False\n\nQ. How can I disable ignoreing exceptions in complex mannar?\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nA. Use ``switch`` argument to specify switch function.\n\n.. code-block:: python\n \n >>> from tolerance import tolerate\n >>> DEBUG = False\n >>> def switch_function(*args, **kwargs):\n ... # do what ever you need, this sample check kwargs and DEBUG\n ... # remove 'fail_silently' attribute and store\n ... fail_silently = kwargs.pop('fail_silently', True)\n ... if DEBUG or not fail_silently:\n ... # do not ignore exceptions. note that kwargs which does not\n ... # have 'fail_silently' is returned back.\n ... return False, args, kwargs\n ... # do ignore exceptions. note that kwargs which does not have\n ... # 'fail_silently' is returned back.\n ... return True, args, kwargs\n >>> @tolerate(switch=switch_function)\n ... def raise_exception():\n ... raise KeyError\n >>> raise_exception() is None\n True\n >>> raise_exception(fail_silently=False)\n Traceback (most recent call last):\n ...\n KeyError\n >>> DEBUG = True\n >>> raise_exception()\n Traceback (most recent call last):\n ...\n KeyError\n\nQ. I just want to change the attribute name, making switch function is too complicated\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nA. Use ``argument_switch_generator`` to make switch function.\n\n.. code-block:: python\n \n >>> from tolerance import tolerate\n >>> from tolerance import argument_switch_generator\n >>> switch_function = argument_switch_generator('quiet')\n >>> @tolerate(switch=switch_function)\n ... def raise_exception():\n ... raise KeyError\n >>> raise_exception() is None\n True\n >>> # you can use `quiet=False` instead of `fail_silently`\n >>> raise_exception(quiet=False)\n Traceback (most recent call last):\n ...\n KeyError\n >>> # raise_exception does not know fail_silently so ignore\n >>> raise_exception(fail_silently=False) is None\n True\n >>> #\n >>> # From Version 0.1.1\n >>> #\n >>> @tolerate(switch='quiet')\n ... def raise_exception():\n ... raise KeyError\n >>> raise_exception() is None\n True\n >>> raise_exception(quiet=False)\n Traceback (most recent call last):\n ...\n KeyError\n >>> raise_exception(fail_silently=False) is None\n True\n\n.. note::\n From Version 0.1.1, you can simply specify the argument name to ``switch``\n argument and then ``tolerant`` function will call\n ``argument_switch_generator`` internally with the specified name.\n\n See detailed informations on API documentation\n\nQ. I want to make the function ignoreing exceptions only when ``fail_silently=True`` is passed\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nA. Use ``default`` argument to tell ``argument_switch_generator`` function\n\n.. code-block:: python\n \n >>> from tolerance import tolerate\n >>> from tolerance import argument_switch_generator\n >>> switch_function = argument_switch_generator('fail_silently', default=False)\n >>> @tolerate(switch=switch_function)\n ... def raise_exception():\n ... raise KeyError\n >>> raise_exception() is None\n Traceback (most recent call last):\n ...\n KeyError\n >>> raise_exception(fail_silently=True) is None\n True\n >>> #\n >>> # From Version 0.1.1\n >>> #\n >>> @tolerate(switch=[None, False])\n ... def raise_exception():\n ... raise KeyError\n >>> raise_exception() is None\n Traceback (most recent call last):\n ...\n KeyError\n >>> @tolerate(switch={'default': False})\n ... def raise_exception():\n ... raise KeyError\n >>> raise_exception() is None\n Traceback (most recent call last):\n ...\n KeyError\n\n.. note::\n From Version 0.1.1, you can simply specify ``*args`` or ``**kwargs`` of\n ``argument_switch_generator`` to ``switch`` argument and ``tolerant``\n function will call ``argument_switch_generator`` internally with the\n specified arguments.\n\n See detailed informations on API documentation\n\nQ. I want to disable the ignoreing exceptions when ``verbose=False`` is passed\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nA. Use ``reverse`` argument to tell ``argument_switch_generator`` function\n\n.. code-block:: python\n \n >>> from tolerance import tolerate\n >>> from tolerance import argument_switch_generator\n >>> switch_function = argument_switch_generator('verbose', reverse=True)\n >>> @tolerate(switch=switch_function)\n ... def raise_exception():\n ... raise KeyError\n >>> raise_exception() is None\n True\n >>> raise_exception(verbose=True)\n Traceback (most recent call last):\n ...\n KeyError\n >>> #\n >>> # From Version 0.1.1\n >>> #\n >>> @tolerate(switch={'argument_name': 'verbose', 'reverse': True})\n ... def raise_exception():\n ... raise KeyError\n >>> raise_exception() is None\n True\n >>> raise_exception(verbose=True)\n Traceback (most recent call last):\n ...\n KeyError\n\nQ. I want to use ``fail_silently`` argument even in decorated function\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\nA. Use ``keep`` argument to tell ``argument_switch_generator`` function\n\n.. code-block:: python\n \n >>> from tolerance import tolerate\n >>> from tolerance import argument_switch_generator\n >>> switch_function = argument_switch_generator('fail_silently', keep=True)\n >>> @tolerate(switch=switch_function)\n ... def raise_exception(**kwargs):\n ... if 'fail_silently' in kwargs:\n ... raise KeyError\n ... return 'Failed!'\n >>> raise_exception(fail_silently=True) is None\n True\n >>> raise_exception(fail_silently=False)\n Traceback (most recent call last):\n ...\n KeyError\n >>> #\n >>> # From Version 0.1.1\n >>> #\n >>> @tolerate(switch={'keep': True})\n ... def raise_exception(**kwargs):\n ... if 'fail_silently' in kwargs:\n ... raise KeyError\n ... return 'Failed!'\n >>> raise_exception(fail_silently=True) is None\n True\n >>> raise_exception(fail_silently=False)\n Traceback (most recent call last):\n ...\n KeyError", "description_content_type": null, "docs_url": null, "download_url": "https://github.com/lambdalisue/tolerance/tarball/master", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/lambdalisue/tolerance", "keywords": "function decorator,decorator,fail silently", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "tolerance", "package_url": "https://pypi.org/project/tolerance/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/tolerance/", "project_urls": { "Download": "https://github.com/lambdalisue/tolerance/tarball/master", "Homepage": "https://github.com/lambdalisue/tolerance" }, "release_url": "https://pypi.org/project/tolerance/0.1.2/", "requires_dist": null, "requires_python": null, "summary": "A function decorator which makes a function tolerant (the function fail silently).", "version": "0.1.2" }, "last_serial": 1163800, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "0596210a1d7f09b085d3c2a07bcd83d3", "sha256": "2d4b05cc8eb27d982bbc12a2022ae71424f3aba00653075f8838ad9c8a015b4e" }, "downloads": -1, "filename": "tolerance-0.1.0.tar.gz", "has_sig": false, "md5_digest": "0596210a1d7f09b085d3c2a07bcd83d3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9956, "upload_time": "2014-01-20T16:13:24", "url": "https://files.pythonhosted.org/packages/b7/7f/371c91807dd084d37c14f1c67cceb86f1c8c2c1f5d8a7421da823e30535b/tolerance-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "e0a44ec7af8d7f5571f6d0e5e708e9be", "sha256": "2a6f8399c8246b4a53ae44c91c3804ed1b262f40bd4f443c7258e74019a7b177" }, "downloads": -1, "filename": "tolerance-0.1.1.tar.gz", "has_sig": false, "md5_digest": "e0a44ec7af8d7f5571f6d0e5e708e9be", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11157, "upload_time": "2014-01-21T05:33:57", "url": "https://files.pythonhosted.org/packages/b4/6a/f42465f3af24076a0dea9d5bc770f4d8fb43c0367cad24551bc661cd082f/tolerance-0.1.1.tar.gz" } ], "0.1.2": [ { "comment_text": "", "digests": { "md5": "b092c809d6c9b31c32d84411597c8d93", "sha256": "a551a591468261cadb5440dcc19a8d5577514956f0ab283a18a56ce1ff56e51e" }, "downloads": -1, "filename": "tolerance-0.1.2.tar.gz", "has_sig": false, "md5_digest": "b092c809d6c9b31c32d84411597c8d93", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11213, "upload_time": "2014-07-21T01:01:40", "url": "https://files.pythonhosted.org/packages/68/19/e49b2915d182f9967bb6950faad6935ece45566356a3bf50e8f5c73ab4d7/tolerance-0.1.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "b092c809d6c9b31c32d84411597c8d93", "sha256": "a551a591468261cadb5440dcc19a8d5577514956f0ab283a18a56ce1ff56e51e" }, "downloads": -1, "filename": "tolerance-0.1.2.tar.gz", "has_sig": false, "md5_digest": "b092c809d6c9b31c32d84411597c8d93", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11213, "upload_time": "2014-07-21T01:01:40", "url": "https://files.pythonhosted.org/packages/68/19/e49b2915d182f9967bb6950faad6935ece45566356a3bf50e8f5c73ab4d7/tolerance-0.1.2.tar.gz" } ] }