Metadata-Version: 2.4
Name: decore
Version: 0.0.4
Summary: A small pure-python package for utility decorators.
Author-email: Shay Palachy Affek <shay.palachy@gmail.com>
License: MIT License
        
        Copyright (c) 2017 Shay Palachy
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Source, https://github.com/shaypal5/decore
Keywords: decorator,decorators
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Other/Nonlisted Topic
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Utilities
Description-Content-Type: text/x-rst
License-File: LICENSE
Dynamic: license-file

decore
#########
|PyPI-Status| |PyPI-Versions| |Build-Status| |Codecov| |LICENCE|

A small pure-python package for utility decorators.

.. code-block:: python

  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)

.. contents::

.. section-numbering::


Installation
============

Install ``decore`` with:

.. code-block:: bash

  pip install decore


Decorators
==========

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 triggering the calculation.

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

.. code-block:: python

  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)


threadsafe_generator
--------------------

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

.. code-block:: python

  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__()


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.

Installing for development
--------------------------

Clone:

.. code-block:: bash

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


Install in development mode with test dependencies:

.. code-block:: bash

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


Running the tests
-----------------

To run the tests, use:

.. code-block:: bash

  python -m pytest --cov=decore


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`_.

.. _`numpy docstring conventions`: https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt
.. _`these conventions`: https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt


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

.. |PyPI-Status| image:: https://img.shields.io/pypi/v/decore.svg
  :target: https://pypi.python.org/pypi/decore

.. |PyPI-Versions| image:: https://img.shields.io/pypi/pyversions/decore.svg
   :target: https://pypi.python.org/pypi/decore

.. |Build-Status| image:: https://travis-ci.org/shaypal5/decore.svg?branch=master
  :target: https://travis-ci.org/shaypal5/decore

.. |LICENCE| image:: https://img.shields.io/pypi/l/decore.svg
  :target: https://pypi.python.org/pypi/decore

.. |Codecov| image:: https://codecov.io/github/shaypal5/decore/coverage.svg?branch=master
   :target: https://codecov.io/github/shaypal5/decore?branch=master
