# [pyonce](https://pypi.org/project/pyonce/)
Run code once! -- or once in a while...
```py
def once(every: int = 0, key: Any = None) -> bool
```
### Example
```py
import pyonce
def update():
if pyonce.once(key='ONCE-001'):
print('global unique key')
if pyonce.once():
print('this is called from update')
if pyonce.once(every=5):
print('update() / 5')
for i in range(10):
if pyonce.once(every=2):
print(f'hello {i}')
update()
update()
```
**output**
```
hello 0
global unique key
this is called from update
update() / 5
this is called from update
update() / 5
hello 2
hello 4
update() / 5
update() / 5
hello 6
hello 8
```
The `once()` function relies on the traceback to derive the key.
A unique key corresponds to a unique state which is a simple counter.
When the counter hits zero the return value is `True`.
The first call for a key is always `True`.
---
Do not use it in production, such magic is pure evil.
Raw data
{
"_id": null,
"home_page": "https://github.com/szabolcsdombi/pyonce/",
"name": "pyonce",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "once",
"author": "Szabolcs Dombi",
"author_email": "cprogrammer1994@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/c5/c6/0472665661ec773cc7c672bd8b64426aa902d8e781c2b97e7e340a7a7eea/pyonce-1.0.0.tar.gz",
"platform": "any",
"description": "# [pyonce](https://pypi.org/project/pyonce/)\r\n\r\nRun code once! -- or once in a while...\r\n\r\n```py\r\ndef once(every: int = 0, key: Any = None) -> bool\r\n```\r\n\r\n### Example\r\n\r\n```py\r\nimport pyonce\r\n\r\n\r\ndef update():\r\n if pyonce.once(key='ONCE-001'):\r\n print('global unique key')\r\n if pyonce.once():\r\n print('this is called from update')\r\n if pyonce.once(every=5):\r\n print('update() / 5')\r\n\r\n\r\nfor i in range(10):\r\n if pyonce.once(every=2):\r\n print(f'hello {i}')\r\n update()\r\n update()\r\n```\r\n\r\n**output**\r\n\r\n```\r\nhello 0\r\nglobal unique key\r\nthis is called from update\r\nupdate() / 5\r\nthis is called from update\r\nupdate() / 5\r\nhello 2\r\nhello 4\r\nupdate() / 5\r\nupdate() / 5\r\nhello 6\r\nhello 8\r\n```\r\n\r\nThe `once()` function relies on the traceback to derive the key.\r\nA unique key corresponds to a unique state which is a simple counter.\r\nWhen the counter hits zero the return value is `True`.\r\nThe first call for a key is always `True`.\r\n\r\n---\r\n\r\nDo not use it in production, such magic is pure evil.\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Run code once!",
"version": "1.0.0",
"split_keywords": [
"once"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "c184670382f17de61dc55a2c3eefd77a",
"sha256": "274eab294bdf206aeca2db810dde9651c39bab9e0ce603960e4b6b7ed45fc009"
},
"downloads": -1,
"filename": "pyonce-1.0.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "c184670382f17de61dc55a2c3eefd77a",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 2907,
"upload_time": "2022-12-04T18:17:00",
"upload_time_iso_8601": "2022-12-04T18:17:00.306592Z",
"url": "https://files.pythonhosted.org/packages/e1/83/4319452be6f91cb36e9c47d22535a14dae8675eea9e637e892b987b192b0/pyonce-1.0.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "a96157e45391d3a84d58f13266625932",
"sha256": "b9e311f28a7d274547ca986b034d75eebc5d269a23a946fe92200c3018562ceb"
},
"downloads": -1,
"filename": "pyonce-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "a96157e45391d3a84d58f13266625932",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 2656,
"upload_time": "2022-12-04T18:17:02",
"upload_time_iso_8601": "2022-12-04T18:17:02.350660Z",
"url": "https://files.pythonhosted.org/packages/c5/c6/0472665661ec773cc7c672bd8b64426aa902d8e781c2b97e7e340a7a7eea/pyonce-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-04 18:17:02",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "szabolcsdombi",
"github_project": "pyonce",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pyonce"
}