withwait


Namewithwait JSON
Version 0.1.2 PyPI version JSON
download
home_pagehttps://github.com/MaddyGuthridge/withwait
SummaryA simple utility to ensure that sleep operations always complete
upload_time2024-06-10 14:18:14
maintainerNone
docs_urlNone
authorMaddy Guthridge
requires_python<4.0,>=3.9
licenseMIT
keywords testing with sleep time wait delay
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Withwait

A simple utility to ensure that sleep operations always complete, even if an
exception happens within the with statement.

## Usage

```py
from withwait import wait

# Start a 2 second wait timer
with wait(2):
    # These operations happen while the timer is run
    print("Started timer")
    # Even if an error is raised, the timer will always be allowed to complete
    # before the withwait block is closed
    raise Exception("Yikes")

# The exception isn't actually caught so this code won't run
print("This never prints")
```

### Cancelling a timer

If you need to stop the timer during the operation, you can stop it using the
`cancel` method. Normal running will resume from the end of the `with`
statement.

```py
from withwait import wait

with wait(1) as timer:
    # Stop the timer
    timer.cancel()
    # Code after this point in the with statement won't be run

# Program flow resumes normally after
print("Ok")
```

* Different timers can be cancelled separately - if you cancel one timer, all
  other timers will run to completion.

### Aborting a timer

If you don't want to resume normal program flow after the `with` statement,
you can stop the timer using the `abort` method.

```py
from withwait import wait, WithwaitAbort

try:
    with wait(1) as timer:
        # Abort the timer
        timer.abort()
        # Code after this point in the with statement won't be run

    # And code after the with statement won't be run either
    print("Nope")

# We need to catch the exception ourselves
except WithwaitAbort:
    print("Caught")
```

* Arguments can be provided to the `abort` method which will be
  given as arguments to the exception
* Different timers can be aborted separately - if you cancel one timer, all
  other timers will run to completion.

### Aborting all timers

If you want to cancel anything, you can call the `abort_all` method. You can
also raise a `WithwaitAbortAll` exception, which will have the same effect.

```py
from withwait import wait, WithwaitAbortAll

try:
    with wait(1):
        with wait(1):
            with wait(1) as timer:
                # Abort all timers
                timer.abort_all()
except WithwaitAbortAll:
    print("All timers stopped")
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/MaddyGuthridge/withwait",
    "name": "withwait",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.9",
    "maintainer_email": null,
    "keywords": "testing, with, sleep, time, wait, delay",
    "author": "Maddy Guthridge",
    "author_email": "hello@maddyguthridge.com",
    "download_url": "https://files.pythonhosted.org/packages/db/57/e62964d881374132d778465c165f30456e5241edfb1523a9106e839cd0c1/withwait-0.1.2.tar.gz",
    "platform": null,
    "description": "# Withwait\n\nA simple utility to ensure that sleep operations always complete, even if an\nexception happens within the with statement.\n\n## Usage\n\n```py\nfrom withwait import wait\n\n# Start a 2 second wait timer\nwith wait(2):\n    # These operations happen while the timer is run\n    print(\"Started timer\")\n    # Even if an error is raised, the timer will always be allowed to complete\n    # before the withwait block is closed\n    raise Exception(\"Yikes\")\n\n# The exception isn't actually caught so this code won't run\nprint(\"This never prints\")\n```\n\n### Cancelling a timer\n\nIf you need to stop the timer during the operation, you can stop it using the\n`cancel` method. Normal running will resume from the end of the `with`\nstatement.\n\n```py\nfrom withwait import wait\n\nwith wait(1) as timer:\n    # Stop the timer\n    timer.cancel()\n    # Code after this point in the with statement won't be run\n\n# Program flow resumes normally after\nprint(\"Ok\")\n```\n\n* Different timers can be cancelled separately - if you cancel one timer, all\n  other timers will run to completion.\n\n### Aborting a timer\n\nIf you don't want to resume normal program flow after the `with` statement,\nyou can stop the timer using the `abort` method.\n\n```py\nfrom withwait import wait, WithwaitAbort\n\ntry:\n    with wait(1) as timer:\n        # Abort the timer\n        timer.abort()\n        # Code after this point in the with statement won't be run\n\n    # And code after the with statement won't be run either\n    print(\"Nope\")\n\n# We need to catch the exception ourselves\nexcept WithwaitAbort:\n    print(\"Caught\")\n```\n\n* Arguments can be provided to the `abort` method which will be\n  given as arguments to the exception\n* Different timers can be aborted separately - if you cancel one timer, all\n  other timers will run to completion.\n\n### Aborting all timers\n\nIf you want to cancel anything, you can call the `abort_all` method. You can\nalso raise a `WithwaitAbortAll` exception, which will have the same effect.\n\n```py\nfrom withwait import wait, WithwaitAbortAll\n\ntry:\n    with wait(1):\n        with wait(1):\n            with wait(1) as timer:\n                # Abort all timers\n                timer.abort_all()\nexcept WithwaitAbortAll:\n    print(\"All timers stopped\")\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A simple utility to ensure that sleep operations always complete",
    "version": "0.1.2",
    "project_urls": {
        "Bug Tracker": "https://github.com/MaddyGuthridge/withwait/issues",
        "Documentation": "https://maddyguthridge.github.io/withwait",
        "Homepage": "https://github.com/MaddyGuthridge/withwait",
        "Online Documentation": "https://github.com/MaddyGuthridge/withwait",
        "Repository": "https://github.com/MaddyGuthridge/withwait"
    },
    "split_keywords": [
        "testing",
        " with",
        " sleep",
        " time",
        " wait",
        " delay"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "385e754298fcfd37acdc364d0ac7b5bbbd25f24968189364b74d5559d3d3c9e4",
                "md5": "b4e0538ad155d8a8d1232a64e981cabc",
                "sha256": "82dac56f0f9d33ae7e3f1289640820c65d6364b6b644f8f8ec78319f558843e1"
            },
            "downloads": -1,
            "filename": "withwait-0.1.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b4e0538ad155d8a8d1232a64e981cabc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.9",
            "size": 4777,
            "upload_time": "2024-06-10T14:18:12",
            "upload_time_iso_8601": "2024-06-10T14:18:12.306399Z",
            "url": "https://files.pythonhosted.org/packages/38/5e/754298fcfd37acdc364d0ac7b5bbbd25f24968189364b74d5559d3d3c9e4/withwait-0.1.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "db57e62964d881374132d778465c165f30456e5241edfb1523a9106e839cd0c1",
                "md5": "31de9e42f782d74cece0f27709e41f73",
                "sha256": "e416faf3624294ce8e594239c600d5525044778f040b1251e99fd91f490452f4"
            },
            "downloads": -1,
            "filename": "withwait-0.1.2.tar.gz",
            "has_sig": false,
            "md5_digest": "31de9e42f782d74cece0f27709e41f73",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.9",
            "size": 3913,
            "upload_time": "2024-06-10T14:18:14",
            "upload_time_iso_8601": "2024-06-10T14:18:14.572660Z",
            "url": "https://files.pythonhosted.org/packages/db/57/e62964d881374132d778465c165f30456e5241edfb1523a9106e839cd0c1/withwait-0.1.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-10 14:18:14",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "MaddyGuthridge",
    "github_project": "withwait",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "withwait"
}
        
Elapsed time: 0.73985s