pax_global_header00006660000000000000000000000064146756053200014522gustar00rootroot0000000000000052 comment=637ae71f06dfa5d605cacb3deb5accfe21484a99 jaraco.functools-4.1.0/000077500000000000000000000000001467560532000147765ustar00rootroot00000000000000jaraco.functools-4.1.0/.coveragerc000066400000000000000000000005451467560532000171230ustar00rootroot00000000000000[run] omit = # leading `*/` for pytest-dev/pytest-cov#456 */.tox/* disable_warnings = couldnt-parse [report] show_missing = True exclude_also = # Exclude common false positives per # https://coverage.readthedocs.io/en/latest/excluding.html#advanced-exclusion # Ref jaraco/skeleton#97 and jaraco/skeleton#135 class .*\bProtocol\): if TYPE_CHECKING: jaraco.functools-4.1.0/.editorconfig000066400000000000000000000003661467560532000174600ustar00rootroot00000000000000root = true [*] charset = utf-8 indent_style = tab indent_size = 4 insert_final_newline = true end_of_line = lf [*.py] indent_style = space max_line_length = 88 [*.{yml,yaml}] indent_style = space indent_size = 2 [*.rst] indent_style = space jaraco.functools-4.1.0/.github/000077500000000000000000000000001467560532000163365ustar00rootroot00000000000000jaraco.functools-4.1.0/.github/FUNDING.yml000066400000000000000000000000401467560532000201450ustar00rootroot00000000000000tidelift: pypi/jaraco.functools jaraco.functools-4.1.0/.github/dependabot.yml000066400000000000000000000002241467560532000211640ustar00rootroot00000000000000version: 2 updates: - package-ecosystem: "pip" directory: "/" schedule: interval: "daily" allow: - dependency-type: "all" jaraco.functools-4.1.0/.github/workflows/000077500000000000000000000000001467560532000203735ustar00rootroot00000000000000jaraco.functools-4.1.0/.github/workflows/main.yml000066400000000000000000000057611467560532000220530ustar00rootroot00000000000000name: tests on: merge_group: push: branches-ignore: # temporary GH branches relating to merge queues (jaraco/skeleton#93) - gh-readonly-queue/** tags: # required if branches-ignore is supplied (jaraco/skeleton#103) - '**' pull_request: workflow_dispatch: permissions: contents: read env: # Environment variable to support color support (jaraco/skeleton#66) FORCE_COLOR: 1 # Suppress noisy pip warnings PIP_DISABLE_PIP_VERSION_CHECK: 'true' PIP_NO_PYTHON_VERSION_WARNING: 'true' PIP_NO_WARN_SCRIPT_LOCATION: 'true' # Ensure tests can sense settings about the environment TOX_OVERRIDE: >- testenv.pass_env+=GITHUB_*,FORCE_COLOR jobs: test: strategy: # https://blog.jaraco.com/efficient-use-of-ci-resources/ matrix: python: - "3.8" - "3.13" platform: - ubuntu-latest - macos-latest - windows-latest include: - python: "3.9" platform: ubuntu-latest - python: "3.10" platform: ubuntu-latest - python: "3.11" platform: ubuntu-latest - python: "3.12" platform: ubuntu-latest - python: "3.14" platform: ubuntu-latest - python: pypy3.10 platform: ubuntu-latest runs-on: ${{ matrix.platform }} continue-on-error: ${{ matrix.python == '3.14' }} steps: - uses: actions/checkout@v4 - name: Setup Python uses: actions/setup-python@v4 with: python-version: ${{ matrix.python }} allow-prereleases: true - name: Install tox run: python -m pip install tox - name: Run run: tox collateral: strategy: fail-fast: false matrix: job: - diffcov - docs runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - name: Setup Python uses: actions/setup-python@v4 with: python-version: 3.x - name: Install tox run: python -m pip install tox - name: Eval ${{ matrix.job }} run: tox -e ${{ matrix.job }} check: # This job does nothing and is only used for the branch protection if: always() needs: - test - collateral runs-on: ubuntu-latest steps: - name: Decide whether the needed jobs succeeded or failed uses: re-actors/alls-green@release/v1 with: jobs: ${{ toJSON(needs) }} release: permissions: contents: write needs: - check if: github.event_name == 'push' && contains(github.ref, 'refs/tags/') runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup Python uses: actions/setup-python@v4 with: python-version: 3.x - name: Install tox run: python -m pip install tox - name: Run run: tox -e release env: TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} jaraco.functools-4.1.0/.pre-commit-config.yaml000066400000000000000000000002261467560532000212570ustar00rootroot00000000000000repos: - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.5.6 hooks: - id: ruff args: [--fix, --unsafe-fixes] - id: ruff-format jaraco.functools-4.1.0/.readthedocs.yaml000066400000000000000000000005161467560532000202270ustar00rootroot00000000000000version: 2 python: install: - path: . extra_requirements: - doc # required boilerplate readthedocs/readthedocs.org#10401 build: os: ubuntu-lts-latest tools: python: latest # post-checkout job to ensure the clone isn't shallow jaraco/skeleton#114 jobs: post_checkout: - git fetch --unshallow || true jaraco.functools-4.1.0/LICENSE000066400000000000000000000017771467560532000160170ustar00rootroot00000000000000Permission 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. jaraco.functools-4.1.0/NEWS.rst000066400000000000000000000066661467560532000163220ustar00rootroot00000000000000v4.1.0 ====== Features -------- - Added chainable decorator. v4.0.2 ====== No significant changes. v4.0.1 ====== No significant changes. v4.0.0 ====== Features -------- - Added ``splat`` function. Deprecations and Removals ------------------------- - Removed deprecated 'call_aside'. (#21) v3.9.0 ====== Features -------- - Enhanced type hints and declare the package as typed. Module is now a package. (#22) v3.8.1 ====== Bugfixes -------- - Restored type checking and repaired broken exclusion. (#50550895) v3.8.0 ====== Features -------- - Require Python 3.8 or later. v3.7.0 ====== Added ``bypass_unless`` and ``bypass_when`` and ``identity``. v3.6.0 ====== #21: Renamed ``call_aside`` to ``invoke``, deprecating ``call_aside``. v3.5.2 ====== Refreshed packaging. v3.5.1 ====== Packaging refresh. Enrolled with Tidelift. v3.5.0 ====== * #19: Add type annotations to ``method_cache``. * Require Python 3.7. v3.4.0 ====== ``apply`` now uses ``functools.wraps`` to ensure docstring passthrough. v3.3.0 ====== #18: In method_cache, support cache_clear before cache is initialized. v3.2.1 ====== Refreshed package metadata. v3.2.0 ====== Switched to PEP 420 for ``jaraco`` namespace. v3.1.0 ====== Added ``except_`` decorator. v3.0.1 ====== #14: Removed unnecessary compatibility libraries in testing. v3.0.0 ====== Require Python 3.6 or later. 2.0 === Switch to `pkgutil namespace technique `_ for the ``jaraco`` namespace. 1.20 ==== Added ``save_method_args``, adopted from ``irc.functools``. 1.19 ==== Added ``.reset`` support to ``once``. 1.18 ==== Add ``result_invoke`` decorator. 1.17 ==== Add ``retry`` decorator. 1.16 ==== #7: ``retry_call`` now accepts infinity for the ``retries`` parameter. 1.15.2 ====== Refresh packaging. 1.15.1 ====== Fix assign_params on Python 2. 1.15 ==== Add ``assign_params`` function. 1.14 ==== Add ``pass_none`` decorator function. 1.13 ==== Add ``print_yielded`` func implementing the func of the same name found in autocommand docs. 1.12 ==== Issue #6: Added a bit of documentation and xfail tests showing that the ``method_cache`` can't be used with other decorators such as ``property``. 1.11 ==== Include dates and links in changelog. 1.10 ==== Use Github for continuous deployment to PyPI. 1.9 === Add ``retry_call``, a general-purpose function retry mechanism. See ``test_functools`` for tests and example usage. 1.8 === More generous handling of missing lru_cache when installed on Python 2 and older pip. Now all functools except ``method_cache`` will continue to work even if ``backports.functools_lru_cache`` is not installed. Also allows functools32 as a fallback if available. 1.7 === Moved hosting to github. 1.6 === ``method_cache`` now accepts a cache_wrapper parameter, allowing for custom parameters to an ``lru_cache`` or an entirely different cache implementation. Use ``backports.functools_lru_cache`` to provide ``lru_cache`` for Python 2. 1.5 === Implement ``Throttler`` as a descriptor so it may be used to decorate methods. Introduces ``first_invoke`` function. Fixed failure in Throttler on Python 2 due to improper use of integer division. 1.4 === Added ``Throttler`` class from `irc `_. 1.3 === Added ``call_aside`` decorator. 1.2 === Added ``apply`` decorator. 1.0 === Initial release drawn from jaraco.util. jaraco.functools-4.1.0/README.rst000066400000000000000000000027411467560532000164710ustar00rootroot00000000000000.. image:: https://img.shields.io/pypi/v/jaraco.functools.svg :target: https://pypi.org/project/jaraco.functools .. image:: https://img.shields.io/pypi/pyversions/jaraco.functools.svg .. image:: https://github.com/jaraco/jaraco.functools/actions/workflows/main.yml/badge.svg :target: https://github.com/jaraco/jaraco.functools/actions?query=workflow%3A%22tests%22 :alt: tests .. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json :target: https://github.com/astral-sh/ruff :alt: Ruff .. image:: https://readthedocs.org/projects/jaracofunctools/badge/?version=latest :target: https://jaracofunctools.readthedocs.io/en/latest/?badge=latest .. image:: https://img.shields.io/badge/skeleton-2024-informational :target: https://blog.jaraco.com/skeleton .. image:: https://tidelift.com/badges/package/pypi/jaraco.functools :target: https://tidelift.com/subscription/pkg/pypi-jaraco.functools?utm_source=pypi-jaraco.functools&utm_medium=readme Additional functools in the spirit of stdlib's functools. For Enterprise ============== Available as part of the Tidelift Subscription. This project and the maintainers of thousands of other packages are working with Tidelift to deliver one enterprise subscription that covers all of the open source you use. `Learn more `_. jaraco.functools-4.1.0/SECURITY.md000066400000000000000000000002641467560532000165710ustar00rootroot00000000000000# Security Contact To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. jaraco.functools-4.1.0/conftest.py000066400000000000000000000012641467560532000172000ustar00rootroot00000000000000import re import sys import jaraco.functools def pytest_configure() -> None: patch_for_issue_12() def patch_for_issue_12() -> None: # pragma: nocover """ Issue #12 revealed that Python 3.7.3 had a subtle change in the C implementation of functools that broke the assumptions around the method_cache (or any caller using possibly empty keyword arguments). This patch adjusts the docstring for that test so it can pass on that Python version. """ affected_ver = 3, 7, 3 if sys.version_info[:3] != affected_ver: return mc = jaraco.functools.method_cache mc.__doc__ = re.sub(r'^(\s+)75', r'\g<1>76', mc.__doc__ or "", flags=re.M) jaraco.functools-4.1.0/docs/000077500000000000000000000000001467560532000157265ustar00rootroot00000000000000jaraco.functools-4.1.0/docs/conf.py000066400000000000000000000031151467560532000172250ustar00rootroot00000000000000from __future__ import annotations extensions = [ 'sphinx.ext.autodoc', 'jaraco.packaging.sphinx', ] master_doc = "index" html_theme = "furo" # Link dates and other references in the changelog extensions += ['rst.linker'] link_files = { '../NEWS.rst': dict( using=dict(GH='https://github.com'), replace=[ dict( pattern=r'(Issue #|\B#)(?P\d+)', url='{package_url}/issues/{issue}', ), dict( pattern=r'(?m:^((?Pv?\d+(\.\d+){1,2}))\n[-=]+\n)', with_scm='{text}\n{rev[timestamp]:%d %b %Y}\n', ), dict( pattern=r'PEP[- ](?P\d+)', url='https://peps.python.org/pep-{pep_number:0>4}/', ), ], ) } # Be strict about any broken references nitpicky = True nitpick_ignore: list[tuple[str, str]] = [] # Include Python intersphinx mapping to prevent failures # jaraco/skeleton#51 extensions += ['sphinx.ext.intersphinx'] intersphinx_mapping = { 'python': ('https://docs.python.org/3', None), } # Preserve authored syntax for defaults autodoc_preserve_defaults = True # Add support for linking usernames, PyPI projects, Wikipedia pages github_url = 'https://github.com/' extlinks = { 'user': (f'{github_url}%s', '@%s'), 'pypi': ('https://pypi.org/project/%s', '%s'), 'wiki': ('https://wikipedia.org/wiki/%s', '%s'), } extensions += ['sphinx.ext.extlinks'] # local extensions += ['jaraco.tidelift'] nitpick_ignore.append( ('py:class', 'jaraco.functools._T'), ) jaraco.functools-4.1.0/docs/history.rst000066400000000000000000000001161467560532000201570ustar00rootroot00000000000000:tocdepth: 2 .. _changes: History ******* .. include:: ../NEWS (links).rst jaraco.functools-4.1.0/docs/index.rst000066400000000000000000000005621467560532000175720ustar00rootroot00000000000000Welcome to |project| documentation! =================================== .. sidebar-links:: :home: :pypi: .. toctree:: :maxdepth: 1 history .. tidelift-referral-banner:: .. automodule:: jaraco.functools :members: :undoc-members: :show-inheritance: Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search` jaraco.functools-4.1.0/jaraco/000077500000000000000000000000001467560532000162355ustar00rootroot00000000000000jaraco.functools-4.1.0/jaraco/functools/000077500000000000000000000000001467560532000202515ustar00rootroot00000000000000jaraco.functools-4.1.0/jaraco/functools/__init__.py000066400000000000000000000427201467560532000223670ustar00rootroot00000000000000from __future__ import annotations import collections.abc import functools import inspect import itertools import operator import time import types import warnings from typing import Callable, TypeVar import more_itertools def compose(*funcs): """ Compose any number of unary functions into a single unary function. Comparable to `function composition `_ in mathematics: ``h = g ∘ f`` implies ``h(x) = g(f(x))``. In Python, ``h = compose(g, f)``. >>> import textwrap >>> expected = str.strip(textwrap.dedent(compose.__doc__)) >>> strip_and_dedent = compose(str.strip, textwrap.dedent) >>> strip_and_dedent(compose.__doc__) == expected True Compose also allows the innermost function to take arbitrary arguments. >>> round_three = lambda x: round(x, ndigits=3) >>> f = compose(round_three, int.__truediv__) >>> [f(3*x, x+1) for x in range(1,10)] [1.5, 2.0, 2.25, 2.4, 2.5, 2.571, 2.625, 2.667, 2.7] """ def compose_two(f1, f2): return lambda *args, **kwargs: f1(f2(*args, **kwargs)) return functools.reduce(compose_two, funcs) def once(func): """ Decorate func so it's only ever called the first time. This decorator can ensure that an expensive or non-idempotent function will not be expensive on subsequent calls and is idempotent. >>> add_three = once(lambda a: a+3) >>> add_three(3) 6 >>> add_three(9) 6 >>> add_three('12') 6 To reset the stored value, simply clear the property ``saved_result``. >>> del add_three.saved_result >>> add_three(9) 12 >>> add_three(8) 12 Or invoke 'reset()' on it. >>> add_three.reset() >>> add_three(-3) 0 >>> add_three(0) 0 """ @functools.wraps(func) def wrapper(*args, **kwargs): if not hasattr(wrapper, 'saved_result'): wrapper.saved_result = func(*args, **kwargs) return wrapper.saved_result wrapper.reset = lambda: vars(wrapper).__delitem__('saved_result') return wrapper def method_cache(method, cache_wrapper=functools.lru_cache()): """ Wrap lru_cache to support storing the cache data in the object instances. Abstracts the common paradigm where the method explicitly saves an underscore-prefixed protected property on first call and returns that subsequently. >>> class MyClass: ... calls = 0 ... ... @method_cache ... def method(self, value): ... self.calls += 1 ... return value >>> a = MyClass() >>> a.method(3) 3 >>> for x in range(75): ... res = a.method(x) >>> a.calls 75 Note that the apparent behavior will be exactly like that of lru_cache except that the cache is stored on each instance, so values in one instance will not flush values from another, and when an instance is deleted, so are the cached values for that instance. >>> b = MyClass() >>> for x in range(35): ... res = b.method(x) >>> b.calls 35 >>> a.method(0) 0 >>> a.calls 75 Note that if method had been decorated with ``functools.lru_cache()``, a.calls would have been 76 (due to the cached value of 0 having been flushed by the 'b' instance). Clear the cache with ``.cache_clear()`` >>> a.method.cache_clear() Same for a method that hasn't yet been called. >>> c = MyClass() >>> c.method.cache_clear() Another cache wrapper may be supplied: >>> cache = functools.lru_cache(maxsize=2) >>> MyClass.method2 = method_cache(lambda self: 3, cache_wrapper=cache) >>> a = MyClass() >>> a.method2() 3 Caution - do not subsequently wrap the method with another decorator, such as ``@property``, which changes the semantics of the function. See also http://code.activestate.com/recipes/577452-a-memoize-decorator-for-instance-methods/ for another implementation and additional justification. """ def wrapper(self, *args, **kwargs): # it's the first call, replace the method with a cached, bound method bound_method = types.MethodType(method, self) cached_method = cache_wrapper(bound_method) setattr(self, method.__name__, cached_method) return cached_method(*args, **kwargs) # Support cache clear even before cache has been created. wrapper.cache_clear = lambda: None return _special_method_cache(method, cache_wrapper) or wrapper def _special_method_cache(method, cache_wrapper): """ Because Python treats special methods differently, it's not possible to use instance attributes to implement the cached methods. Instead, install the wrapper method under a different name and return a simple proxy to that wrapper. https://github.com/jaraco/jaraco.functools/issues/5 """ name = method.__name__ special_names = '__getattr__', '__getitem__' if name not in special_names: return None wrapper_name = '__cached' + name def proxy(self, /, *args, **kwargs): if wrapper_name not in vars(self): bound = types.MethodType(method, self) cache = cache_wrapper(bound) setattr(self, wrapper_name, cache) else: cache = getattr(self, wrapper_name) return cache(*args, **kwargs) return proxy def apply(transform): """ Decorate a function with a transform function that is invoked on results returned from the decorated function. >>> @apply(reversed) ... def get_numbers(start): ... "doc for get_numbers" ... return range(start, start+3) >>> list(get_numbers(4)) [6, 5, 4] >>> get_numbers.__doc__ 'doc for get_numbers' """ def wrap(func): return functools.wraps(func)(compose(transform, func)) return wrap def result_invoke(action): r""" Decorate a function with an action function that is invoked on the results returned from the decorated function (for its side effect), then return the original result. >>> @result_invoke(print) ... def add_two(a, b): ... return a + b >>> x = add_two(2, 3) 5 >>> x 5 """ def wrap(func): @functools.wraps(func) def wrapper(*args, **kwargs): result = func(*args, **kwargs) action(result) return result return wrapper return wrap def invoke(f, /, *args, **kwargs): """ Call a function for its side effect after initialization. The benefit of using the decorator instead of simply invoking a function after defining it is that it makes explicit the author's intent for the function to be called immediately. Whereas if one simply calls the function immediately, it's less obvious if that was intentional or incidental. It also avoids repeating the name - the two actions, defining the function and calling it immediately are modeled separately, but linked by the decorator construct. The benefit of having a function construct (opposed to just invoking some behavior inline) is to serve as a scope in which the behavior occurs. It avoids polluting the global namespace with local variables, provides an anchor on which to attach documentation (docstring), keeps the behavior logically separated (instead of conceptually separated or not separated at all), and provides potential to re-use the behavior for testing or other purposes. This function is named as a pithy way to communicate, "call this function primarily for its side effect", or "while defining this function, also take it aside and call it". It exists because there's no Python construct for "define and call" (nor should there be, as decorators serve this need just fine). The behavior happens immediately and synchronously. >>> @invoke ... def func(): print("called") called >>> func() called Use functools.partial to pass parameters to the initial call >>> @functools.partial(invoke, name='bingo') ... def func(name): print('called with', name) called with bingo """ f(*args, **kwargs) return f class Throttler: """Rate-limit a function (or other callable).""" def __init__(self, func, max_rate=float('Inf')): if isinstance(func, Throttler): func = func.func self.func = func self.max_rate = max_rate self.reset() def reset(self): self.last_called = 0 def __call__(self, *args, **kwargs): self._wait() return self.func(*args, **kwargs) def _wait(self): """Ensure at least 1/max_rate seconds from last call.""" elapsed = time.time() - self.last_called must_wait = 1 / self.max_rate - elapsed time.sleep(max(0, must_wait)) self.last_called = time.time() def __get__(self, obj, owner=None): return first_invoke(self._wait, functools.partial(self.func, obj)) def first_invoke(func1, func2): """ Return a function that when invoked will invoke func1 without any parameters (for its side effect) and then invoke func2 with whatever parameters were passed, returning its result. """ def wrapper(*args, **kwargs): func1() return func2(*args, **kwargs) return wrapper method_caller = first_invoke( lambda: warnings.warn( '`jaraco.functools.method_caller` is deprecated, ' 'use `operator.methodcaller` instead', DeprecationWarning, stacklevel=3, ), operator.methodcaller, ) def retry_call(func, cleanup=lambda: None, retries=0, trap=()): """ Given a callable func, trap the indicated exceptions for up to 'retries' times, invoking cleanup on the exception. On the final attempt, allow any exceptions to propagate. """ attempts = itertools.count() if retries == float('inf') else range(retries) for _ in attempts: try: return func() except trap: cleanup() return func() def retry(*r_args, **r_kwargs): """ Decorator wrapper for retry_call. Accepts arguments to retry_call except func and then returns a decorator for the decorated function. Ex: >>> @retry(retries=3) ... def my_func(a, b): ... "this is my funk" ... print(a, b) >>> my_func.__doc__ 'this is my funk' """ def decorate(func): @functools.wraps(func) def wrapper(*f_args, **f_kwargs): bound = functools.partial(func, *f_args, **f_kwargs) return retry_call(bound, *r_args, **r_kwargs) return wrapper return decorate def print_yielded(func): """ Convert a generator into a function that prints all yielded elements. >>> @print_yielded ... def x(): ... yield 3; yield None >>> x() 3 None """ print_all = functools.partial(map, print) print_results = compose(more_itertools.consume, print_all, func) return functools.wraps(func)(print_results) def pass_none(func): """ Wrap func so it's not called if its first param is None. >>> print_text = pass_none(print) >>> print_text('text') text >>> print_text(None) """ @functools.wraps(func) def wrapper(param, /, *args, **kwargs): if param is not None: return func(param, *args, **kwargs) return None return wrapper def assign_params(func, namespace): """ Assign parameters from namespace where func solicits. >>> def func(x, y=3): ... print(x, y) >>> assigned = assign_params(func, dict(x=2, z=4)) >>> assigned() 2 3 The usual errors are raised if a function doesn't receive its required parameters: >>> assigned = assign_params(func, dict(y=3, z=4)) >>> assigned() Traceback (most recent call last): TypeError: func() ...argument... It even works on methods: >>> class Handler: ... def meth(self, arg): ... print(arg) >>> assign_params(Handler().meth, dict(arg='crystal', foo='clear'))() crystal """ sig = inspect.signature(func) params = sig.parameters.keys() call_ns = {k: namespace[k] for k in params if k in namespace} return functools.partial(func, **call_ns) def save_method_args(method): """ Wrap a method such that when it is called, the args and kwargs are saved on the method. >>> class MyClass: ... @save_method_args ... def method(self, a, b): ... print(a, b) >>> my_ob = MyClass() >>> my_ob.method(1, 2) 1 2 >>> my_ob._saved_method.args (1, 2) >>> my_ob._saved_method.kwargs {} >>> my_ob.method(a=3, b='foo') 3 foo >>> my_ob._saved_method.args () >>> my_ob._saved_method.kwargs == dict(a=3, b='foo') True The arguments are stored on the instance, allowing for different instance to save different args. >>> your_ob = MyClass() >>> your_ob.method({str('x'): 3}, b=[4]) {'x': 3} [4] >>> your_ob._saved_method.args ({'x': 3},) >>> my_ob._saved_method.args () """ args_and_kwargs = collections.namedtuple('args_and_kwargs', 'args kwargs') @functools.wraps(method) def wrapper(self, /, *args, **kwargs): attr_name = '_saved_' + method.__name__ attr = args_and_kwargs(args, kwargs) setattr(self, attr_name, attr) return method(self, *args, **kwargs) return wrapper def except_(*exceptions, replace=None, use=None): """ Replace the indicated exceptions, if raised, with the indicated literal replacement or evaluated expression (if present). >>> safe_int = except_(ValueError)(int) >>> safe_int('five') >>> safe_int('5') 5 Specify a literal replacement with ``replace``. >>> safe_int_r = except_(ValueError, replace=0)(int) >>> safe_int_r('five') 0 Provide an expression to ``use`` to pass through particular parameters. >>> safe_int_pt = except_(ValueError, use='args[0]')(int) >>> safe_int_pt('five') 'five' """ def decorate(func): @functools.wraps(func) def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except exceptions: try: return eval(use) except TypeError: return replace return wrapper return decorate def identity(x): """ Return the argument. >>> o = object() >>> identity(o) is o True """ return x def bypass_when(check, *, _op=identity): """ Decorate a function to return its parameter when ``check``. >>> bypassed = [] # False >>> @bypass_when(bypassed) ... def double(x): ... return x * 2 >>> double(2) 4 >>> bypassed[:] = [object()] # True >>> double(2) 2 """ def decorate(func): @functools.wraps(func) def wrapper(param, /): return param if _op(check) else func(param) return wrapper return decorate def bypass_unless(check): """ Decorate a function to return its parameter unless ``check``. >>> enabled = [object()] # True >>> @bypass_unless(enabled) ... def double(x): ... return x * 2 >>> double(2) 4 >>> del enabled[:] # False >>> double(2) 2 """ return bypass_when(check, _op=operator.not_) @functools.singledispatch def _splat_inner(args, func): """Splat args to func.""" return func(*args) @_splat_inner.register def _(args: collections.abc.Mapping, func): """Splat kargs to func as kwargs.""" return func(**args) def splat(func): """ Wrap func to expect its parameters to be passed positionally in a tuple. Has a similar effect to that of ``itertools.starmap`` over simple ``map``. >>> pairs = [(-1, 1), (0, 2)] >>> more_itertools.consume(itertools.starmap(print, pairs)) -1 1 0 2 >>> more_itertools.consume(map(splat(print), pairs)) -1 1 0 2 The approach generalizes to other iterators that don't have a "star" equivalent, such as a "starfilter". >>> list(filter(splat(operator.add), pairs)) [(0, 2)] Splat also accepts a mapping argument. >>> def is_nice(msg, code): ... return "smile" in msg or code == 0 >>> msgs = [ ... dict(msg='smile!', code=20), ... dict(msg='error :(', code=1), ... dict(msg='unknown', code=0), ... ] >>> for msg in filter(splat(is_nice), msgs): ... print(msg) {'msg': 'smile!', 'code': 20} {'msg': 'unknown', 'code': 0} """ return functools.wraps(func)(functools.partial(_splat_inner, func=func)) _T = TypeVar('_T') def chainable(method: Callable[[_T, ...], None]) -> Callable[[_T, ...], _T]: """ Wrap an instance method to always return self. >>> class Dingus: ... @chainable ... def set_attr(self, name, val): ... setattr(self, name, val) >>> d = Dingus().set_attr('a', 'eh!') >>> d.a 'eh!' >>> d2 = Dingus().set_attr('a', 'eh!').set_attr('b', 'bee!') >>> d2.a + d2.b 'eh!bee!' Enforces that the return value is null. >>> class BorkedDingus: ... @chainable ... def set_attr(self, name, val): ... setattr(self, name, val) ... return len(name) >>> BorkedDingus().set_attr('a', 'eh!') Traceback (most recent call last): ... AssertionError """ @functools.wraps(method) def wrapper(self, *args, **kwargs): assert method(self, *args, **kwargs) is None return self return wrapper jaraco.functools-4.1.0/jaraco/functools/__init__.pyi000066400000000000000000000074461467560532000225460ustar00rootroot00000000000000from collections.abc import Callable, Hashable, Iterator from functools import partial from operator import methodcaller import sys from typing import ( Any, Generic, Protocol, TypeVar, overload, ) if sys.version_info >= (3, 10): from typing import Concatenate, ParamSpec else: from typing_extensions import Concatenate, ParamSpec _P = ParamSpec('_P') _R = TypeVar('_R') _T = TypeVar('_T') _R1 = TypeVar('_R1') _R2 = TypeVar('_R2') _V = TypeVar('_V') _S = TypeVar('_S') _R_co = TypeVar('_R_co', covariant=True) class _OnceCallable(Protocol[_P, _R]): saved_result: _R reset: Callable[[], None] def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R: ... class _ProxyMethodCacheWrapper(Protocol[_R_co]): cache_clear: Callable[[], None] def __call__(self, *args: Hashable, **kwargs: Hashable) -> _R_co: ... class _MethodCacheWrapper(Protocol[_R_co]): def cache_clear(self) -> None: ... def __call__(self, *args: Hashable, **kwargs: Hashable) -> _R_co: ... # `compose()` overloads below will cover most use cases. @overload def compose( __func1: Callable[[_R], _T], __func2: Callable[_P, _R], /, ) -> Callable[_P, _T]: ... @overload def compose( __func1: Callable[[_R], _T], __func2: Callable[[_R1], _R], __func3: Callable[_P, _R1], /, ) -> Callable[_P, _T]: ... @overload def compose( __func1: Callable[[_R], _T], __func2: Callable[[_R2], _R], __func3: Callable[[_R1], _R2], __func4: Callable[_P, _R1], /, ) -> Callable[_P, _T]: ... def once(func: Callable[_P, _R]) -> _OnceCallable[_P, _R]: ... def method_cache( method: Callable[..., _R], cache_wrapper: Callable[[Callable[..., _R]], _MethodCacheWrapper[_R]] = ..., ) -> _MethodCacheWrapper[_R] | _ProxyMethodCacheWrapper[_R]: ... def apply( transform: Callable[[_R], _T] ) -> Callable[[Callable[_P, _R]], Callable[_P, _T]]: ... def result_invoke( action: Callable[[_R], Any] ) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: ... def invoke( f: Callable[_P, _R], /, *args: _P.args, **kwargs: _P.kwargs ) -> Callable[_P, _R]: ... class Throttler(Generic[_R]): last_called: float func: Callable[..., _R] max_rate: float def __init__( self, func: Callable[..., _R] | Throttler[_R], max_rate: float = ... ) -> None: ... def reset(self) -> None: ... def __call__(self, *args: Any, **kwargs: Any) -> _R: ... def __get__(self, obj: Any, owner: type[Any] | None = ...) -> Callable[..., _R]: ... def first_invoke( func1: Callable[..., Any], func2: Callable[_P, _R] ) -> Callable[_P, _R]: ... method_caller: Callable[..., methodcaller] def retry_call( func: Callable[..., _R], cleanup: Callable[..., None] = ..., retries: int | float = ..., trap: type[BaseException] | tuple[type[BaseException], ...] = ..., ) -> _R: ... def retry( cleanup: Callable[..., None] = ..., retries: int | float = ..., trap: type[BaseException] | tuple[type[BaseException], ...] = ..., ) -> Callable[[Callable[..., _R]], Callable[..., _R]]: ... def print_yielded(func: Callable[_P, Iterator[Any]]) -> Callable[_P, None]: ... def pass_none( func: Callable[Concatenate[_T, _P], _R] ) -> Callable[Concatenate[_T, _P], _R]: ... def assign_params( func: Callable[..., _R], namespace: dict[str, Any] ) -> partial[_R]: ... def save_method_args( method: Callable[Concatenate[_S, _P], _R] ) -> Callable[Concatenate[_S, _P], _R]: ... def except_( *exceptions: type[BaseException], replace: Any = ..., use: Any = ... ) -> Callable[[Callable[_P, Any]], Callable[_P, Any]]: ... def identity(x: _T) -> _T: ... def bypass_when( check: _V, *, _op: Callable[[_V], Any] = ... ) -> Callable[[Callable[[_T], _R]], Callable[[_T], _T | _R]]: ... def bypass_unless( check: Any, ) -> Callable[[Callable[[_T], _R]], Callable[[_T], _T | _R]]: ... jaraco.functools-4.1.0/jaraco/functools/py.typed000066400000000000000000000000001467560532000217360ustar00rootroot00000000000000jaraco.functools-4.1.0/mypy.ini000066400000000000000000000005661467560532000165040ustar00rootroot00000000000000[mypy] # Is the project well-typed? strict = True # Early opt-in even when strict = False warn_unused_ignores = True warn_redundant_casts = True enable_error_code = ignore-without-code # Support namespace packages per https://github.com/python/mypy/issues/14057 explicit_package_bases = True disable_error_code = # Disable due to many false positives overload-overlap, jaraco.functools-4.1.0/pyproject.toml000066400000000000000000000022651467560532000177170ustar00rootroot00000000000000[build-system] requires = ["setuptools>=61.2", "setuptools_scm[toml]>=3.4.1"] build-backend = "setuptools.build_meta" [project] name = "jaraco.functools" authors = [ { name = "Jason R. Coombs", email = "jaraco@jaraco.com" }, ] description = "Functools like those found in stdlib" readme = "README.rst" classifiers = [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", ] requires-python = ">=3.8" dependencies = [ "more_itertools", ] dynamic = ["version"] [project.urls] Source = "https://github.com/jaraco/jaraco.functools" [project.optional-dependencies] test = [ # upstream "pytest >= 6, != 8.1.*", # local "jaraco.classes", ] doc = [ # upstream "sphinx >= 3.5", "jaraco.packaging >= 9.3", "rst.linker >= 1.9", "furo", "sphinx-lint", # tidelift "jaraco.tidelift >= 1.4", # local ] check = [ "pytest-checkdocs >= 2.4", "pytest-ruff >= 0.2.1; sys_platform != 'cygwin'", ] cover = [ "pytest-cov", ] enabler = [ "pytest-enabler >= 2.2", ] type = [ # upstream "pytest-mypy", # local ] [tool.setuptools_scm] jaraco.functools-4.1.0/pytest.ini000066400000000000000000000011101467560532000170200ustar00rootroot00000000000000[pytest] norecursedirs=dist build .tox .eggs addopts= --doctest-modules --import-mode importlib consider_namespace_packages=true filterwarnings= ## upstream # Ensure ResourceWarnings are emitted default::ResourceWarning # realpython/pytest-mypy#152 ignore:'encoding' argument not specified::pytest_mypy # python/cpython#100750 ignore:'encoding' argument not specified::platform # pypa/build#615 ignore:'encoding' argument not specified::build.env # dateutil/dateutil#1284 ignore:datetime.datetime.utcfromtimestamp:DeprecationWarning:dateutil.tz.tz ## end upstream jaraco.functools-4.1.0/ruff.toml000066400000000000000000000010201467560532000166260ustar00rootroot00000000000000[lint] extend-select = [ "C901", "PERF401", "W", ] ignore = [ # https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules "W191", "E111", "E114", "E117", "D206", "D300", "Q000", "Q001", "Q002", "Q003", "COM812", "COM819", "ISC001", "ISC002", ] [format] # Enable preview to get hugged parenthesis unwrapping and other nice surprises # See https://github.com/jaraco/skeleton/pull/133#issuecomment-2239538373 preview = true # https://docs.astral.sh/ruff/settings/#format_quote-style quote-style = "preserve" jaraco.functools-4.1.0/test_functools.py000066400000000000000000000167311467560532000204330ustar00rootroot00000000000000from __future__ import annotations import copy import functools import itertools import os import platform import random import time from typing import Literal, TypeVar from unittest import mock import pytest from jaraco.classes import properties from jaraco.functools import Throttler, method_cache, retry, retry_call _T = TypeVar("_T") class TestThrottler: @pytest.mark.xfail( 'GITHUB_ACTIONS' in os.environ and platform.system() in ('Darwin', 'Windows'), reason="Performance is heavily throttled on Github Actions Mac/Windows runs", ) def test_function_throttled(self) -> None: """ Ensure the throttler actually throttles calls. """ # set up a function to be called counter = itertools.count() # set up a version of `next` that is only called 30 times per second limited_next = Throttler(next, 30) # for one second, call next as fast as possible deadline = time.time() + 1 while time.time() < deadline: limited_next(counter) # ensure the counter was advanced about 30 times assert 28 <= next(counter) <= 32 # ensure that another burst of calls after some idle period will also # get throttled time.sleep(1) deadline = time.time() + 1 counter = itertools.count() while time.time() < deadline: limited_next(counter) assert 28 <= next(counter) <= 32 def test_reconstruct_unwraps(self) -> None: """ The throttler should be re-usable - if one wants to throttle a function that's aready throttled, the original function should be used. """ wrapped = Throttler(next, 30) wrapped_again = Throttler(wrapped, 60) assert wrapped_again.func is next assert wrapped_again.max_rate == 60 def test_throttled_method(self) -> None: class ThrottledMethodClass: @Throttler def echo(self, arg: _T) -> _T: return arg tmc = ThrottledMethodClass() assert tmc.echo('foo') == 'foo' class TestMethodCache: bad_vers = '(3, 5, 0) <= sys.version_info < (3, 5, 2)' @pytest.mark.skipif(bad_vers, reason="https://bugs.python.org/issue25447") def test_deepcopy(self) -> None: """ A deepcopy of an object with a method cache should still succeed. """ class ClassUnderTest: calls = 0 @method_cache def method(self, value: _T) -> _T: self.calls += 1 return value ob = ClassUnderTest() copy.deepcopy(ob) ob.method(1) copy.deepcopy(ob) def test_special_methods(self) -> None: """ Test method_cache with __getitem__ and __getattr__. """ class ClassUnderTest: getitem_calls = 0 getattr_calls = 0 @method_cache def __getitem__(self, item: _T) -> _T: self.getitem_calls += 1 return item @method_cache def __getattr__(self, name: _T) -> _T: self.getattr_calls += 1 return name ob = ClassUnderTest() # __getitem__ ob[1] + ob[1] assert ob.getitem_calls == 1 # __getattr__ ob.one + ob.one # type: ignore[operator] # Using ParamSpec on methods is still limited assert ob.getattr_calls == 1 @pytest.mark.xfail(reason="can't replace property with cache; #6") def test_property(self) -> None: """ Can a method_cache decorated method also be a property? """ class ClassUnderTest: @property @method_cache def mything(self) -> float: # pragma: nocover return random.random() ob = ClassUnderTest() assert ob.mything == ob.mything @pytest.mark.xfail(reason="can't replace property with cache; #6") def test_non_data_property(self) -> None: """ A non-data property also does not work because the property gets replaced with a method. """ class ClassUnderTest: @properties.NonDataProperty @method_cache def mything(self) -> float: return random.random() ob = ClassUnderTest() assert ob.mything == ob.mything class TestRetry: def attempt(self, arg: mock.Mock | None = None) -> Literal['Success']: if next(self.fails_left): raise ValueError("Failed!") if arg: arg.touch() return "Success" def set_to_fail(self, times: int) -> None: self.fails_left = itertools.count(times, -1) def test_set_to_fail(self) -> None: """ Test this test's internal failure mechanism. """ self.set_to_fail(times=2) with pytest.raises(ValueError): self.attempt() with pytest.raises(ValueError): self.attempt() assert self.attempt() == 'Success' def test_retry_call_succeeds(self) -> None: self.set_to_fail(times=2) res = retry_call(self.attempt, retries=2, trap=ValueError) assert res == "Success" def test_retry_call_fails(self) -> None: """ Failing more than the number of retries should raise the underlying error. """ self.set_to_fail(times=3) with pytest.raises(ValueError) as res: retry_call(self.attempt, retries=2, trap=ValueError) assert str(res.value) == 'Failed!' def test_retry_multiple_exceptions(self) -> None: self.set_to_fail(times=2) errors = ValueError, NameError res = retry_call(self.attempt, retries=2, trap=errors) assert res == "Success" def test_retry_exception_superclass(self) -> None: self.set_to_fail(times=2) res = retry_call(self.attempt, retries=2, trap=Exception) assert res == "Success" def test_default_traps_nothing(self) -> None: self.set_to_fail(times=1) with pytest.raises(ValueError): retry_call(self.attempt, retries=1) def test_default_does_not_retry(self) -> None: self.set_to_fail(times=1) with pytest.raises(ValueError): retry_call(self.attempt, trap=Exception) def test_cleanup_called_on_exception(self) -> None: calls = random.randint(1, 10) cleanup = mock.Mock() self.set_to_fail(times=calls) retry_call(self.attempt, retries=calls, cleanup=cleanup, trap=Exception) assert cleanup.call_count == calls cleanup.assert_called_with() def test_infinite_retries(self) -> None: self.set_to_fail(times=999) cleanup = mock.Mock() retry_call(self.attempt, retries=float('inf'), cleanup=cleanup, trap=Exception) assert cleanup.call_count == 999 def test_with_arg(self) -> None: self.set_to_fail(times=0) arg = mock.Mock() bound = functools.partial(self.attempt, arg) res = retry_call(bound) assert res == 'Success' assert arg.touch.called def test_decorator(self) -> None: self.set_to_fail(times=1) attempt = retry(retries=1, trap=Exception)(self.attempt) res = attempt() assert res == "Success" def test_decorator_with_arg(self) -> None: self.set_to_fail(times=0) attempt = retry()(self.attempt) arg = mock.Mock() res = attempt(arg) assert res == 'Success' assert arg.touch.called jaraco.functools-4.1.0/towncrier.toml000066400000000000000000000000541467560532000177060ustar00rootroot00000000000000[tool.towncrier] title_format = "{version}" jaraco.functools-4.1.0/tox.ini000066400000000000000000000025061467560532000163140ustar00rootroot00000000000000[testenv] description = perform primary checks (tests, style, types, coverage) deps = setenv = PYTHONWARNDEFAULTENCODING = 1 commands = pytest {posargs} usedevelop = True extras = test check cover enabler type passenv = GITHUB_ACTIONS [testenv:diffcov] description = run tests and check that diff from main is covered deps = {[testenv]deps} diff-cover commands = pytest {posargs} --cov-report xml diff-cover coverage.xml --compare-branch=origin/main --html-report diffcov.html diff-cover coverage.xml --compare-branch=origin/main --fail-under=100 [testenv:docs] description = build the documentation extras = doc test changedir = docs commands = python -m sphinx -W --keep-going . {toxinidir}/build/html python -m sphinxlint [testenv:finalize] description = assemble changelog and tag a release skip_install = True deps = towncrier jaraco.develop >= 7.23 pass_env = * commands = python -m jaraco.develop.finalize [testenv:release] description = publish the package to PyPI and GitHub skip_install = True deps = build twine>=3 jaraco.develop>=7.1 pass_env = TWINE_PASSWORD GITHUB_TOKEN setenv = TWINE_USERNAME = {env:TWINE_USERNAME:__token__} commands = python -c "import shutil; shutil.rmtree('dist', ignore_errors=True)" python -m build python -m twine upload dist/* python -m jaraco.develop.create-github-release