Skip to main content

A small pure-python package for utility decorators.

Project description

PyPI-Status PyPI-Versions Build-Status Codecov LICENCE

A small pure-python package for utility decorators.

from decore import lazy_property

@lazy_property
def paramless_big_calc():
  sub_res = [big_func(const) for const in array_of_constants]
  return sum(sub_res)

1   Installation

Install decore with:

pip install decore

2   Decorators

2.1   lazy_property

The lazy_property decorator is meant to decorate functions that compute some constant value or property that you only want to compute once. The first call to the decorated function will run it and save the value (in memory) before returning it; subsequent calls will get this value without trigerring the calculation.

You can think about it like a functools.lru_cache(maxsize=1) for functions with no parameters.

from decore import lazy_property

@lazy_property
def paramless_big_calc():
  """I take a lot of time!"""
  sub_res = [big_func(const) for const in array_of_constants]
  return sum(sub_res)

2.2   threadsafe_generator

The threadsafe_generator decorator makes generators threadsafe. This means multiple threads can be given references to the decorated generator and it is guarenteed that each item in the stream will be yielded (i.e. returned) to only a single thread.

from decore import threadsafe_generator

@threadsafe_generator
def user_documents(day):
  """I yield some MongoDB documents!"""
  client = get_mongodb_client(some_params)
  dt_obj = translate_day_to_dt(day)
  user_document_cursor = client.some_mongodb_query(dt_obj, SOME_CONST)
  while True:
    yield user_document_cursor.__next__()

3   Contributing

Package author and current maintainer is Shay Palachy (shay.palachy@gmail.com); You are more than welcome to approach him for help. Contributions are very welcomed.

3.1   Installing for development

Clone:

git clone git@github.com:shaypal5/decore.git

Install in development mode with test dependencies:

cd pdpipe
pip install -e ".[test]"

3.2   Running the tests

To run the tests, use:

python -m pytest --cov=decore

3.3   Adding documentation

This project is documented using the numpy docstring conventions, which were chosen as they are perhaps the most widely-spread conventions that are both supported by common tools such as Sphinx and result in human-readable docstrings (in my personal opinion, of course). When documenting code you add to this project, please follow these conventions.

4   Credits

Created by Shay Palachy (shay.palachy@gmail.com).

Project details


Release history Release notifications

This version
History Node

0.0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
decore-0.0.1.tar.gz (19.7 kB) Copy SHA256 hash SHA256 Source None Oct 2, 2017

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page