cofunctions


Namecofunctions JSON
Version 2023.2.1 PyPI version JSON
download
home_page
Summarycofunction: simple coroutines
upload_time2023-03-20 17:18:20
maintainer
docs_urlNone
author
requires_python>=3.6
license
keywords generators coroutines
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            cofunctions: Simple coroutines for Python
=========================================

To create a cofunction, write a generator with the ``@cofunction`` decorator
and ``yield`` a function that does your processing.

Example of a function that accumulates a sum:

```py
from cofunctions import cofunction
from math import exp, log

@cofunction
def logsumexp(initial=0.):
    '''log-sum-exp example (does not actually prevent overflow)'''
    sum = initial

    # this function gathers the inputs
    def _(x):
        nonlocal sum  # important for changing values
        sum += exp(x)
        return sum

    yield _

    # do additional processing here

    result = log(sum)

    return result

# create the cofunction
lse = logsumexp()

# call the cofunction to do processing
partial1 = lse(1)
partial2 = lse(2)
partial3 = lse(3)

# finish the cofunction to run the additional processing
result = lse.finish()

print(partial1, partial2, partial3)
# 2.718281828459045 10.107337927389695 30.19287485057736
print(result)
# 3.40760596444438
```


            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "cofunctions",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "generators,coroutines",
    "author": "",
    "author_email": "Nicolas Tessore <n.tessore@ucl.ac.uk>",
    "download_url": "https://files.pythonhosted.org/packages/3b/da/169cd23fc10381b10d2694834d5f29d02b9e98bb7bd630e4cf34a95a6ade/cofunctions-2023.2.1.tar.gz",
    "platform": null,
    "description": "cofunctions: Simple coroutines for Python\n=========================================\n\nTo create a cofunction, write a generator with the ``@cofunction`` decorator\nand ``yield`` a function that does your processing.\n\nExample of a function that accumulates a sum:\n\n```py\nfrom cofunctions import cofunction\nfrom math import exp, log\n\n@cofunction\ndef logsumexp(initial=0.):\n    '''log-sum-exp example (does not actually prevent overflow)'''\n    sum = initial\n\n    # this function gathers the inputs\n    def _(x):\n        nonlocal sum  # important for changing values\n        sum += exp(x)\n        return sum\n\n    yield _\n\n    # do additional processing here\n\n    result = log(sum)\n\n    return result\n\n# create the cofunction\nlse = logsumexp()\n\n# call the cofunction to do processing\npartial1 = lse(1)\npartial2 = lse(2)\npartial3 = lse(3)\n\n# finish the cofunction to run the additional processing\nresult = lse.finish()\n\nprint(partial1, partial2, partial3)\n# 2.718281828459045 10.107337927389695 30.19287485057736\nprint(result)\n# 3.40760596444438\n```\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "cofunction: simple coroutines",
    "version": "2023.2.1",
    "split_keywords": [
        "generators",
        "coroutines"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "179ed58efb34d23d7d78b68171ff6ec848f79af1830bede6e8818c40f1596ad5",
                "md5": "2e9d0d1a717b8bac196b48b9fe98f3d5",
                "sha256": "3337fc55b9b8a5bbe35495289d72cf11a27afa0680d7adade6c5f895fc6ef720"
            },
            "downloads": -1,
            "filename": "cofunctions-2023.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2e9d0d1a717b8bac196b48b9fe98f3d5",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 2785,
            "upload_time": "2023-03-20T17:18:18",
            "upload_time_iso_8601": "2023-03-20T17:18:18.872146Z",
            "url": "https://files.pythonhosted.org/packages/17/9e/d58efb34d23d7d78b68171ff6ec848f79af1830bede6e8818c40f1596ad5/cofunctions-2023.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3bda169cd23fc10381b10d2694834d5f29d02b9e98bb7bd630e4cf34a95a6ade",
                "md5": "10fff933f6c57d787d37863fe0fdcc60",
                "sha256": "b55e6d404a089d33bd28f783dfa3b0f53335b9960b5454f4d5c24ac74cec55db"
            },
            "downloads": -1,
            "filename": "cofunctions-2023.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "10fff933f6c57d787d37863fe0fdcc60",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 2228,
            "upload_time": "2023-03-20T17:18:20",
            "upload_time_iso_8601": "2023-03-20T17:18:20.557280Z",
            "url": "https://files.pythonhosted.org/packages/3b/da/169cd23fc10381b10d2694834d5f29d02b9e98bb7bd630e4cf34a95a6ade/cofunctions-2023.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-20 17:18:20",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "cofunctions"
}
        
Elapsed time: 0.04641s