pax_global_header00006660000000000000000000000064134126732020014512gustar00rootroot0000000000000052 comment=d84738ac9f0fcc7a6f175a9873f63a70b87881c4 jaraco.functools-2.0/000077500000000000000000000000001341267320200146255ustar00rootroot00000000000000jaraco.functools-2.0/.flake8000066400000000000000000000003261341267320200160010ustar00rootroot00000000000000[flake8] ignore = # Allow tabs for indentation W191 # W503 violates spec https://github.com/PyCQA/pycodestyle/issues/513 W503 # W504 has issues https://github.com/OCA/maintainer-quality-tools/issues/545 W504 jaraco.functools-2.0/.readthedocs.yml000066400000000000000000000001121341267320200177050ustar00rootroot00000000000000python: version: 3 extra_requirements: - docs pip_install: true jaraco.functools-2.0/.travis.yml000066400000000000000000000024231341267320200167370ustar00rootroot00000000000000dist: xenial sudo: false language: python python: - 2.7 - 3.6 - &latest_py3 3.7 jobs: fast_finish: true include: - stage: deploy if: tag IS present python: *latest_py3 before_script: skip env: - TWINE_USERNAME=jaraco # TWINE_PASSWORD - secure: MploUiVMnwq1vqCrSGRmdSlx21PnlXCg9w4bjhD6tYyN4NvHowbpT9E4or2aBW622xmIi7puiu3fuxgTrp7nh+7CVl0hR1Aa2+Y5ggA50uAHUoO5VSYN21w9V4nNe1u26IjnktZ9jK5Fz+aubrhA9kQX6M1Msowdo/IDCEi+gMQQdGR5S7IEBb0ZWpuW28AKT7j/iivs1gffrmmuiIJ+7o+khWJ/LZ9I0qAJCJDksqql1UP3ScZbOPihpjU0Lmo0aWxUSIiF6Qn7S4+3PAOXLxw29wf4yHECu+n1TzOWy3l/Hl5RpTLGPXeHfgWwDNOsHmWJhhc8rj2c+6fDgCWUMqxDWgJtOTKeRIRoxy6Dz8xosssYu/LhDPCklkNoswRRHvWLCCbwnJplNdmgWU+jZ9DQZ8Mvfy1dy6M92uxwYKfCYBTyGqR6tkAZD5zWZkm3gVXbJw2vMc+kKngPgNKY89w1epK3o53LybTaFvT+3FlUtgoMJjvUFbhGIqtpKcPKWToqUSrnaMmi3aJB9CcIeSE71oMn/4S5TTaI9JAwidVYZQw+JF3UeZ3aDJ2fAvj7Xpklk41sm5URJu9QFMbE10AxX4+deoRXC/mxlVfoTgRyWgxgjtjM6i1P++57ETx75K1FyDgI1qTXMvVCHbrLggjlvulTU5zB/yGs/pwBmQY= - TOX_TESTENV_PASSENV="TWINE_USERNAME TWINE_PASSWORD" script: tox -e release cache: pip install: - pip install tox tox-venv before_script: # Disable IPv6. Ref travis-ci/travis-ci#8361 - if [ "${TRAVIS_OS_NAME}" == "linux" ]; then sudo sh -c 'echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6'; fi script: tox jaraco.functools-2.0/CHANGES.rst000066400000000000000000000041001341267320200164220ustar00rootroot000000000000002.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-2.0/LICENSE000066400000000000000000000020321341267320200156270ustar00rootroot00000000000000Copyright Jason R. Coombs 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. jaraco.functools-2.0/README.rst000066400000000000000000000013071341267320200163150ustar00rootroot00000000000000.. 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://img.shields.io/travis/jaraco/jaraco.functools/master.svg :target: https://travis-ci.org/jaraco/jaraco.functools .. .. image:: https://img.shields.io/appveyor/ci/jaraco/jaraco-functools/master.svg .. :target: https://ci.appveyor.com/project/jaraco-functools/skeleton/branch/master .. image:: https://readthedocs.org/projects/jaracofunctools/badge/?version=latest :target: https://jaracofunctools.readthedocs.io/en/latest/?badge=latest Additional functools in the spirit of stdlib's functools.jaraco.functools-2.0/appveyor.yml000066400000000000000000000007061341267320200172200ustar00rootroot00000000000000environment: APPVEYOR: true matrix: - PYTHON: "C:\\Python36-x64" - PYTHON: "C:\\Python27-x64" install: # symlink python from a directory with a space - "mklink /d \"C:\\Program Files\\Python\" %PYTHON%" - "SET PYTHON=\"C:\\Program Files\\Python\"" - "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%" build: off cache: - '%LOCALAPPDATA%\pip\Cache' test_script: - "python -m pip install tox tox-venv" - "tox" version: '{build}' jaraco.functools-2.0/docs/000077500000000000000000000000001341267320200155555ustar00rootroot00000000000000jaraco.functools-2.0/docs/conf.py000066400000000000000000000013631341267320200170570ustar00rootroot00000000000000#!/usr/bin/env python3 # -*- coding: utf-8 -*- extensions = ["sphinx.ext.autodoc", "jaraco.packaging.sphinx", "rst.linker"] master_doc = "index" link_files = { "../CHANGES.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://www.python.org/dev/peps/pep-{pep_number:0>4}/", ), ], ) } jaraco.functools-2.0/docs/history.rst000066400000000000000000000001211341267320200200020ustar00rootroot00000000000000:tocdepth: 2 .. _changes: History ******* .. include:: ../CHANGES (links).rst jaraco.functools-2.0/docs/index.rst000066400000000000000000000004721341267320200174210ustar00rootroot00000000000000Welcome to jaraco.functools documentation! ========================================== .. toctree:: :maxdepth: 1 history .. automodule:: jaraco.functools :members: :undoc-members: :show-inheritance: Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search` jaraco.functools-2.0/jaraco/000077500000000000000000000000001341267320200160645ustar00rootroot00000000000000jaraco.functools-2.0/jaraco/__init__.py000066400000000000000000000001011341267320200201650ustar00rootroot00000000000000__path__ = __import__('pkgutil').extend_path(__path__, __name__) jaraco.functools-2.0/jaraco/functools.py000066400000000000000000000255021341267320200204560ustar00rootroot00000000000000from __future__ import ( absolute_import, unicode_literals, print_function, division, ) import functools import time import warnings import inspect import collections from itertools import count __metaclass__ = type try: from functools import lru_cache except ImportError: try: from backports.functools_lru_cache import lru_cache except ImportError: try: from functools32 import lru_cache except ImportError: warnings.warn("No lru_cache available") import more_itertools.recipes def compose(*funcs): """ Compose any number of unary functions into a single unary function. >>> import textwrap >>> from six import text_type >>> stripped = text_type.strip(textwrap.dedent(compose.__doc__)) >>> compose(text_type.strip, textwrap.dedent)(compose.__doc__) == stripped 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 method_caller(method_name, *args, **kwargs): """ Return a function that will call a named method on the target object with optional positional and keyword arguments. >>> lower = method_caller('lower') >>> lower('MyString') 'mystring' """ def call_method(target): func = getattr(target, method_name) return func(*args, **kwargs) return call_method 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=None): """ 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() Another cache wrapper may be supplied: >>> cache = 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. """ cache_wrapper = cache_wrapper or lru_cache() def wrapper(self, *args, **kwargs): # it's the first call, replace the method with a cached, bound method bound_method = functools.partial(method, self) cached_method = cache_wrapper(bound_method) setattr(self, method.__name__, cached_method) return cached_method(*args, **kwargs) 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 wrapper_name = '__cached' + name def proxy(self, *args, **kwargs): if wrapper_name not in vars(self): bound = functools.partial(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): ... return range(start, start+3) >>> list(get_numbers(4)) [6, 5, 4] """ def wrap(func): return 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 """ def wrap(func): @functools.wraps(func) def wrapper(*args, **kwargs): result = func(*args, **kwargs) action(result) return result return wrapper return wrap def call_aside(f, *args, **kwargs): """ Call a function for its side effect after initialization. >>> @call_aside ... def func(): print("called") called >>> func() called Use functools.partial to pass parameters to the initial call >>> @functools.partial(call_aside, 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, type=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 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 = count() if retries == float('inf') else range(retries) for attempt 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.recipes.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 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 """ try: sig = inspect.signature(func) params = sig.parameters.keys() except AttributeError: spec = inspect.getargspec(func) params = spec.args 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 jaraco.functools-2.0/pyproject.toml000066400000000000000000000001701341267320200175370ustar00rootroot00000000000000[build-system] requires = ["setuptools>=34.4", "wheel", "setuptools_scm>=1.15"] build-backend = "setuptools.build_meta" jaraco.functools-2.0/pytest.ini000066400000000000000000000007101341267320200166540ustar00rootroot00000000000000[pytest] norecursedirs=dist build .tox .eggs addopts=--doctest-modules --flake8 doctest_optionflags=ALLOW_UNICODE ELLIPSIS filterwarnings= ignore:Possible nested set::pycodestyle:113 ignore:Using or importing the ABCs::flake8:410 # workaround for https://sourceforge.net/p/docutils/bugs/348/ ignore:'U' mode is deprecated::docutils.io # workaround for https://gitlab.com/pycqa/flake8/issues/275 ignore:You passed a bytestring as `filenames`.::flake8 jaraco.functools-2.0/setup.cfg000066400000000000000000000017211341267320200164470ustar00rootroot00000000000000[bdist_wheel] universal = 1 [metadata] license_file = LICENSE name = jaraco.functools author = Jason R. Coombs author_email = jaraco@jaraco.com description = Functools like those found in stdlib long_description = file:README.rst url = https://github.com/jaraco/jaraco.functools classifiers = Development Status :: 5 - Production/Stable Intended Audience :: Developers License :: OSI Approved :: MIT License Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 [options] packages = find: include_package_data = true python_requires = >=2.7 install_requires = more_itertools backports.functools_lru_cache >= 1.0.3; python_version == "2.7" setup_requires = setuptools_scm >= 1.15.0 [options.extras_require] testing = # upstream pytest >= 3.5, !=3.7.3 pytest-checkdocs pytest-flake8 # local six backports.unittest_mock jaraco.classes docs = # upstream sphinx jaraco.packaging >= 3.2 rst.linker >= 1.9 # local [options.entry_points] jaraco.functools-2.0/setup.py000066400000000000000000000001601341267320200163340ustar00rootroot00000000000000#!/usr/bin/env python import setuptools if __name__ == "__main__": setuptools.setup(use_scm_version=True) jaraco.functools-2.0/skeleton.md000066400000000000000000000163601341267320200170010ustar00rootroot00000000000000# Overview This project is merged with [skeleton](https://github.com/jaraco/skeleton). What is skeleton? It's the scaffolding of a Python project jaraco [introduced in his blog](https://blog.jaraco.com/a-project-skeleton-for-python-projects/). It seeks to provide a means to re-use techniques and inherit advances when managing projects for distribution. ## An SCM Managed Approach While maintaining dozens of projects in PyPI, jaraco derives best practices for project distribution and publishes them in the [skeleton repo](https://github.com/jaraco/skeleton), a git repo capturing the evolution and culmination of these best practices. It's intended to be used by a new or existing project to adopt these practices and honed and proven techniques. Adopters are encouraged to use the project directly and maintain a small deviation from the technique, make their own fork for more substantial changes unique to their environment or preferences, or simply adopt the skeleton once and abandon it thereafter. The primary advantage to using an SCM for maintaining these techniques is that those tools help facilitate the merge between the template and its adopting projects. # Usage ## new projects To use skeleton for a new project, simply pull the skeleton into a new project: ``` $ git init my-new-project $ cd my-new-project $ git pull gh://jaraco/skeleton ``` Now customize the project to suit your individual project needs. ## existing projects If you have an existing project, you can still incorporate the skeleton by merging it into the codebase. ``` $ git merge skeleton --allow-unrelated-histories ``` The `--allow-unrelated-histories` is necessary because the history from the skeleton was previously unrelated to the existing codebase. Resolve any merge conflicts and commit to the master, and now the project is based on the shared skeleton. ## Updating Whenever a change is needed or desired for the general technique for packaging, it can be made in the skeleton project and then merged into each of the derived projects as needed, recommended before each release. As a result, features and best practices for packaging are centrally maintained and readily trickle into a whole suite of packages. This technique lowers the amount of tedious work necessary to create or maintain a project, and coupled with other techniques like continuous integration and deployment, lowers the cost of creating and maintaining refined Python projects to just a few, familiar git operations. Thereafter, the target project can make whatever customizations it deems relevant to the scaffolding. The project may even at some point decide that the divergence is too great to merit renewed merging with the original skeleton. This approach applies maximal guidance while creating minimal constraints. # Features The features/techniques employed by the skeleton include: - PEP 517/518 based build relying on setuptools as the build tool - setuptools declarative configuration using setup.cfg - tox for running tests - A README.rst as reStructuredText with some popular badges, but with readthedocs and appveyor badges commented out - A CHANGES.rst file intended for publishing release notes about the project. ## Packaging Conventions A pyproject.toml is included to enable PEP 517 and PEP 518 compatibility and declares the requirements necessary to build the project on setuptools (a minimum version compatible with setup.cfg declarative config). The setup.cfg file implements the following features: - Assumes universal wheel for release - Advertises the project's LICENSE file (MIT by default) - Reads the README.rst file into the long description - Some common Trove classifiers - Includes all packages discovered in the repo - Data files in the package are also included (not just Python files) - Declares the required Python versions - Declares install requirements (empty by default) - Declares setup requirements for legacy environments - Supplies two 'extras': - testing: requirements for running tests - docs: requirements for building docs - these extras split the declaration into "upstream" (requirements as declared by the skeleton) and "local" (those specific to the local project); these markers help avoid merge conflicts - Placeholder for defining entry points Additionally, the setup.py file declares `use_scm_version` which relies on [setuptools_scm](https://pypi.org/project/setuptools_scm) to do two things: - derive the project version from SCM tags - ensure that all files committed to the repo are automatically included in releases ## Running Tests The skeleton assumes the developer has [tox](https://pypi.org/project/tox) installed. The developer is expected to run `tox` to run tests on the current Python version using [pytest](https://pypi.org/project/pytest). Other environments (invoked with `tox -e {name}`) supplied include: - a `build-docs` environment to build the documentation - a `release` environment to publish the package to PyPI A pytest.ini is included to define common options around running tests. In particular: - rely on default test discovery in the current directory - avoid recursing into common directories not containing tests - run doctests on modules and invoke flake8 tests - in doctests, allow unicode literals and regular literals to match, allowing for doctests to run on Python 2 and 3. Also enable ELLIPSES, a default that would be undone by supplying the prior option. - filters out known warnings caused by libraries/functionality included by the skeleton Relies a .flake8 file to correct some default behaviors: - allow tabs for indentation (legacy for jaraco projects) - disable mutually incompatible rules W503 and W504. ## Continuous Integration The project is pre-configured to run tests in [Travis-CI](https://travis-ci.org) (.travis.yml). Any new project must be enabled either through their web site or with the `travis enable` command. In addition to running tests, an additional deploy stage is configured to automatically release tagged commits. The username and password for PyPI must be configured for each project using the `travis` command and only after the travis project is created. As releases are cut with [twine](https://pypi.org/project/twine), the two values are supplied through the `TWINE_USERNAME` and `TWINE_PASSWORD`. To configure the latter as a secret, run the following command: ``` echo "TWINE_PASSWORD={password}" | travis encrypt ``` Or disable it in the CI definition and configure it through the web UI. Features include: - test against Python 2 and 3 - run on Ubuntu Xenial - correct for broken IPv6 Also provided is a minimal template for running under Appveyor (Windows). ## Building Documentation Documentation is automatically built by [Read the Docs](https://readthedocs.org) when the project is registered with it, by way of the .readthedocs.yml file. To test the docs build manually, a tox env may be invoked as `tox -e build-docs`. Both techniques rely on the dependencies declared in `setup.cfg/options.extras_require.docs`. In addition to building the sphinx docs scaffolded in `docs/`, the docs build a `history.html` file that first injects release dates and hyperlinks into the CHANGES.rst before incorporating it as history in the docs. ## Cutting releases By default, tagged commits are released through the continuous integration deploy stage. jaraco.functools-2.0/test_functools.py000066400000000000000000000133201341267320200202510ustar00rootroot00000000000000import itertools import time import copy import random import functools from unittest import mock import pytest from jaraco.classes import properties from jaraco.functools import Throttler, method_cache, retry_call, retry __metaclass__ = type class TestThrottler: def test_function_throttled(self): """ 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): """ 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): class ThrottledMethodClass: @Throttler def echo(self, arg): 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): """ A deepcopy of an object with a method cache should still succeed. """ class ClassUnderTest: calls = 0 @method_cache def method(self, value): self.calls += 1 return value ob = ClassUnderTest() copy.deepcopy(ob) ob.method(1) copy.deepcopy(ob) def test_special_methods(self): """ Test method_cache with __getitem__ and __getattr__. """ class ClassUnderTest: getitem_calls = 0 getattr_calls = 0 @method_cache def __getitem__(self, item): self.getitem_calls += 1 return item @method_cache def __getattr__(self, name): self.getattr_calls += 1 return name ob = ClassUnderTest() # __getitem__ ob[1] + ob[1] assert ob.getitem_calls == 1 # __getattr__ ob.one + ob.one assert ob.getattr_calls == 1 @pytest.mark.xfail(reason="can't replace property with cache; #6") def test_property(self): """ Can a method_cache decorated method also be a property? """ class ClassUnderTest: @property @method_cache def mything(self): 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): """ 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): return random.random() ob = ClassUnderTest() assert ob.mything == ob.mything class TestRetry: def attempt(self, arg=None): if next(self.fails_left): raise ValueError("Failed!") if arg: arg.touch() return "Success" def set_to_fail(self, times): self.fails_left = itertools.count(times, -1) def test_set_to_fail(self): """ 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): self.set_to_fail(times=2) res = retry_call(self.attempt, retries=2, trap=ValueError) assert res == "Success" def test_retry_call_fails(self): """ 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): 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): self.set_to_fail(times=2) res = retry_call(self.attempt, retries=2, trap=Exception) assert res == "Success" def test_default_traps_nothing(self): self.set_to_fail(times=1) with pytest.raises(ValueError): retry_call(self.attempt, retries=1) def test_default_does_not_retry(self): self.set_to_fail(times=1) with pytest.raises(ValueError): retry_call(self.attempt, trap=Exception) def test_cleanup_called_on_exception(self): 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 assert cleanup.called_with() def test_infinite_retries(self): 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): 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): 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): 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-2.0/tox.ini000066400000000000000000000010511341267320200161350ustar00rootroot00000000000000[tox] envlist = python minversion = 2.4 [testenv] deps = setuptools>=31.0.1 commands = pytest {posargs} usedevelop = True extras = testing [testenv:build-docs] extras = docs testing changedir = docs commands = python -m sphinx . {toxinidir}/build/html [testenv:release] skip_install = True deps = pep517>=0.5 # workaround for https://github.com/pypa/twine/issues/423 git+https://github.com/pypa/twine path.py commands = python -c "import path; path.Path('dist').rmtree_p()" python -m pep517.build . python -m twine upload dist/*