Skip to main content

Snowball

Library with several utilities for merging "n+1" amount function calls into one.

This utilities might be helpful for tasks/functions that should run not very frequently, but there could be a lot of initiating calls to that functions.

Installation

pip install px-snowball

Usage

Simple usage:

from px_snowball import thread_throttle, thread_debounce
# Or shortcuts:
# from px_snowball import throttle, debounce

context = {}

# We're delaying function execution after each call:
@thread_debounce(0.2)
def debouncer(values):
  # `values` will be a list of all arguments and key arguments it was called.
  for args, kwargs in values:
    # do some stuff
    print(args)

debouncer(1)
debouncer(2)
debouncer(3)
time.sleep(0.1)
debouncer(4)
time.sleep(0.2)

# And only now `debouncer` function will be called.
# The same is for throttle function.

More practical example.

Let's assume we need to write messages to file. But system calls for every message - is too expensive:

from px_snowball import thread_throttle, thread_debounce

# At most every 1 second messages will be written to log file:
@thread_throttle(1)
def log(values: List[Tuple[Tuple[str], dict]):
  messages = (message for (message, *args), kwargs in values)
  data_to_write = '\n'.join(messages)

  with open('/tmp/log.log', 'a+') as f:
    f.write(data_to_write)


log('First')
log('Second')
# Will be empty:
# > cat /tmp/log.log
time.sleep(0.5)
log('Third')
# Still empty:
# > cat /tmp/log.log

time.sleep(0.5)
# Now, after 1 second has passed all logs are in file.
# Write operation was executed only once, instead of 3 times as would be if func
# was executed as usual.
# > cat /tmp/log.log
# > First
# > Second
# > Third

Yes, the resulting function is a bit ugly, but the main profit is that we could perform our operations once in "bulk"(with possible optimizations) even if we call functions 10k times.

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[Unreleased]

[0.1.0]

Initial version.

Metadata

Release files for px-snowball 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for px-snowball 0.1.0
File Size Uploaded Provenance
px-snowball-0.1.0.tar.gz 5.5 kB Without provenance Details

Release files / px-snowball-0.1.0.tar.gz

Download URL px-snowball-0.1.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
175f3f9cc8b466712199afa384923405f028d8b808a9c9fb0d2b569ede3679c9
BLAKE2b-256 checksum
How to use checksums
75380645eb064e4fc7909e147c8a9578dead6c42e9fb7fccb0779c32ab80f118
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.12

Security for px-snowball 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page