<commit_before>===========
Wraptor
===========

.. image:: https://github.com/carlsverre/wraptor/raw/master/docs/images/raptor.jpg

Testing
=======

.. image:: https://travis-ci.org/carlsverre/wraptor.png
    :target: https://travis-ci.org/carlsverre/wraptor

Run tests by executing :code:`python setup.py test`.

Decorators
==========

Memoize
-------
Add a cache to a function such that multiple calls with the same args
will return cached results.  Supports an optional cache timeout which
will flush items from the cache after a set interval for
recomputation.

.. code:: python

    from wraptor.decorators import memoize

    @memoize()
    def foo(bar, baz):
        print(bar, baz)

    foo(1, 2)
    # prints (1, 2)
    foo(3, 4)
    # prints (3, 4)
    foo(1, 2)
    # no-op

Supports timeouts!

.. code:: python

    @memoize(timeout=.5)
    def foo(bar, baz):
        print(bar, baz)

    foo(1, 2)
    # prints (1, 2)
    foo(1, 2)
    # no-op

    import time
    time.sleep(2)

    foo(1, 2)
    # prints (1, 2)

Supports attaching to an instance method!

.. code:: python

    class foo(object):
        @memoize(instance_method=True)
        def bar(self, a, b):
            return random()

    f = foo()
    f2 = foo()

    # they don't share a cache!
    f.bar(1,2) != f2.bar(1,2)

Throttle
--------
Throttle a function to firing at most 1 time per interval.  The function
is fired on the forward edge (meaning it will fire the first time you
call it).

.. code:: python

    from wraptor.decorators import throttle
    import time

    @throttle(.5)
    def foo(bar, baz):
        print(bar, baz)

    foo(1, 2)
    # prints (1, 2)
    foo(3, 4)
    # no-op
    time.sleep(1)
    foo(5, 6)
    # prints (1, 2)
<commit_msg>Update README.rst<commit_after>===========
Wraptor
===========

.. image:: https://github.com/carlsverre/wraptor/raw/master/docs/images/raptor.jpg

Testing
=======

.. image:: https://travis-ci.org/carlsverre/wraptor.png
    :target: https://travis-ci.org/carlsverre/wraptor

Run tests by executing :code:`python setup.py test`.

Decorators
==========

Memoize
-------
Add a cache to a function such that multiple calls with the same args
will return cached results.  Supports an optional cache timeout which
will flush items from the cache after a set interval for
recomputation.

.. code:: python

    from wraptor.decorators import memoize

    @memoize()
    def foo(bar, baz):
        print(bar, baz)

    foo(1, 2)
    # prints (1, 2)
    foo(3, 4)
    # prints (3, 4)
    foo(1, 2)
    # no-op

Supports timeouts!

.. code:: python

    @memoize(timeout=.5)
    def foo(bar, baz):
        print(bar, baz)

    foo(1, 2)
    # prints (1, 2)
    foo(1, 2)
    # no-op

    import time
    time.sleep(2)

    foo(1, 2)
    # prints (1, 2)

Supports attaching to an instance method!

.. code:: python

    class foo(object):
        @memoize(instance_method=True)
        def bar(self, a, b):
            return random()

    f = foo()
    f2 = foo()

    # they don't share a cache!
    f.bar(1,2) != f2.bar(1,2)

Throttle
--------
Throttle a function to firing at most 1 time per interval.  The function
is fired on the forward edge (meaning it will fire the first time you
call it).

.. code:: python

    from wraptor.decorators import throttle
    import time

    @throttle(.5)
    def foo(bar, baz):
        print(bar, baz)

    foo(1, 2)
    # prints (1, 2)
    foo(3, 4)
    # no-op
    time.sleep(1)
    foo(5, 6)
    # prints (1, 2)
    
Supports attaching to an instance method!

.. code:: python

    arr = []

    class foo(object):
        @throttle(1, instance_method=True)
        def bar(self):
            arr.append(1)

    x = foo()
    x2 = foo()

    x.bar()
    x2.bar()
    
    # they don't share the same throttle!
    assert arr == [1, 1]


{'fraction_non_alphanumeric': 0.14499213424226534, 'fraction_numerical': 0.023072889355007866, 'mean_word_length': 2.355321020228672, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2032710', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1358, 'n_words': 415}