Skip to main content

No project description provided

Project description

malib

A few utilities that I find useful.

RateLimiter

from malib import RateLimiter

# call a function at most 10 times per minute
rl = RateLimiter(max_calls=10, period=60) 
# call .wait_and_call() every time before calling the function
rl.wait_and_call()

ttl_cache

from malib import ttl_cache
from time import sleep


@ttl_cache(ttl=1)
def f():
    print("computing")
    return "result"


print(f()) # prints "computing"
print(f()) # cached
sleep(1)
print(f()) # prints "computing"

Exact cover

Code inspired by this blog post.

from malib import exact_cover

piece_to_constraints = {"A": {1}, "B": {2, 4}, "C": {2, 3, 5}, "D": {3, 5}}
next(exact_cover(piece_to_constraints))
# ("A", "B", "D")

PyTorch bivariate normal cdf

Provide two functions to compute a differentiable cumulative distribution function of a bivariate normal distribution.

Requires scipy and pytorch.

import torch
from malib import standard_bivariate_normal_cdf, bivariate_normal_cdf

# standard bivariate normal cdf
x = torch.tensor([0.0, 0.0], requires_grad=True)
cor = 0.5
y = standard_bivariate_normal_cdf(x, cor)
print(y)
# tensor(0.3333, grad_fn=<StandardBivariateNormalCDFBackward>)
y.backward()
print(x.grad)
# tensor([0.1995, 0.1995])

# bivariate_normal_cdf
x = torch.tensor([0.0, 0.0], requires_grad=True)
mean = torch.tensor([0.0, 0.0])
cov = torch.tensor([[1.0, 0.5], [0.5, 1.0]])
y = bivariate_normal_cdf(x, mean, cov)
print(y)
# tensor(0.3333, grad_fn=<BivariateNormalCDFBackward>)
y.backward()
print(x.grad)
# tensor([0.1995, 0.1995])

PyTorch interpolation

import torch
from malib import interp

x = torch.tensor([0.0, 1.0, 2.0])
y = torch.tensor([0.0, 1.0, 4.0])
interp(torch.tensor([0.5, 1.5]), x, y)
# tensor([0.5000, 2.5000])

Async to sync generator

The sync_gen function allows you to convert an asynchronous generator into a synchronous one. This can be useful when you want to use async code in a synchronous context.

import asyncio
from malib import sync_gen

async def async_generator():
    for i in range(5):
        await asyncio.sleep(0.1)
        yield i

# Convert async generator to sync generator
sync_generator = sync_gen(async_generator())

# Use the sync generator in a regular for loop
for item in sync_generator:
    print(item)

Testing

poetry install --with dev
pytest

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

malib-0.9.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

malib-0.9.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file malib-0.9.0.tar.gz.

File metadata

  • Download URL: malib-0.9.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.5

File hashes

Hashes for malib-0.9.0.tar.gz
Algorithm Hash digest
SHA256 957ba5584685c0ad7b43d4c7733fd340ba010922ea951d4d4b95aef118a2d868
MD5 4c783615a81d6a504b4af606658e5da1
BLAKE2b-256 fe51ae5a8ec12c124a876cd6697b3b9ea91f97fafcc13af19f3fa0183e67163e

See more details on using hashes here.

File details

Details for the file malib-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: malib-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.5

File hashes

Hashes for malib-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3b88b4ded956fc784a9781fb6389cdbc539c79eb5e0c5fda6039097d901741a
MD5 a6c7509cbf9c913c58c925eb4d91f285
BLAKE2b-256 eafbc185bdaf5718fd30e56b2a3a45fd018742ea2d815eacf9fd2939069385f3

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