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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

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