Skip to main content

A collection of python decorators I wish existed before

Project description

utde

A collection of utility decorators to simplify my life.

Persist

Instead of recomputing an expensive function annotate it with a generic_persist decorator. This decorator allows you to specify:

- key_or_fn: Either a string or a function
that generates a string from the wrapped_fn args
- load_fn: A function that is used to retrieve
stored data from key
- store_fn: A function that is used to store
the results of the "expensive" function call so that
it can be loaded next time instead

Example:

from utde.persist import generic_persist

cache = dict()

def key_fn(day_str):
    year, month, day = day_str.split("-")
    return f"{year}/{month}/{day}"

def load_fn(key):
    if key in cache:
        return cache[key]

def store_fn(x, key):
    cache[key] = x

@generic_persist(key_fn, load_fn, store_fn)
def wrapped_fn(x, day_str):
    print("Imagine an expensive operation")
    return x * 2

Pandas (optional)

pip install utde[pandas]

There is a overloaded version for pandas that will load/store using the pickle format. Then you only have to provide where to load/store the file from/to.

WARNING: Only provide a key to a location you trust in, as unpickling a pickle file may execute arbitrary code.

import pandas as pd
from utde.persist import persist_pd

@persist_pd(lambda year: f"yearly_report_{year}.pkl")
def yearly_report(year: str) -> pd.DataFrame:
    return pd.DataFrame(data={"year": [year], "profit": [42]})

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

utde-0.1.2.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.

utde-0.1.2-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file utde-0.1.2.tar.gz.

File metadata

  • Download URL: utde-0.1.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for utde-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5b61294af9df13a58ef4cc739ae931d3e36b2c099c27a339a7edbe1ec041eb15
MD5 8c9fcae6f80d55dd310acc907b45a712
BLAKE2b-256 83a6b91e0416a748f55727243488f6ebd0af45d9b5c1616e1802897cd80dc72a

See more details on using hashes here.

File details

Details for the file utde-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: utde-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for utde-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f98ff7de9c970c2d221d3fce60a2b4a11d03fcbfcb2b866ad3c6ba460e126e1d
MD5 9de1030f2d33a81a76b2a39c006caba1
BLAKE2b-256 529d32d482936ceb58a2f51e8579fcd0315b3ff7768fe6beb03b1e39b3fee753

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