Skip to main content

Run code once!

Project description

pyonce

Run code once! -- or once in a while...

def once(every: int = 0, key: Any = None) -> bool

Example

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyonce-1.0.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

pyonce-1.0.0-py2.py3-none-any.whl (2.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pyonce-1.0.0.tar.gz.

File metadata

  • Download URL: pyonce-1.0.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for pyonce-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b9e311f28a7d274547ca986b034d75eebc5d269a23a946fe92200c3018562ceb
MD5 a96157e45391d3a84d58f13266625932
BLAKE2b-256 c5c60472665661ec773cc7c672bd8b64426aa902d8e781c2b97e7e340a7a7eea

See more details on using hashes here.

File details

Details for the file pyonce-1.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: pyonce-1.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 2.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for pyonce-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 274eab294bdf206aeca2db810dde9651c39bab9e0ce603960e4b6b7ed45fc009
MD5 c184670382f17de61dc55a2c3eefd77a
BLAKE2b-256 e1834319452be6f91cb36e9c47d22535a14dae8675eea9e637e892b987b192b0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page