{ "info": { "author": "mittelholcz", "author_email": "dev.mittelholcz@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "# Contextfun\n\n[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/mittelholcz/contextfun/blob/master/LICENSE)\n[![pypi](https://img.shields.io/badge/pypi-0.7.0-blue.svg)](https://pypi.org/project/contextfun/)\n\nThe *contextfun* python package provides functions for context-based\nfiltering and mapping.\n\nWe can filter a list with the Python's own\n[*filter()*](https://docs.python.org/3/library/functions.html#filter)\nfunction, based on the properties of items.\nFor example, we can filter out prime numbers (`filter(is_prime, range(10))`).\nBut what if we are interested in numbers standing in the list before prime\nnumbers?\nOr numbers that have at least two prime numbers in their three radius context?\n\nSimilarly, we can replace items in a list, based on their properties with\nPython's own [*map()*](https://docs.python.org/3/library/functions.html#map>)\nfunction. For example, we can replace prime numbers with zeros\n(`map(lambda x: 0 if is_prime(x) else x, range(10))`).\nBut what if we want replace numbers standing in the list after prime numbers?\nOr numbers that have at most three prime numbers in their context?\n\nThis package can help to resolve this problem with their *contextual_filter()* and\n*contextual_map()* functions.\n\n## 1 Install\n\nYou can install *contextfun* easily from PyPI:\n\n```shell\npip install contextfun\n```\n\n## 2 Usage\n\n### 2.1 Filtering\n\n**contextual_filter**(*iterable, predicate, before=0, after=0, quantifier=all*)\n\nWith *contextual_filter* we can filter items of iterables based on their\ncontext. The function returns a generator of the filtered items.\n\n*iterable*:\nThe iterable to filter.\n\n*predicate*:\nA function which can be applied for items of the context. Its input is an\nitem of the *iterable*, its output is Boolean\n(`predicate(obj: object) -> bool`).\n\nThe context can be defined with the parameters *before* and *after*.\nThe context of an item consists of the *before* items standing in the\niterable before it, and the *after* items following it.\nThe current item itself is never a part of its context.\nThe context is truncated at the beginning and the end of the iterable.\nFor example, the -1, +2 context (`before=1, after=2`) of the items in the\nlist `[1, 2, 3, 4, 5]` are\n\n 1: (2, 3)\n 2: (1, 3, 4)\n 3: (2, 4, 5)\n 4: (3, 5)\n 5: (4)\n\n*quantifier*:\nThis parameter allows you to specify the part of the context for which the\n*predicate* should be true.\nDefault is Python's own\n[*all*](https://docs.python.org/3/library/functions.html#all) function.\nIn this case, the *predicate* should be true for all items of the context.\nYou can also choose the\n[*any*](https://docs.python.org/3/library/functions.html#any) function from the\nbuilt-in functions.\nAccording to this, the *predicate* must be true for at least one item of the\ncontext.\n\nYou can also write your own custom function with the following restrictions:\n(1) The only parameter of the function is the\n`(predicate(x) for x in context)` generator, and\n(2) the return value should be Boolean.\nFor example, the 'up to two' can be represented as\n`lambda context: sum((1 for x in context if x)) <= 2`.\n\nRemark: The default behavior (`quantifier=all`) may seem strange for empty\ncontexts, e.g.\n\n```python\n>>> for i in contextual_filter([1], lambda x: x%5==0, after=2):\n... print(i)\n1\n```\n\nThis is not a bug but follows the principles of predicate logic\n(see [vacuous truth](https://en.wikipedia.org/wiki/Vacuous_truth)).\n\n### 2.2 Mapping\n\n**contextual_map**(*iterable, mapping, predicate, before=0, after=0, quantifier=all*)\n\nWith *contextual_map* you can map an iterable based on the context of its\nitems. This function returns a generator of the mapped iterable.\n\nThe parameters are the same as that of *contextual_filter()*\nexcept the *mapping* parameter. It should be callable and will\nbe applied for the items of the iterable if its context fulfils the conditions\nrepresented by *predicate* and *quantifier*.\n\n### 2.3 Examples\n\nLook for words in a text after 'the':\n\n```python\n>>> from contextfun import contextual_filter\n>>> text = '''Alright but apart from the sanitation the medicine education\n... wine public order irrigation roads the fresh-water system and public\n... health what have the Romans ever done for us?'''.split()\n>>> pred = lambda word: word == 'the'\n>>> for word in contextual_filter(text, pred, before=1, quantifier=any):\n... print(word)\nsanitation\nmedicine\nfresh-water\nRomans\n```\n\nHighlight the words after the word 'the' in a text:\n\n```python\n>>> from contextfun import contextual_map\n>>> highlighter = lambda word: f'{word}'\n>>> words = contextual_map(text, highlighter, pred, before=1, quantifier=any)\n>>> ' '.join(words)\n'Alright but apart from the sanitation the medicine education wine public order irrigation roads the fresh-water system and public health what have the Romans ever done for us?'\n```\n\n## 3 Development setup\n\nRequirements:\n\n* bash\n* make\n* python3.7\n* [pipenv](https://pipenv.readthedocs.io/en/latest/)\n\nInstall other development dependencies:\n\n```shell\nmake dev\n```\n\nRun automated test-suite:\n\n```shell\nmake test\n```\n\n## 4 Release history\n\n* 0.5.0\n * Work in progress\n* 0.6.0\n * initial release\n* 0.6.1\n * updated readme\n* 0.7.0\n * performance improvement\n\n## 5 Acknowledgments\n\n* [@dlazesz](https://github.com/dlazesz)\n* [@esztersimon](https://github.com/esztersimon)\n* [@sassbalint](https://github.com/sassbalint)\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/mittelholcz/contextfun", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "contextfun", "package_url": "https://pypi.org/project/contextfun/", "platform": "", "project_url": "https://pypi.org/project/contextfun/", "project_urls": { "Homepage": "https://github.com/mittelholcz/contextfun" }, "release_url": "https://pypi.org/project/contextfun/0.7.0/", "requires_dist": null, "requires_python": "", "summary": "context based filtering and mapping", "version": "0.7.0" }, "last_serial": 4947911, "releases": { "0.6.0": [ { "comment_text": "", "digests": { "md5": "df41b7e540285d877eeb17e4cf31443b", "sha256": "97e1e1cc428a800d7107d6f27d96752d60b30de515a4fba5b673939ef3c92e1c" }, "downloads": -1, "filename": "contextfun-0.6.0-py3-none-any.whl", "has_sig": false, "md5_digest": "df41b7e540285d877eeb17e4cf31443b", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 5126, "upload_time": "2019-03-02T15:58:24", "url": "https://files.pythonhosted.org/packages/12/e9/0ae56e9da60c4c3888f785f625f89609993c32d981f17346240efdd72546/contextfun-0.6.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "5a1670a2b25dd7be83c2e9b51b33d643", "sha256": "aa5dd1c736abbdaa65e82f06c6a436ab123b3418021a247c89bf51a1ff9491bd" }, "downloads": -1, "filename": "contextfun-0.6.0.tar.gz", "has_sig": false, "md5_digest": "5a1670a2b25dd7be83c2e9b51b33d643", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15749, "upload_time": "2019-03-02T15:58:26", "url": "https://files.pythonhosted.org/packages/95/01/268f96d75c690854cfb04b375566503c782c8caca053294d6342d2e76c9e/contextfun-0.6.0.tar.gz" } ], "0.6.1": [ { "comment_text": "", "digests": { "md5": "94cc4449212ef58327b788fa9de3623d", "sha256": "3fadc59abcfc7c108e1ec9cf2765159586562312f29834a210e4fb948ee1165b" }, "downloads": -1, "filename": "contextfun-0.6.1-py3-none-any.whl", "has_sig": false, "md5_digest": "94cc4449212ef58327b788fa9de3623d", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 5156, "upload_time": "2019-03-02T16:14:43", "url": "https://files.pythonhosted.org/packages/72/49/25ef644784c81cc9c5715064becaa962c4c4a7d6c0d4cf8bcddb64972fcc/contextfun-0.6.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "95caa44996e3022c2b0dd9dad0e2d48e", "sha256": "987487506f9d918fc1c60776f5275af195379a2d6b4f0f243fb3648b4089b0e6" }, "downloads": -1, "filename": "contextfun-0.6.1.tar.gz", "has_sig": false, "md5_digest": "95caa44996e3022c2b0dd9dad0e2d48e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15829, "upload_time": "2019-03-02T16:14:44", "url": "https://files.pythonhosted.org/packages/53/c7/265610f08c53bd58a8e3cc6a8b8e6edec507c8cb83d8253fd02dfdaf9797/contextfun-0.6.1.tar.gz" } ], "0.7.0": [ { "comment_text": "", "digests": { "md5": "7a8105d06f37c89d67137e4c7694d216", "sha256": "e87362d849de51ed6b7653491bd1e19e1c88fe1ba46a24565235dcaa9e62034a" }, "downloads": -1, "filename": "contextfun-0.7.0-py3-none-any.whl", "has_sig": false, "md5_digest": "7a8105d06f37c89d67137e4c7694d216", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 5276, "upload_time": "2019-03-16T15:22:36", "url": "https://files.pythonhosted.org/packages/f5/cd/2e1236bc3233f9d1148abba60c4a4057ff049a7ac9160dd6409fdf7fdba1/contextfun-0.7.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "54ef8bb25bd666f152b4f25fd7127c87", "sha256": "b3ef455362fb567b977bc976bb7a22f28b2c34736fa6f4c005c31c736c2ac132" }, "downloads": -1, "filename": "contextfun-0.7.0.tar.gz", "has_sig": false, "md5_digest": "54ef8bb25bd666f152b4f25fd7127c87", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16227, "upload_time": "2019-03-16T15:22:38", "url": "https://files.pythonhosted.org/packages/5f/a3/ac13d99b85ec3dcda15a4c967bd66c1ae4d85bf90c651fd025dfc279680b/contextfun-0.7.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "7a8105d06f37c89d67137e4c7694d216", "sha256": "e87362d849de51ed6b7653491bd1e19e1c88fe1ba46a24565235dcaa9e62034a" }, "downloads": -1, "filename": "contextfun-0.7.0-py3-none-any.whl", "has_sig": false, "md5_digest": "7a8105d06f37c89d67137e4c7694d216", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 5276, "upload_time": "2019-03-16T15:22:36", "url": "https://files.pythonhosted.org/packages/f5/cd/2e1236bc3233f9d1148abba60c4a4057ff049a7ac9160dd6409fdf7fdba1/contextfun-0.7.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "54ef8bb25bd666f152b4f25fd7127c87", "sha256": "b3ef455362fb567b977bc976bb7a22f28b2c34736fa6f4c005c31c736c2ac132" }, "downloads": -1, "filename": "contextfun-0.7.0.tar.gz", "has_sig": false, "md5_digest": "54ef8bb25bd666f152b4f25fd7127c87", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16227, "upload_time": "2019-03-16T15:22:38", "url": "https://files.pythonhosted.org/packages/5f/a3/ac13d99b85ec3dcda15a4c967bd66c1ae4d85bf90c651fd025dfc279680b/contextfun-0.7.0.tar.gz" } ] }