Skip to main content

Ensure a function runs only once, regardless of how many times it's called

Project description

philiprehberger-once

Tests PyPI version Last updated

Ensure a function runs only once, regardless of how many times it's called.

Installation

pip install philiprehberger-once

Usage

from philiprehberger_once import once

@once
def load_config():
    print("Loading...")
    return {"debug": True}

load_config()  # prints "Loading...", returns {"debug": True}
load_config()  # returns {"debug": True} without printing

Async support

import asyncio
from philiprehberger_once import once

@once
async def fetch_token():
    print("Fetching...")
    return "abc-123"

asyncio.run(fetch_token())  # prints "Fetching...", returns "abc-123"
asyncio.run(fetch_token())  # returns "abc-123" without fetching

Once per key

from philiprehberger_once import once_per_key

@once_per_key
def connect(host, port=5432):
    print(f"Connecting to {host}...")
    return f"conn:{host}"

connect("db-1")  # prints "Connecting to db-1...", returns "conn:db-1"
connect("db-1")  # returns cached "conn:db-1"
connect("db-2")  # prints "Connecting to db-2...", returns "conn:db-2"

Reset and inspect

from philiprehberger_once import once

@once
def init():
    return 42

init()
init.called   # True
init.reset()
init.called   # False
init()        # runs again

API

Function / Property Description
once(fn) Decorator. Runs fn once, caches and returns the result on subsequent calls. Thread-safe. Supports async.
once_per_key(fn) Decorator. Runs fn once per unique first argument. Thread-safe.
.called bool for once, dict[key, bool] for once_per_key. Whether the function has been called.
.reset() Clear cached result so the function can run again. once_per_key accepts an optional key argument.

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_once-0.1.7.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_once-0.1.7-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_once-0.1.7.tar.gz.

File metadata

  • Download URL: philiprehberger_once-0.1.7.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_once-0.1.7.tar.gz
Algorithm Hash digest
SHA256 b580140c7f07785d748230edb779de465d42ecde8a1279669aaa8c4e5fcc8002
MD5 a4c94b72bd1efcdf620a469f9e3d2d42
BLAKE2b-256 ee6c6875b4bfcd5d2cb02e98e32312dfd4f6861c5b1d21c19e21a8e90ce5b67c

See more details on using hashes here.

File details

Details for the file philiprehberger_once-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_once-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b97a40b16d1d868e6cce9f67bfe80a5f708189013af02fb284637b679640c576
MD5 df0846c1a9a6b51fca7ca9060fca2e85
BLAKE2b-256 05343be6cc6fdfb12e6449ffc3a77bddc416b2daa7fcff44519243173f94f9fb

See more details on using hashes here.

Supported by

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