{ "info": { "author": "Bartek \u0106wik\u0142owski", "author_email": "paczesiowa@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: Public Domain", "Operating System :: POSIX", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6" ], "description": "# Type Value Unifier\n\n# Intro\ntvu library allows you to abstract code, that is usually present at the beginning of most\nfunctions/methods, and handles parameter checking, validation and unification.\nThe goal is to share most of this code to be usable with many functions and\nprovide helpful exceptions to users of your code. tvu does not force you\nto use all of available type-checking, unification and validation features,\nif you don't need them.\n\n```python\nimport tvu\n```\n\n# Typechecking\n## Rationale\nThe following code:\n\n```python\ndef foo(x, y):\n ....\n m = re.match(y, 'bar')\n ...\n\nfoo(None, 2)\n```\n\nResults in an exception that is thrown from re.match(), but the user-error\nwas made when calling foo() with int instead of string. It would be more\nuser-friendly to get an exception, at the exact place error was made,\nthat mentions specifically what was the problem.\n\n## Usage\nMost basic feature of tvu is simple type-checking of function/method's arguments.\nThis is the simplest tvu, that checks if function argument is an integer:\n\n```python\nclass IntTVU(tvu.TVU):\n TYPES = (int,)\n```\n\nTo actually use it, you can use whole tvu module as a function decorator,\nwith previously defined IntTVU as value of parameter x (it must match\nfunction argument name):\n```python\n@tvu(x=IntTVU)\ndef foo(x):\n print \"I'm an integer %d\" % x\n```\n\nIf foo() is called with int parameter it work as expected, but calling it\nwith anything else results in a nice exception:\n\n```\n>>> foo('3')\nTraceback (most recent call last):\n File \"test.py\", line 12, in \n foo('3')\n File \"/tmp/tvu-0.1/tvu/_tvu.py\", line 70, in inner_wrapper\n validator(arg).unify_validate(args_values[arg])\n File \"/tmp/tvu-0.1/tvu/_tvu.py\", line 38, in unify_validate\n self.type_check()\n File \"/tmp/tvu-0.1/tvu/_tvu.py\", line 34, in type_check\n raise TypeError(err_msg)\nTypeError: x must be int, not '3'\n```\n\nThis case of checking if argument is instance of one specific type can be done\nwith a instance wrapper:\n\n```python\n@tvu(x=tvu.instance(int))\ndef foo(x):\n print \"I'm an integer %d\" % x\n```\n\nSubclasses of TVU can provide more types in TYPES tuple, to check arguments\nagainst more available types:\n\n```python\nclass NumberTVU(tvu.TVU):\n TYPES = (int, float)\n\n@tvu(x=NumberTVU)\ndef double(x):\n return 2 * x\n```\n# Unification\nThe goal of unification is to allow wider selection of types as a function's argument,\nbut to unify them to a common value/type before supplying it to a function.\n\nFor example, in python2 you might need a unicode text argument, but don't want \nto discourage use of plain strings when they are ascii-only. The following TVU\nallows you to think only about unicode values but still allow ascii byte strings:\n\n```python\nclass Text(tvu.TVU):\n\n TYPES = (unicode, str)\n\n def unify(self, value):\n if isinstance(value, str):\n try:\n return value.decode('ascii')\n except UnicodeDecodeError:\n self.error(u'unicode text, or ascii-only bytestring')\n return value\n\n@tvu(x=Text)\ndef write(x):\n with open('/tmp/text.txt', 'wb') as f:\n f.write(x.encode('utf-8'))\n\nwrite(u'\u017c\u00f3\u0142w')\nwrite('ascii')\n```\n\nBetter version of Text is available as tvu.tvus.Text\n\n# Validation\nValidation is about validating function's arguments to restrict value space.\nThe following TVU disallows non-positive integers:\n\n```python\nclass PositiveInt(tvu.TVU):\n TYPES = (int,)\n\n def validate(self, value):\n if value <= 0:\n self.error('positive number')\n```\n\n# FAQ\n* **How to install it?**\n\npip install tvu\n* **But python is about duck typing?**\n\nThere's nothing stopping you from using tvu in duck typing way by checking existence of\ncertain methods in TVU.validate() method.\n* **Does it work with python3?**\n\ntvu has been tested with python-2.7 and python-3.6", "description_content_type": null, "docs_url": null, "download_url": "http://github.com/Paczesiowa/tvu/tarball/v0.2.2", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/Paczesiowa/tvu", "keywords": "typecheck,decorator", "license": "UNLICENSE/Public Domain", "maintainer": null, "maintainer_email": null, "name": "tvu", "package_url": "https://pypi.org/project/tvu/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/tvu/", "project_urls": { "Download": "http://github.com/Paczesiowa/tvu/tarball/v0.2.2", "Homepage": "https://github.com/Paczesiowa/tvu" }, "release_url": "https://pypi.org/project/tvu/0.2.2/", "requires_dist": null, "requires_python": null, "summary": "Library for typechecking/validation/unification of function arguments.", "version": "0.2.2" }, "last_serial": 2829766, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "f259895dd79520e6687de926589e9585", "sha256": "65c36f8496b8f91118d611db1ffaf3e0bd96cdb770536b4bf7f0d8903537d155" }, "downloads": -1, "filename": "tvu-0.1.tar.gz", "has_sig": false, "md5_digest": "f259895dd79520e6687de926589e9585", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9211, "upload_time": "2017-02-10T20:35:13", "url": "https://files.pythonhosted.org/packages/4b/e5/571f0482140cabf3e05e5dde3e69c6dff691869bdbc6b0c94f99bff13d96/tvu-0.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "83f864d397f65d852f689091db625178", "sha256": "44c6463eab41f75238477d119a1e031425b4f16a7756b74e754ec8f4da703303" }, "downloads": -1, "filename": "tvu-0.2.tar.gz", "has_sig": false, "md5_digest": "83f864d397f65d852f689091db625178", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11371, "upload_time": "2017-02-15T12:12:47", "url": "https://files.pythonhosted.org/packages/21/b0/6bc895b022049644c2a43c77fd806c60ab374711eed70add7cc7a695ac60/tvu-0.2.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "c201891266cae1b809e1a2a00fadd036", "sha256": "e95297564f98ade713a4e0e68f049de5b26fe93a8a0705721d1e3676ffaa7d36" }, "downloads": -1, "filename": "tvu-0.2.1.tar.gz", "has_sig": false, "md5_digest": "c201891266cae1b809e1a2a00fadd036", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11444, "upload_time": "2017-04-19T19:37:48", "url": "https://files.pythonhosted.org/packages/06/91/624dac9f87898d41f76dfc7e6c5a2b30f32421198b2e817078e8b316fd02/tvu-0.2.1.tar.gz" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "6144584fd60eff7cd22277c0f6bcf972", "sha256": "2e6133325205718702a7344bae7838b20f9b6c357baae0e8d81fceada5ee78fc" }, "downloads": -1, "filename": "tvu-0.2.2.tar.gz", "has_sig": false, "md5_digest": "6144584fd60eff7cd22277c0f6bcf972", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 22715, "upload_time": "2017-04-25T19:18:30", "url": "https://files.pythonhosted.org/packages/73/9d/72ffb39d3045bea14a0900e1d69bc2f87b7dafb2b1efec2888575df1188f/tvu-0.2.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "6144584fd60eff7cd22277c0f6bcf972", "sha256": "2e6133325205718702a7344bae7838b20f9b6c357baae0e8d81fceada5ee78fc" }, "downloads": -1, "filename": "tvu-0.2.2.tar.gz", "has_sig": false, "md5_digest": "6144584fd60eff7cd22277c0f6bcf972", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 22715, "upload_time": "2017-04-25T19:18:30", "url": "https://files.pythonhosted.org/packages/73/9d/72ffb39d3045bea14a0900e1d69bc2f87b7dafb2b1efec2888575df1188f/tvu-0.2.2.tar.gz" } ] }