{ "info": { "author": "Paul Ganssle", "author_email": "paul@ganssle.io", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Natural Language :: English", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6" ], "description": "========\nvariants\n========\n\n\n.. image:: https://img.shields.io/pypi/v/variants.svg\n :target: https://pypi.org/project/variants/\n\n.. image:: https://img.shields.io/travis/python-variants/variants.svg\n :target: https://travis-ci.org/python-variants/variants\n\n.. image:: https://readthedocs.org/projects/variants/badge/?version=latest\n :target: https://variants.readthedocs.io/en/latest/?badge=latest\n :alt: Documentation Status\n\n``variants`` is a library that provides syntactic sugar for creating alternate forms of functions and other callables, in the same way that alternate constructors are class methods that provide alternate forms of the constructor function.\n\nTo create a function with variants, simply decorate the primary form with ``@variants.primary``, which then adds the ``.variant`` decorator to the original function, which can be used to register new variants. Here is a simple example of a function that prints text, with variants that specify the source of the text to print:\n\n.. code-block:: python\n\n import variants\n\n @variants.primary\n def print_text(txt):\n print(txt)\n\n @print_text.variant('from_file')\n def print_text(fobj):\n print_text(fobj.read())\n\n @print_text.variant('from_filepath')\n def print_text(fpath):\n with open(fpath, 'r') as f:\n print_text.from_file(f)\n\n @print_text.variant('from_url')\n def print_text(url):\n import requests\n r = requests.get(url)\n print_text(r.text)\n\n\n``print_text`` and its variants can be used as such:\n\n.. code-block:: python\n\n print_text('Hello, world!') # Hello, world!\n\n # Create a text file\n with open('hello_world.txt', 'w') as f:\n f.write('Hello, world (from file)')\n\n # Print from an open file object\n with open('hello_world.txt', 'r') as f:\n print_text.from_file(f) # Hello, world (from file)\n\n # Print from the path to a file object\n print_text.from_filepath('hello_world.txt') # Hello, world (from file)\n\n # Print from a URL\n hw_url = 'https://ganssle.io/files/hello_world.txt'\n print_text.from_url(hw_url) # Hello, world! (from url)\n\n\nDifferences from singledispatch\n-------------------------------\n\nWhile ``variants`` and |singledispatch|_ are both intended to provide alternative implementations\nto a primary function, the overall aims are slightly different. ``singledispatch`` transparently\ndispatches to variant functions based on the *type* of the argument, whereas ``variants`` provides\n*explicit* alternative forms of the function. Note that in the above example, both\n``print_text.from_filepath`` and ``print_text.from_url`` take a string, one representing a file\npath and one representing a URL.\n\nAdditionally, the ``variants`` is compatible with ``singledispatch``, so you can have the best of\nboth worlds; an example that uses both:\n\n\n.. code-block:: python\n\n @variants.primary\n @singledispatch\n def add(x, y):\n return x + y\n\n @add.variant('from_list')\n @add.register(list)\n def add(x, y):\n return x + [y]\n\nWhich then automatically dispatches between named variants based on type:\n\n.. code-block:: python\n\n >>> add(1, 2)\n 3\n >>> add([1], 2)\n [1, 2]\n\nBut also exposes the explicit variant functions:\n\n.. code-block:: python\n\n >>> add.from_list([1], 2)\n [1, 2]\n >>> add.from_list()\n 7 @add.register(list)\n 8 def add(x, y):\n ----> 9 return x + [y]\n\n TypeError: unsupported operand type(s) for +: 'int' and 'list'\n\nIt is important to note that the ``variants`` decorators **must be the outer decorators**.\n\n\nInstallation\n------------\n\nTo install variants, run this command in your terminal:\n\n.. code-block:: console\n\n $ pip install variants\n\n\nRequirements\n------------\n\nThis is a library for Python, with support for versions 2.7 and 3.4+.\n\n.. |singledispatch| replace:: ``singledispatch``\n.. _singledispatch: https://docs.python.org/3/library/functools.html#functools.singledispatch\n\n\n\nVersion 0.2.0 (2018-05-03)\n==========================\n\nFeatures\n--------\n\n- Added the ``variants.inspect`` module, which provides a public API for\n inspecting variant function groups. It currently provides two functions:\n ``is_primary`` and ``is_primary_method``. (GH #29)\n- Added python_requires to setup, indicating Python version support. (GH #34)\n\nBugfixes\n--------\n\n- Updated MANIFEST.in to include NEWS.rst (GH #34)\n\nImproved Documentation\n----------------------\n\n- Updated permanent redirect links in documentation. (GH #28)\n- Flattened TOC entry for changelog in documentation. (GH #32)\n- Added section for API documentation, including documentation for the inspect\n submodule. (GH #33)\n\n\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/python-variants/variants", "keywords": "variants", "license": "Apache Software License 2.0", "maintainer": "", "maintainer_email": "", "name": "variants", "package_url": "https://pypi.org/project/variants/", "platform": "", "project_url": "https://pypi.org/project/variants/", "project_urls": { "Homepage": "https://github.com/python-variants/variants" }, "release_url": "https://pypi.org/project/variants/0.2.0/", "requires_dist": null, "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*", "summary": "Library that enables the creation of alternate constructor-likevariant forms for arbitrary functions.", "version": "0.2.0" }, "last_serial": 3832448, "releases": { "0.0.0a0": [ { "comment_text": "", "digests": { "md5": "89a163be17c2886a43ae9f1717780337", "sha256": "fbab53ed5d790577c4f0639c20bfe2faed898d061b54dc933b10ba248876718a" }, "downloads": -1, "filename": "variants-0.0.0a0-py2.py3-none-any.whl", "has_sig": true, "md5_digest": "89a163be17c2886a43ae9f1717780337", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 5796, "upload_time": "2017-12-11T15:31:45", "url": "https://files.pythonhosted.org/packages/01/a2/afef26291fbac2b5710b371157c5bcc9c982f593de76d70948ebe6e8e7f2/variants-0.0.0a0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "300b0e6b066367acd7b9e5bd58959fe4", "sha256": "5cb867f488c23451ef8e9bd909ba54c144b5e5550a98b44cd7cd6fe1826d4bce" }, "downloads": -1, "filename": "variants-0.0.0a0.tar.gz", "has_sig": true, "md5_digest": "300b0e6b066367acd7b9e5bd58959fe4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15894, "upload_time": "2017-12-11T15:31:47", "url": "https://files.pythonhosted.org/packages/c2/ec/3a0a98e48150b61062567876dc872e3b978567d78e60f8f3fa8616117374/variants-0.0.0a0.tar.gz" } ], "0.1.0": [ { "comment_text": "", "digests": { "md5": "5ea9f660d742292d8255b5715e53f2f2", "sha256": "bd4572ac54ce20096fdb3b33b6bcfb876e8f1d376c5d4cdfc40f721eae10b073" }, "downloads": -1, "filename": "variants-0.1.0-py2.py3-none-any.whl", "has_sig": true, "md5_digest": "5ea9f660d742292d8255b5715e53f2f2", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 7261, "upload_time": "2017-12-13T23:20:16", "url": "https://files.pythonhosted.org/packages/cd/52/954d23383b84924766c3f313b91b1652f9bde986aa47e4c605ced4107ea3/variants-0.1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "cfba400785e5275e271cb2aa8c9dc9a2", "sha256": "1b2fbb17c409469335fe8c1d6dcae2589abecac410b4ee061e163e18ea783f9a" }, "downloads": -1, "filename": "variants-0.1.0.tar.gz", "has_sig": true, "md5_digest": "cfba400785e5275e271cb2aa8c9dc9a2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 17911, "upload_time": "2017-12-13T23:20:18", "url": "https://files.pythonhosted.org/packages/f4/0a/f3f8bcee4c7c2c53d0a4fefaa0bbaedd882088e942d38b245ee6b2475e74/variants-0.1.0.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "5c9a8b03aa592a9b9fbbbc0c1b133562", "sha256": "c1fb0a35dd9d822d371860ba08f9bc3d544772c225e7fc34ae8591a941609a81" }, "downloads": -1, "filename": "variants-0.2.0-py2.py3-none-any.whl", "has_sig": true, "md5_digest": "5c9a8b03aa592a9b9fbbbc0c1b133562", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*", "size": 5629, "upload_time": "2018-05-03T21:09:27", "url": "https://files.pythonhosted.org/packages/33/6a/28e8d20e2e7406c2a93f43c1c74be972b18a29edd8554dfe5926ea9eb5c0/variants-0.2.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "6b5967772affa7838782fc3c97469647", "sha256": "511f75b4cf7483c27e4d86d9accf2b5317267900c166d17636beeed118929b90" }, "downloads": -1, "filename": "variants-0.2.0.tar.gz", "has_sig": true, "md5_digest": "6b5967772affa7838782fc3c97469647", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*", "size": 19673, "upload_time": "2018-05-03T21:09:28", "url": "https://files.pythonhosted.org/packages/60/de/8fe198896ac7d4fb7dddb0b1a1d3ac8bb632a053ffede0d4bc469f2d905a/variants-0.2.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "5c9a8b03aa592a9b9fbbbc0c1b133562", "sha256": "c1fb0a35dd9d822d371860ba08f9bc3d544772c225e7fc34ae8591a941609a81" }, "downloads": -1, "filename": "variants-0.2.0-py2.py3-none-any.whl", "has_sig": true, "md5_digest": "5c9a8b03aa592a9b9fbbbc0c1b133562", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*", "size": 5629, "upload_time": "2018-05-03T21:09:27", "url": "https://files.pythonhosted.org/packages/33/6a/28e8d20e2e7406c2a93f43c1c74be972b18a29edd8554dfe5926ea9eb5c0/variants-0.2.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "6b5967772affa7838782fc3c97469647", "sha256": "511f75b4cf7483c27e4d86d9accf2b5317267900c166d17636beeed118929b90" }, "downloads": -1, "filename": "variants-0.2.0.tar.gz", "has_sig": true, "md5_digest": "6b5967772affa7838782fc3c97469647", "packagetype": "sdist", "python_version": "source", "requires_python": ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*", "size": 19673, "upload_time": "2018-05-03T21:09:28", "url": "https://files.pythonhosted.org/packages/60/de/8fe198896ac7d4fb7dddb0b1a1d3ac8bb632a053ffede0d4bc469f2d905a/variants-0.2.0.tar.gz" } ] }